3

Suppose we have a Book model with index, show, edit and new views. Suppose we want to internationalize and translate all of those views and use lazy lookup (automatic translation scoping) when passing the key names to the t method in the views.

The only solution I have found so far is to include multiple keys for the same attribute in the dictionary (YML file): one key for the model (which will translate the form labels) and one additional key for each view. Example:

#pt-BR.yml
pt-BR:
  activerecord:
    attributes:
      book:
        title: Título
  books:
    index:
      title: Título
    show:
      title: Título

Given the importance attributed to the DRY principle in the Rails community, I believe there is probably a way to avoid having multiple keys for the same attribute.

One possible alternative could be to include only the activerecord keys in the dictionary (only translate the model) but it seems like lazy lookup will not work in the views. We could do something like <%= t('activerecord.attributes.book.title') %> in the views, but it does not look like a proper solution as we have to repeat the entire scope (activerecord.attributes.book) every time we call the t method.

I appreciate if anyone can point me to a way to better/DRYer way to to this. Thank you.

BrunoF
  • 3,239
  • 26
  • 39
  • First create directory structure for views, controllers & models! And follow this documentation http://guides.rubyonrails.org/i18n.html – Muhammad Ans Jan 16 '17 at 13:56
  • 1
    I already have the views, controllers, and models in the default Rails directory structure. Also, I have read the Rails Guide on I18n but could not find the answer to this question. Thank you anyway. – BrunoF Jan 16 '17 at 15:28
  • Just found [this post](http://stackoverflow.com/questions/11097572/refactoring-ruby-on-rails-i18n-yaml-files-using-dictionaries) which contains a lot of relevant information. – BrunoF Jan 18 '17 at 12:36

0 Answers0