سؤال

I am localizing an app with the default rails I18n with globalize3 as the back-end.

Is it possible to set a locale with a country code (ie :fr-CA) to fallback to its specific language (:fr) before going to the default fallback automatically? I know its possible to set each locale/country manually with

config.i18n.fallbacks = {'fr-CA' => 'fr'}

But it would be nice to not have to add each fallback manually and have this behaviour automatic.

هل كانت مفيدة؟

المحلول

To achieve precisely this I have an initializer with

I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)

See the source code for more info.

Edit:

This reminds me, there is an annoying bug in the ActionView LookupContext which prevents this from working for localized views (though it works correcly for locale files). I see it still hasn't been fixed. Basically, if you have any localized views (help pages for example, which are unsuitable to store in locale files due to their length) then a fr-CA locale will not fall back to a view called help.fr.html.erb. You either have to name the file help.fr-CA.html.erb or, which is what I have done, monkeypatch the LookupContext with another initializer, sort of like this:

module ActionView
  class LookupContext
    # Override locale= to also set the I18n.locale. If the current I18n.config object responds
    # to original_config, it means that it's has a copy of the original I18n configuration and it's
    # acting as proxy, which we need to skip.
    def locale=(value)
      if value
        config = I18n.config.respond_to?(:original_config) ? I18n.config.original_config : I18n.config
        config.locale = value[0,2] # only use first part of the locale in lookups
      end
      super(@skip_default_locale ? I18n.locale : default_locale)
    end
  end
end

Another edit: Note that the patch is rather crude and breaks full locale lookups, going straight for just the language. If you need to also have fully matching views (language-REGION) you'll need to improve my code!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top