Rails - 如何动态添加/覆盖 i18n yaml 的措辞
例如,我有一个默认的英语语言环境文件“en.yml”,其内容为:
en:
messages: messages
users: users
现在,有一位客户希望在他的产品中将消息命名为讨论,但用户应该保留为用户。所以我想要做的是创建“customer.en.yml”文件
en:
messages: discussions
,它将覆盖默认的“messages”翻译,但会保持所有其他单词相同。我怎样才能实现它?
因为如果我用 : 加载 en.yml ,
config.i18n.load_path += Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')]
然后加载 customer.en.yml (APP_CONFIG['customer_name'] 是之前定义的),
config.i18n.load_path += Dir[File.join(RAILS_ROOT, 'config', 'custom_locales', APP_CONFIG['customer_name']+'.{rb|yml}')]
它只会覆盖我的“en”语言环境,并且“users”翻译将会消失,对吧?
as an example, i have a default english locale file "en.yml" with contents:
en:
messages: messages
users: users
now, there is a customer which wants messages to be named discussions in his product, but users should remain users. so what i want to do is to create "customer.en.yml" file
en:
messages: discussions
which would override the default "messages" translation, but would keep all the other words same. how can i achieve it?
because if i load en.yml with :
config.i18n.load_path += Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')]
and afterwards load customer.en.yml (APP_CONFIG['customer_name'] is defined before) with
config.i18n.load_path += Dir[File.join(RAILS_ROOT, 'config', 'custom_locales', APP_CONFIG['customer_name']+'.{rb|yml}')]
it will just overwrite my "en" locale, and "users" translation will disappear, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 i18n.fallbacks
我不确定这是什么时候添加到 Rails 中的,但在 4.2.2 中你可以这样做:
如果你还设置了
config.action_view.raise_on_missing_translations = true
(我在开发中这样做,并且test),仅当在区域设置或后备中找不到键时才会引发该错误。Use i18n.fallbacks
I'm not sure when this was added to Rails, but in 4.2.2 you can do:
If you also set
config.action_view.raise_on_missing_translations = true
(which I do in development and test), it will raise only if a key isn't found in the locale or the fallback.它不应覆盖您的“en”区域设置。翻译已合并。简单的 I18n 后端中的
store_translations
调用merge_translations
,如下所示:如您所见,只有您在后面的翻译 yml 文件中输入的键才会被覆盖。
It should not overwrite your "en" locale. Translations are merged.
store_translations
in the simple I18n backend callsmerge_translations
, which looks like this:As you can see, only the keys you enter in the latter translation yml file will be overwritten.
当我们编写pirate.yml翻译时,我想要类似的东西,但我希望pirate.yml中未定义的任何内容都默认为en.yml中的内容。
我写的内容可以在 Github 上找到。
I wanted something similar when we were writing a pirate.yml translation, but I wanted anything not defined in pirate.yml to default to what we had in en.yml.
What I wrote can be found on Github.
我认为你可能混淆了两种不同的东西。
i18n 文件用于翻译。
如果您的客户需要某个字段的特定名称,那么这不是翻译问题,而是功能问题。
换句话说,我认为您需要这样的东西:
然后在其他地方添加特定功能,例如在您的视图中:
或者,您可以向客户添加一个字符串属性,默认为“消息”。那么你的视图将如下所示:
I think you might be mixing up two different things.
The i18n file is for translations.
If you have a client that needs an specific name for some field, that is not a translation issue, but functionality.
In other words, I think you need something like this:
And then add the specific functionality somewhere else, for example in your view:
Alternatively, you could add a string attribute to your customers, defaulting to 'messages'. Then your view would look like this: