如何以简单形式 gem 为集合的 "priority:" 选项实现 i18n

How to implement i18n in the Simple Form gem for a collection's "priority:" option

将 i18n 与简单表单集成 gem

Simple_form 很好地适应了 i18n,他们的 docs 在 i18n 上非常详尽,但他们没有专门解决集合字段的 "priority:" 选项的解决方案。我已经尝试应用他们用于 "hints:" 和 "prompts:" 的 idea/structure 但它不起作用。我肯定错过了什么。

yml 摘录:

simple_form:
    hints:
      location:
        website: You must enter the leading http:// 
        short_desc:  General information, not a review.   
    prompts:
      location:
        state: Select the state   
    # Tried this, doesn't seem to work       
    priority:
      location:
        country: United States of America    

这是表格的一个片段:

# Works for prompt per spec.
<%= f.input :state, collection: us_states, prompt: :translate %> 

# Tried this. Nope. Missing translation error.
<%= f.input :country, priority: [ t('.country') ] %> 

# Tried this. Nope.  Missing translation error.
<%= f.input :country, priority: :translate %> 

我可以通过创建自定义 t 来完成这项工作。默认 yml 中 "priority:" 设置的字符串,但如果此设置的字符串与 simple_form 的 "hints:" 和 "prompts:" 具有一致的实现,它将更合乎逻辑且更易于维护必须有一个 simple_form 解决方案。提前致谢。

添加注释(编辑) 对于遇到此问题的任何其他人,我想在这里添加注释。在控制台逐字翻译 path-to-str 将起作用。但是,Simple From gem 使用基于以其 gem 解释方式格式化的 yml 的上下文相关翻译。他们的规范对此很明确,这适用于 2 个表单属性 "hints" 和 "prompts." 问题似乎是简单表单没有为表单集合正确实现此上下文 属性 "priority" 与对 "prompts" 和 "hints." 的处理方式相同我认为它是故意省略的。我将不得不联系 gem 建设者,看看他们有什么要说的。

集合表单字段"priority"属性的简单表单中的i18n 实现

为了得到答案,我联系了 Simple Form 的 git 存储库中的 gem 贡献者。我问的问题总结:

Is the form collection field feature "priority" implemented in i18n for Simple Form? If so, I would be most grateful for information on the yaml needs to be structured to get Simple Form to use the translation.

答案既是又不是。

是,对于国家 select 字段,如果您使用 country_select gem

国家优先事项由 country_select gem 处理。简单表单将其委托给国家 select 的 "priority" 字符串设置。因此,您没有明确地将字符串添加到简单表单的 yaml 中,就像我在上面的问题示例中尝试做的那样。所以我的问题是通过在country_select组件中设置优先级来解决的,好吧。

但是其他集合,您希望对其进行默认 select 编辑并使其成为您的 simple_form 翻译 yaml 的一部分的事物列表呢?

否,对于其他自定义集合

我将引用该项目的贡献者

There's nothing related to generic priority fields right now, only for countries/time zones that are delegated to country_select/Rails.

这不是世界末日

在 Simple Form 表单循环中,提示、标签和提示都是从您设置的 simple_form yaml 中提取的,但是您必须明确分配一个带有翻译字符串的优先级字段在表格本身。请参阅下面的代码示例。

en.yml

en:
  # Simple_form custom widgets
  simple_form:
    hints:
      location:
        website: Must be a valid website 
        short_desc:  50 words ofr less   
    prompts:
      location:
        state: Select the state   
    # Must be explicitly referenced      
    priority:
      location:
        country: United States of America    

form.html.erb

重要的旁注:为了让您在 yaml 中设置的 "prompt" 正常工作,您必须在提示时显式调用 :translate :(您不必在t 字符串,我只是想让示例尽可能清楚。)

<%= simple_form_for(@location) do |f| %>
      <%= f.input :name %>
      <%= f.input :street %>
      <%= f.input :city %>
      <%= f.input :state, collection: us_states, prompt: :translate %>
      <%= f.input :country, priority:[t('simple_form.priority.location.country')] %> 
      <%= f.input :phone %>
      <%= f.input :website %>
      <%= f.button :submit, class: "btn btn-primary" %>
    <% end %>