Rails I18n 多文件别名
我想知道是否可以拥有一个语言环境的多个文件,也许可以在目录中组织并仍然使用 YAML 别名,例如:
在公共文件中:
# config/locales/common/en.yml
en:
first_name: &first_name "First name"
last_name: &last_name "Last name"
和在更具体的文件中:
# config/locales/models/user/en.yml
en:
helpers:
label:
user:
first_name: *first_name
last_name: *last_name
这将有助于最大限度地减少翻译和翻译更改。在我的 Rails 配置中,我将其设置为加载子目录中的所有语言环境文件。此设置对我不起作用,加载页面时出现错误的别名错误。
我尝试使用一个 init 脚本,将所有 en.yml 编译为一个文件,然后只使用该一个文件,并且别名可以正常工作,但我无法合并键。例如,如果我在其他文件中有另一个“助手”,它只使用最后指定的任何内容(事实上,“en”也不断被覆盖)而不是合并键。
或者这一切有更好的模式吗?非常感谢!
I was wondering if it's possible to have multiple files of a locale, maybe organized in directories and still use the YAML aliases, for example:
in a common file:
# config/locales/common/en.yml
en:
first_name: &first_name "First name"
last_name: &last_name "Last name"
and in a more specific file:
# config/locales/models/user/en.yml
en:
helpers:
label:
user:
first_name: *first_name
last_name: *last_name
This will help minimize translations and change in translations. in my rails config, i set it to load all locale file in subdirectories. This setup does not work for me, i get a bad alias error when i load a page.
I tried having an init script that compiles all the en.yml into one and just use that one file, and the alias works and all, but i cant have the keys merging. For example, the if i have another "helpers" in some other file, it just uses whatever was specified last (in fact, the "en" keeps getting overridden, too) instead of merging the keys.
Or is there a better pattern to all of this? Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我了解,您想要拥有多个带有锚点和别名的 .yml 文件,或者拥有一个具有关键合并功能的文件。
不幸的是,由于 Rails 的工作方式,这两件事都是不可能的。
技术背景:
Rails 的 I18n 后端使用两个独立的系统执行两个主要步骤:
.yml
文件单独加载使用 YAML 库。这就是解析锚点和别名并构建 Ruby 哈希的地方。此步骤中不允许有重复的节点,即每个节点都会覆盖具有相同名称和父级的前一个节点。这就是锚点和别名不能跨越多个文件的原因。另一方面,您可以获得 YAML 不支持的简洁合并功能。
换句话说:通过将所有内容连接到一个巨大的文件中,您可能会获得“全局别名”功能,但您会失去合并/重复键功能。您可以选择其中之一,但不能同时拥有两者。就可维护性而言,多文件方法肯定更令人愉快。
PS:当然,你提到的重复也让我有点畏缩,但最好关注DRY 键和良好的键结构,而不是DRY 翻译。
这是因为翻译经常会随着时间的推移而变化:应用程序早期阶段相同的内容在应用程序发展后可能会略有不同。也许在某些地方它必须是“名字”,而在另一个地方它必须是“请输入您的名字”。
我还可以想象,就语法和其他与英语无关的上下文而言,拥有规范的主翻译并不适用于每种语言。
所以我个人的建议是:坚持使用 Rails 建议的方法(即多个文件)并忽略一些不可避免的重复。
As far as I understand you want to either have multiple .yml files with anchors and aliases or have a single file with the key merging feature.
Unfortunately both things are impossible, because of the way Rails works.
Technical background:
There are two major steps which are performed by the I18n backend of Rails using two separate systems:
.yml
file is loaded individually using a YAML library. That's where the anchors and aliases are resolved and a Ruby hash is built. Duplicate nodes are not allowed in this step, i.e. every node overrides a previous node with the same name and parent.That is why anchors and aliases cannot span multiple files. On the other hand you get a neat merge feature which isn't supported by YAML.
In other words: By concatenating everything into a giant file you'd potentially gain the "global alias" feature, but you'd loose the merge/duplicate keys feature. You can have one or the other, but not both. In terms of maintainability the multiple file approach is definitely more pleasant.
PS: Of course the duplication you mention makes me cringe a little, too, but it's better to focus on DRY keys and a good key structure than on DRY translations.
That's because translations often change in time: What was the same in the early stages of your application might be slightly different after your application has evolved. Perhaps at some places it must be "First name" and at another it must be "Enter your first name, please".
I also can imagine that having a canonical master translation doesn't work for every language, regarding grammar and other contexts which have no relevance in English.
So my personal advice would be: Stick with the method suggested by Rails (i.e. multiple files) and ignore some unavoidable duplication.
此处提供了组织语言环境的默认模式:
http://guides.rubyonrails.org/i18n。 html#组织区域设置文件
The default pattern to organize locales is presented here:
http://guides.rubyonrails.org/i18n.html#organization-of-locale-files
尝试将 *config.i18n.load_path* 参数的 application.rb 默认值替换为:
它对我有用。
Try replace in application.rb default value of *config.i18n.load_path* parameter with that:
It works for me.