Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rails, how to organize localized data for form elements other than labels?

I'm creating my first Rails app and it is going to have internationalization support.

I've already read the i18n guide, and it is a very good source. It even suggests a file organization for the localization files, which is a very good start point:

|-defaults
|---es.rb
|---en.rb
|-models
|---book
|-----es.rb
|-----en.rb
|-views
|---defaults
|-----es.rb
|-----en.rb
|---books
|-----es.rb
|-----en.rb
|---users
|-----es.rb
|-----en.rb
|---navigation
|-----es.rb
|-----en.rb

However, I'm not sure what is the best place to hold some extra (internationalized) information usually found in forms, like:

  • input placeholders
  • helper texts (beside the input, with a more detailed information about it)

The thing is: this information pertains to views (not to models). But, it is frequently described in a per model, per attribute basis - which makes me inclined to put them inside the models files, under the activerecord property.

My question: what is the best place to put this localized data? Both in terms of file organization (which folder) and in terms of the organization inside the file.

like image 509
fegemo Avatar asked Nov 08 '12 13:11

fegemo


1 Answers

I don't think there's a "correct" answer to this question.

Personally I tend to disagree somehwat with the organization suggested by the guide. i18n is, by nature, wholly in the domain of the view layer. To drop a "views" folder in there is a bit confusing, and probably has led more than one developer to the exact problem you're having now.

I would suggest throwing out the views/book files and keeping everything pertaining to the model in the same file. All translations are for "views", and keeping all translations pertaining to individual models in one place is probably cleaner and less error-prone.

# locale/models/model.en (or es, etc)

# all the activerecord translations
activerecord:
  models:
    # ...
  attributes:
    # ...
  errors:
    # ...
# then your form translations.
helpers:
  label:
    # Here you might add overrides for activerecord attribute lookups, but
    # do so with care (see note below).

  # then below are your custom view fields
  placeholders:
    model:
      attribute: 'translation'
  details:
    model:
      attribute: 'translation'

A note on helpers.label versus activerecord.attributes:

Rails itself currently looks in two places for label translations. First:

helpers:
  label:
    model:
      attribute: 'translation'

Falling back to the activerecord translations:

activerecord:
  attributes:
    model:
      attribute: 'translation'

This is somewhat of a trap, as it means form labels and activerecord errors (and other translations) will be different. This is fine if, for example, you want your form label to be more of a prompt (e.g. "Enter the body" for attribute "body"), but can also be a source of errors if your helper.label isn't obviously referring to the activerecord translation, e.g. ("Enter the message" for attribute "body"). So override with care.

like image 145
numbers1311407 Avatar answered Nov 14 '22 01:11

numbers1311407