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.