Question

I am using translation files to manage the various languages of my Rails app:

fr:
  books:
    index:
      title: "Livres"

What is the Rails way of storing a title that appears in multiple views?

This is not DRY at all and therefore not what I want:

fr:
  books:
    index:
      title: "Livres"
    new:
      title: "Livres"
    edit:
      title: "Livres"

Right now I am doing something like this:

fr:
  books:
    title: "Livres"

But is that the convention?

I am not specifying the names of any views in there, so I wonder if it's good practice.

Thanks for any input.

Was it helpful?

Solution

Say, You need to store translations for books model. You should be storing them under:

en.active_record.models.book

And, all the book attributes should go under:

en.active_record.attributes.book.title

Keeping translations for each view might not be a very good idea as:

1) As you have pointed out, It is not DRY.

2) Translations take much time to load up in memory. So, more of them would increase the server start time.

OTHER TIPS

I generally like to add a shared section for views so you can still utilize the shorthand.

fr:
 books:
   shared: &shared
    title: Livres
    thing_with_variable: "%{count} things"
   index:
    <<: *shared
    thing: thing
   new:
    <<: *shared
    other_thing: other thing
   edit:
    <<: *shared

Then in the index, new, etc.. views you can use the shorthand all the same.

<%= translate('.title') %>
<%= translate('.thing_with_variable', count: 2) %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top