简体   繁体   中英

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

Integrating i18n with Simple Form gem

Simple_form accommodates i18n well, and their docs are pretty thorough on i18n, but they don't specifically address the solution for the "priority:" option for a collection field. I've tried applying the idea/structure they use for "hints:" and "prompts:" but it isn't working. I must be missing something.

Excerpt from 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    

Here's a snippet from the form:

# 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 %> 

I could make this work by creating a custom t. string for the "priority:" setting in a default yml, but it would be more logical and maintainable if this setting's string had a consistent implementation with simple_form's the "hints:" and "prompts:" There must be a simple_form solution. Thanks in advance.

Added notes (Edit) For anyone else who has this problem, I wanted to add notes here. Translating the path-to-str verbatim at the console will work. But, the Simple From gem uses context dependent translations based on the yml formatted in a way their gem interprets. Their specification is clear on this and this works for the 2 form properties "hints" and "prompts." The problem seems to be that Simple Form doesn't implement this context correctly for the form collection property "priority" in the same way it does for "prompts" and "hints." The form property "Priority" is not spec'ed by Simple Form, and does not work, which makes me think that it's been omitted on purpose. I'm going to have to contact the gem builders and see what they have to say.

i18n implementation in Simple Form for the "priority" property for a collection form field

To get an answer, I contacted the gem contributors on the git repo for Simple Form. Summary of the question I asked:

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.

The answer is both yes and no.

Yes, for the country select field, if you are using the country_select gem

Country priorities are handled by the country_select gem. Simple Form delegates that to country select's "priority" string setting. So you do not explicitly add a string to your simple form's yaml, like I tried to do in the question example above. So my problem is solved by setting priority in the country_select component, okay.

But what about other collections, lists of things for which you'd like to have a default selected and have that be part of your simple_form translation yaml?

No, for other custom collections

I'll quote a contributor to the project

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

It's not the end of the world

In the Simple Form form loop, the hints, labels and prompts are all pulled in from the simple_form yaml you've set up, but you will have to explicitly assign a priority field with a translation string in the form itself. See code example below.

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

Important side note: For the "prompt" you've set up in the yaml to work, you must explicitly invoke :translate on prompt: (You need not be this explicit on the t string, I just wanted the example to be clear as possible.)

<%= 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 %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM