您如何在Rails中构造i18n yaml文件? [英] How do you structure i18n yaml files in Rails?
问题描述
我开始在Rails中填充一个en yaml文件,我已经知道它很快就会变得凌乱和无法控制.是否有使该文件井井有条的惯例?
I started populating an en yaml file in Rails and I can already tell it will get messy and out of hand before too long. Is there a convention to keeping this file organized?
到目前为止,我具有以下结构:
So far I have this structure:
language:
resource:
pages: # index, show, new, edit
page html elements: # h1, title
activerecord:
attributes:
model:
property:
现在我想将以下内容放入此结构中,但不确定如何:
Now I have the following things that I want to fit into this structure but I'm unsure how to:
- 导航
- 按钮文字(保存更改,创建帐户等)
- 来自控制器闪存的错误消息
- 如何添加多字键.我要使用空格还是下划线?例如,
t(".update button")
)或t(".update_button")
- Navigation
- Button text (save changes, create account, etc)
- Error messages from controller flash
- How to add multi-word keys. Do I use a space or an underscore? For exmaple,
t(".update button")
) ort(".update_button")
语言环境文件结构是否有约定?
Is there a convention to locale file structure?
推荐答案
我发现最好的整体策略是某种程度地重现文件结构,以便进行任何翻译,我都可以立即找到它的调用位置.这为我提供了进行翻译的环境.
I've found that the best overall strategy is to somewhat reproduce the file structure so that given any translation, I can immediately find where it was called from. This gives me some kind of context for making the translation.
大多数应用程序翻译都在视图中找到,所以我最大的顶级名称空间通常是views
.
The majority of application translations are found in views, so my biggest top level namespace is usually views
.
我为控制器名称和动作名称或部分使用的子名称创建子名称空间:
I create sub namespaces for the controller name and the action name or partial being used ex :
-
views.users.index.title
-
views.articles._sidebar.header
views.users.index.title
views.articles._sidebar.header
这两个示例都应该清楚表明我们正在翻译我的应用程序的哪一部分以及要查找的文件.
Both of these examples should make it obvious what part of my app we're translating and which file to look in to find it.
您提到导航和按钮,如果它们是通用的,则它们与视图对应项一样属于views.application
命名空间:
You mention navigation and buttons, if they are to be generic, then they belong in the views.application
namespace just as do their view counterparts :
-
views.application._main_nav.links.about_us
-应用程序主导航部分中的链接 -
views.application.buttons.save
-
views.application.buttons.create
-我有一堆这样的按钮可以在需要时使用
views.application._main_nav.links.about_us
- a link in our app's main navigation partialviews.application.buttons.save
views.application.buttons.create
- I have a bunch of these buttons ready to be used when needed
Flash消息是从控制器生成的,因此它们的顶级名称空间是... controllers
! :)
Flash messages are generated from the controller, so their top level namespace is... controllers
! :)
我们对视图应用相同的逻辑:
We apply the same logic as we do to views :
-
controllers.users.create.flash.success|alert|notice
同样,如果您想提供诸如操作成功"之类的通用Flash消息,则应编写如下内容:
Again if you wanted to provide generic flash messages like "Operation successful", you would write something like this :
-
controllers.application.create.flash.notice
最后,键可以是任何有效的YAML,但作为约定,请坚持使用句点.
作为分隔符,并在单词之间使用下划线_
.
Finally, keys can be anything that is valid YAML, but please stick to using periods .
as separators and underscores _
between words as a matter of convention.
现在唯一需要解决的就是将Rails的翻译转换到其自己的名称空间中,以清理顶层:)
The only thing left to sort out now, is getting rails' translations into its own namespace to clean up our top level :)
这篇关于您如何在Rails中构造i18n yaml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!