I started populating an en yaml file in Rails and I can already tell it will get messy and out of hand before too long. Is there a convention to keeping this file organized?
It's almost two years after I asked this question, and I want to share some insights. I believe the optimal structure is to namespace translations according to their MVC role (models, views, controllers). This keeps the locale file tidy, and prevents namespace collisions (for example, the scope en.users
can represent a view or a 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 tidy namespaces like that breaks the lazy lookup feature in Rails. If you use lazy lookup, Rails will insert the namespace automatically for you, and it will not include the top level namespaces you created (views
, controllers
, etc...).
For example, the scope of t('.welcome_flash')
resolves to en.users.show
. Which stinks because users isn't clearly defined. What is it? A controller? A view? Something else?
To solve this problem I created the gem I18nLazyLookup. It allows you to use lazy lookup with your own custom namespaces.
Instead of using t
, you can use t_scoped('welcome_flash')
, and that would automatically resolve the scope to en.controllers.users.show
. It also works for views and mailers, and you can customise the namespace the way you like.