How to configure locale aliases using i18n & rails?

点点圈 提交于 2019-12-04 19:00:39

Short answer

Take a look to the fallbacks

Create a file in initializers like i18n_fallbacks.rb

config.i18n.fallbacks = {:no => [:nb], :nn => [:nb]}

Here the reference

Things related

You can even set multiple fallbacks, and they will be taken in the same order as specified:

for instance:

config.i18n.default_locale = :de
config.i18n.fallbacks = {:de => [:en,:es]}

de.yml

  :de:
    greeting: Hallo

en.yml

  :en:
    foo: bar

es.yml

  :es:
    bar: baz

You get the following:

I18n.t :greeting # found in de.yml, no fallback
# => 'Hallo'

I18n.t :foo # not in :de, try in :en and found
# => "bar"

I18n.t :bar # not in :de, try in :en and in :es
# => "baz"

I81n.t :other # not found anywhere, note the message delivers not found for the current locale:
# => "translation missing: de.other"

In latest i18n gem (0.7.0) I have found it necessary to define fallback locales like this (in config/application.rb):

# Custom I18n fallbacks
config.after_initialize do
  I18n.fallbacks = I18n::Locale::Fallbacks.new(at: :"de-DE", ch: :"de-DE", gb: :"en-US")
end

You also need to set config.i18n.fallbacks = true in all config/environments/*.rb files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!