Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is lazy lookup in Rails I18n a bad practice?

Using shorter i18n keys (e.g. t '.submit_button') in Rails views makes them easier to type, but is it actually good? When later you decide to refactor your views and partials you have to remember to update the respective localization entries. Wouldn't it be more robust to name them by their business meaning and always specify the full key-name?

like image 824
RocketR Avatar asked Apr 17 '12 09:04

RocketR


People also ask

What is I18n in Rails?

The Ruby I18n (shorthand for internationalization) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or for providing multi-language support in your application.

What is lazy lookup?

Lazy lookup is a feature that ships with Rails and the I18n gem. It allows you to translate strings without explicitly qualifying their entire scope. For example, consider the following code that lives in show. html. erb inside app/views/users , and that your application's locale is English.


1 Answers

Well, I'm using a mixture. ;-)

For things like "yes", "no", "submit", "cancel" I tend to use a namespace called "defaults" so I always use it like t 'defaults.cancel'. That could also address the "submit_button" thing you mentioned above. For my specific views I decided to use the lazy lookup feature.

If you want I18n keys for specific views you have to decide what fits best for you:

  • If you don't mind searching your yaml file for the I18n keys and change them if you change the view do so. The advantage is that you save some characters for each I18n.t call of it in your view.

  • But if you change your view names very often (not sure why you should have to do so :) ) then you might be better of using the way you described.

As I already said I prefer the first option since it's more convenient for me.

like image 169
flooooo Avatar answered Sep 30 '22 13:09

flooooo