Ruby on Rails 4 和 Simple Form 中 "helpers" 的可用 i18n 翻译列表?

List of available i18n translations for "helpers" in Ruby on Rails 4 and Simple Form?

我似乎找不到文档来列出 helpers 可用于 Rails' i18n。

我第一次看到它是在 Simple Form 的 README

我找到的最接近的是 the example here

helpers:
    select:
      prompt: Please select
    submit:
      create: Create %{model}
      submit: Save %{model}
      update: Update %{model}

但是,我也知道 labels 和其他内容也适用于此部分。

可用 helpers 条目的完整列表是什么?

简单形式 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...

而这个的简短答案。如果您需要 follow-up,请告诉我。

附录:为任何案例找到正确的上下文标签

用 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 中有很多功能。很有帮助。