在生产环境中,Rails的本地化回退功能无法正常工作。

5
我有一个Rails 3.2应用程序。 它有两个本地化语言,韩语和英语。 韩语是默认语言,但如果不可用,我希望它回退到英语。 在开发环境中,回退可以正常工作,但在生产环境中却不能。
[config/application.rb]
    config.i18n.default_locale = :ko
    config.i18n.fallbacks = [:en]

[config/environments/production.rb]
  config.i18n.fallbacks = true


[config/locales/en.yml]
  ttt: TTT

[config/locales/ko.yml]
  (ttt is not defined)


**In development console:**

I18n.locale #=> :ko
I18n.t("ttt") #=> "TTT" (Works fine)


**In production console:**

I18n.locale #=> :ko
I18n.t("ttt") #=> "translation missing: ko.ttt" (Not working)

我错过了什么?


1
我在config/environments/production.rb中注释掉了config.i18n.fallbacks = true。现在它可以工作了。可能这并不是看起来的那样。 - Sam Kong
2个回答

8

如果在您的生产 / staging环境中注释掉config.i18n.fallbacks = true,则它将按预期工作。


设置 fallbacks = true 将覆盖 fallbacks = [:en],这样 default_locale 就会被用作回退语言(在该问题中是 ko 而不是 en)。 - Christopher Oezbek

3
即使这个问题/答案已经很老了,我仍然会分享我在我的情况下发现的解决方法(Rails 5.X)。设置应该像这样在application.rb中。
config.i18n.default_locale = :en

config.i18n.available_locales = %i(en de)
config.i18n.fallbacks = {
  de: :en
}

因此,在不同的环境中,所有对config.i18n.fallbacks = true的引用都应删除。


我认为将fallbacks作为一个可以是booleanHashArray等配置变量的设计选择真的很糟糕。像@Cris R所说,使用Hash是最好的选择。 - Christopher Oezbek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接