user2148956
user2148956

Reputation: 179

I18n in rails 3.2: scoping or lazy lookup for translations within controllers

I try to retrieve some translations in my controllers but the scope will be ignored. For example:

In UsersController in create action

t(".notice")

will look for 'notice' at the very first level (i.e. en -> notice) an not at en -> users -> create -> notice. How can I "activate" the scope so that I don't have to enter

t("users.create.notice") 

everytime.

And why scoping of lazy lookup is activated for views by default but not for controllers?

Using rails 3.2

Upvotes: 1

Views: 1398

Answers (1)

Paul Fioravanti
Paul Fioravanti

Reputation: 16793

Unfortunately, you're out of luck unless you upgrade to Rails 4.

The Rails i18n guide section 3.2.13 on "lazy" lookups says:

Rails implements a convenient way to look up the locale inside views

(Emphasis theirs; lazy lookup functionality is restricted only to views in Rails 3.2)

This is the commit to Rails 4 that brought "lazy" lookups into controllers as well.

The latest Rails i18n guide as of this writing states that lazy lookups are view-only in Rails 4, but it is not correct anymore as you would be able to use t(".notice") in your UsersController#create action and get back the expected value.

Upvotes: 2

Related Questions