Search code examples
ruby-on-railsruby-on-rails-4rails-i18n

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


I can't seem to find a documentation to list what helpers are available for Rails' i18n.

I first saw it in Simple Form's README

The closest I find is in the example here

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

However, I am also aware that labels and other things are also applicable in this section.

What is the complete list of available helpers entries?


Solution

  • Simple form i18n

    There's a short answer and a long answer for your question. This is the short version. If you want the long one, please let me know.

    Using i18n takes practice and keen attention paid to runtime context. But it's possible to setup only the simple form i18n wihout getting into the whole i18n architecture.

    The helpers you list are not a part of simple form, they are part of the standard application yaml. Except for select: which is part of simple form, but defined with the label 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}
    

    Simple form

    Simple form has it's own yaml section, which is within your application yaml but not within helpers: It's at the same precedence level in the yaml structure as helpers: if that helps.

    In your yaml -- the title label is simple_form:the structure and labels used in this file are critical.

    ...
      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...
    

    And this is the short answer. Let me know if you need any follow-up.

    Addendum: Finding the right contextual labels for any case

    Plugging in the holes with i18n-tasks gem

    Install the i18n-tasks gem -- It is a command line tool that will help understand the necessary structure for your apps localization strings in the YAML(s). It reveals the structure behind what is incredibly opaque.

    $i18n-tasks health
    

    It will spit out something like this:

    |   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  
    

    Each one of the label. levels reference your translation yaml directly.

    There are a number of commend line options. A useful one is

    $i18-tasks missing  
    

    Herein lies the twist -- in your code where you want a translation storing, use your translation helper with a bogus string t('bogus').

    i18-tasks will flag that as a missing translation string and in the process tell you the structure of the yaml for this string in this context in order for Rails i18n to find it.

    Yes, it's crazy backwards, but you only need to do this until you get an understanding of how the i18n is structured. And then from experience you'll have a better idea of where to put the strings in the localization yaml.

    Hammer approach For the sake of completeness, I'll also mention it is possible to hardcode every translation path, for instance...

    t('defaults.labels.read_more_link_label')
    

    Looks up:

    your_app_name:
      application:
        defaults:
         labels:
            read_more_link_label: "Read more..."
    

    There are a lot of features in the i18-tasks gem. It's pretty helpful.