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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With