简单形式 i18n
您的问题有一个简短的答案和一个长答案。这是短版。如果你想要长的,请告诉我。
使用 i18n 需要练习并高度关注运行时上下文。但也可以只设置简单的 i18n 形式而不进入整个 i18n 架构。
您列出的助手不是简单表单的一部分,它们是标准应用程序 yaml 的一部分。 select: 除外,它是简单形式的一部分,但使用标签 prompts: 定义。
your_app_name:
application:
helpers:
submit:
model_1_name:
create: Add %{model}
delete: Delete %{model}
update: Save changes to %{model}
model_2_name:
create: Like
delete: Unlike
update: Unlike
mode_3_name:
create: Add %{model}
delete: Delete %{model}
update: Save changes to %{model}
简单形式
简单表单有自己的 yaml 部分,它在您的应用程序 yaml 中,但不在 helpers: 中。如果有帮助,它在 yaml 结构中的优先级与 helpers: 相同。
在您的 yaml 中 -- 标题标签为 simple_form:该文件中使用的结构和标签至关重要。
...
simple_form:
error_notification:
default_message: "Please review the problems below:"
hints:
your_model:
column_1_name: Your hint sentence.
column_whatever_name: Please enter the ...
another_model:
column_in_this_model: Valid range is from 1 to 5 ....
labels:
your_model:
column_1_name: Email
column_2_name: Password
priority:
model:
column: Text
prompts:
model:
column: Text
required:
text: 'required'
mark: '*'
prompts:
your_model:
column_name: Select the type of..
another_model:
column_name: Select the state...
而这是简短的回答。如果您需要任何跟进,请告诉我。
附录:为任何情况找到正确的上下文标签
使用 i18n-tasks gem 填补漏洞
安装i18n-tasks gem -- 它是一个命令行工具,可帮助您了解 YAML 中应用本地化字符串的必要结构。它揭示了令人难以置信的不透明背后的结构。
$i18n-tasks health
它会吐出这样的东西:
| en | simple_form.error_notification.default_message | Please review the problems below: |
| en | simple_form.hints.location.short_desc | General information, not a review. |
| en | simple_form.hints.location.website | Please enter the leading http:// or https:// |
| en | simple_form.hints.review.rating | Range is from 1 = Meh to 5 = Super yum |
| en | simple_form.labels.session.email | Email |
| en | simple_form.labels.session.password | Password |
| en | simple_form.no | No |
| en | simple_form.priority.article.category | Article |
| en | simple_form.priority.location.country | United States of America |
| en | simple_form.prompts.article.category | Select the type of article |
| en | simple_form.prompts.location.state | Select the state |
| en | simple_form.required.mark | * |
| en | simple_form.required.text | required |
| en | simple_form.yes
每个label. 级别都直接引用您的翻译 yaml。
有许多推荐行选项。一个有用的是
$i18-tasks missing
这就是转折点——在您想要翻译存储的代码中,使用带有伪造字符串 t('bogus') 的翻译助手。
i18-tasks 会将其标记为缺少的翻译字符串,并在此过程中告诉您此字符串在此上下文中的 yaml 结构,以便 Rails i18n 找到它。
是的,倒退很疯狂,但您只需要这样做,直到您了解 i18n 的结构。然后根据经验,您将更好地了解将字符串放在本地化 yaml 中的位置。
锤击法
为了完整起见,我还要提到可以对每个翻译路径进行硬编码,例如...
t('defaults.labels.read_more_link_label')
抬头:
your_app_name:
application:
defaults:
labels:
read_more_link_label: "Read more..."
i18-tasks gem 中有很多功能。很有帮助。