How do you structure i18n yaml files in Rails?

I started filling out the en yaml file in Rails, and I can already say that it is too confused and out of order for too long. Is there an agreement to save this file?

So far I have this structure:

language: resource: pages: # index, show, new, edit page html elements: # h1, title activerecord: attributes: model: property: 

Now I have the following things that I want to fit into this structure, but I'm not sure how:

  • Navigation
  • Button text (saving changes, creating an account, etc.)
  • Controller Flash Error Messages
  • How to add verbose keys. Am I using a space or underscore? For exmaple, t(".update button") ) or t(".update_button")

Is there a convention for the locale file structure?

+45
ruby-on-rails yaml ruby-on-rails-3 internationalization structure
Apr 23 2018-12-12T00:
source share
6 answers

I found that the best general strategy is to reproduce the file structure somewhat so that with any translation I can immediately find where it was called from. This gives me some context for the translation.

Most application translations are in views, so my biggest top-level namespace is usually views .

I create helper namespaces for the controller name and the name of the action or partial use of ex:

  • views.users.index.title
  • views.articles._sidebar.header

Both of these examples should make it obvious which part of my application we are translating, and which file to look for in order to find it.

You mention navigation and buttons, if they should be shared, then they belong to the views.application namespace as well as their type mappings:

  • views.application._main_nav.links.about_us is the link in our part of the navigation of the main application.
  • views.application.buttons.save
  • views.application.buttons.create - I have a bunch of these buttons, ready to use if necessary

Flash messages are generated using the controller, so their top-level namespace is ... controllers ! :)

We apply the same logic as for the views:

  • controllers.users.create.flash.success|alert|notice

Again, if you want to provide generic flash messages such as "Operation successful," you should write something like this:

  • controllers.application.create.flash.notice

Finally, the keys can be any valid YAML, but use words . as delimiters and underscores _ between words as a legend.

Now we just have to figure out if the rails translate into our own namespace in order to clear our upper level :)

+58
Apr 28 '12 at 14:20
source share

I know that the answer has already been accepted, but this question gave me some food for thought, and I thought that I would share a different structure for the Rails i18n yml files for your consideration / criticism.

Given that I would like

  • keep the default structure of the application so that I can use shorthand "lazy" queries, for example t('.some_translation') in my views,
  • avoid as many line repeats as possible, in particular with words that are not the same but also have the same contexts / meanings,
  • you only need to change the key once so that it is reflected everywhere that it referred to,

for the config / locales / en.yml file , which looks something like this:

 activerecord: attributes: user: email: Email name: Name password: Password password_confirmation: Confirmation models: user: User users: fields: email: Email name: Name password: Password confirmation: Confirmation sessions: new: email: Email password: Password 

I see that there is significant repetition, and that the context of words such as "Email" and "Password" is unambiguous and has the same meaning in their respective representations. It would be a little annoying that I need to go and change them all if I decide to change "Email" to "e-mail", so I would like to reorganize the lines for a link to some kind of dictionary. So, how about adding a hash dictionary to the top of the file with some anchored & :

 dictionary: email: &email Email name: &name Name password: &password Password confirmation: &confirmation Confirmation activerecord: attributes: user: email: *email name: *name password: *password password_confirmation: *confirmation models: user: User users: fields: email: *email name: *name password: *password confirmation: *confirmation sessions: new: email: *email password: *password 

Whenever you get more than one copy of exactly the same word / phrase in your submissions, you can reorganize it in the dictionary. If translating the dictionary in the base language does not make sense for the target language, simply change the reference value in the target language to a static string or add it as an additional entry to the dictionary of the target language. I am sure that each dictionary of the language can be reorganized into a different file if they become too large and bulky.

This way of structuring the i18n yaml files seems to work well with some local test applications in which I tried it. I hope the great Localeapp will provide support for this type of anchor / link in the future. But, in any case, all these vocabulary conversations cannot be an original idea, are there any other problems with binding binding in YAML, or maybe just with the general concept of a “dictionary” in general? Or is it simply better to just completely remove the default backend and replace it with Redis or something else?

+46
Jun 18 '12 at 20:00
source share

It is not easy to answer your question, and there is little material in this topic. I found the best resources:

So, I will try right here:

  • Navigation

    I think you mean navigation elements here, such as breadcrumbs, tabs, ... You must define views for them and adhere to the rule for moving all view elements in separate files in the views directory (see Style for rule).

  • Button text (save changes, create an account, etc.)

    View items, go to the same files. If you use the same buttons in different views, define a shared file and use it.

  • Controller Flash Error Messages

    I would use the same rule as for submissions. Define a separate directory, include the files for the controllers there.

  • How to add verbose keys. Am I using a space or underscore? For exmaple, t (". Update button")) or t (". Update_button")

    I would prefer to use .update_button rather than .update button , because it makes it more explicit that it is a single key.

+7
Apr 28 '12 at 10:35
source share

This is almost two years after I asked this question, and I want to share some thoughts. I believe that the optimal structure is to translate the namespace in accordance with their role as MVC (models, views, controllers). This keeps the locale file in order and prevents namespace conflicts (for example, the en.users can represent a view or controller).

 en: controllers: users: show: welcome_flash: "Welcome back!" mailers: users_mailer: welcome_email: subject: "Good of you to join us" views: users: show: notice: "Oh no! 

But using neat namespaces violates the lazy search function in Rails. If you use lazy search, Rails will automatically enter a namespace for you, and it will not contain the top-level namespaces you created ( views , controllers , etc.).

For example, the scope t('.welcome_flash') resolves to en.users.show . Which stinks because users are not clearly defined. What is it? Controller? View? Something else?

To solve this problem I created gem I18nLazyLookup . This allows you to use lazy searches with your own namespaces.

Instead of using t you can use t_scoped('welcome_flash') and this will automatically change the scope to en.controllers.users.show . It also works for browsing and mailing lists, and you can customize the namespace as you like.

+7
Jan 28 '15 at 10:58
source share

Editing yaml files directly leads to messy and unreadable files.
Moreover, it will be difficult for you to provide access to translators if someday you want the non-developer to add a new language.

I would recommend using localeapp , which generates a single yaml file.
But it allows you to easily see and manage your translations in the web interface.
And to create additional access to translators.

+6
Apr 23 2018-12-12T00:
source share

A few years after the battle, but here is a (somewhat complete) answer.

For starters, I don't like the standard t('.xxx') style with the default namespace based on the file structure. I also don't really like the classification of translations depending on the structure of the DOM. Although this is a good approach for very structured translations, it is often repeated and not very intuitive.

I would prefer to rearrange my translations into more useful categories to make it easier for my translators to translate, because they can work on specific topics and not with some weird styles (some translators don't even know what MVC means)

therefore my translation file is structured this way

 fr: theme1: theme11: translationxxx: blabla theme2: translationyyy: blabla 

Depending on the needs, “themes” can be a model, a more abstract context, etc., which is the most intuitive for translators.

Since it would be a difficulty to write each time in scope in my views, I added some convenient methods in my helpers to have a stack based broadcast context.

  • I press / change the translation frame on the stack in my views by calling t_scope([new_scope] and pop_t
  • I override t helper to use last stack area

Code for translation review methods is available which answers

0
Mar 14 '16 at 22:08
source share



All Articles