使用 Rails i18n 支持不同的语言环境
我使用 标准 Rails I18n API 来本地化我们的一些视图。这确实运作良好,但我们现在有一些对 en
语言环境进行区域更改的用例。
API 指南提到不直接支持此功能,应使用其他插件。但是,我想知道是否有更简单的方法可以做到这一点。
我已经有了 en.yml
,所以理论上我可以创建 en-AU.yml
和 en-US.yml
它们是有效的克隆en.yml
,但应用了一些区域更改。然后,我可以向我们的配置添加额外的 English - American
和 English - Australian
选项,这些选项将映射到新的特定于区域的区域设置,并允许用户使用特定于区域的区域设置。
我能想到的唯一问题是它不干燥——我会对所有常见的英语单词都有重复的翻译。我看不到解决这个问题的办法。
这种方法还有其他缺点吗?还是我应该硬着头皮去使用 Globalize2 等插件?
I'm using the standard Rails I18n API to localise some of our views. This is working really well, but we now have a few use cases for regional changes to the en
locale.
The API guide mentions that this isn't supported directly, and other plugins should be used. However, I'm wondering whether there's a simpler way to do this.
I already have en.yml
, so in theory I could just create en-AU.yml
and en-US.yml
which are effectively clones of en.yml
but with a few regional changes applied. I could then add additional English - American
and English - Australian
options to our configuration which would map to the new region-specific locales and allow users to use a region-specific locale.
The only problem I can think of with this is that it isn't DRY -- I would have duplicate translations for all common English words. I can't see a way around this.
Are there any other disadvantages to this approach, or should I just bite the bullet and dive into one of the plug-ins such as Globalize2 instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Rails-i18n-translation-inheritance-helper 现在有点过时了,所以这是我针对 Rails 3.2 项目的方法。
如果您将 en-US 和 en-AU 保留在同一个 en.yml 文件中,则可以使用 yml 重复节点来拥有超级 en 部分:
例如:
The rails-i18n-translation-inheritance-helper is getting a bit old now, so here's my approach for a Rails 3.2 project.
If you keep both en-US and en-AU in the same en.yml file you can use the yml repeated node to have a super en section:
For example:
我为此使用 翻译继承帮助器插件。
I'm using translation inheritance helper plugin for this.
在较新版本的 Rails/i18n 中,他们添加了后备功能。与过时的
翻译继承助手
gem 的工作原理类似,请参阅此答案以获取更多信息:如果翻译丢失,则返回默认语言
In newer versions of Rails/i18n, they've added a fallback feature. Works similar to the outdated
translation inheritance helper
gemsee this answer for more info: Fall back to default language if translation missing