1

I am using Ruby on Rails 3.2.2 and I have a strange problem. In my view files I use the following code:

# app/views/articles/show.html.erb
I18n.t('.page_title')

When I render the above view code in the browser I get the translation missing: en.page_title message. However, as you can see from the "missing" message, the translation doesn't refer to the articles.show.page_title YML statement... but it should do that! It seems that the “Lazy” Lookup doesn't work as expected.

Why that happens? Have you some idea about the issue and how to solve it?

Note: I just made a code refactoring renaming statements from translate to I18n.t and from localize to I18n.l...

Backo
  • 18,291
  • 27
  • 103
  • 170
  • Why is there a dot in *.page_title*? Also, I don't know why would Rails look for *articles.show.page_title* while you only specified *page_title*. – Samy Dindane Jun 01 '12 at 07:08
  • @Samy Dindane - It's [“Lazy” Lookup](http://guides.rubyonrails.org/i18n.html#looking-up-translations) (see the 4.1.4 paragraph). – Backo Jun 01 '12 at 07:11

1 Answers1

1

Try simply: t('.page_title') Then you should see another statement. But in fact, I18n searches in several places. You can add the translation and see for yourself:

en:
  page-title:

Or:

en:
  articles:
    show:
      page-title:
Mik
  • 4,117
  • 1
  • 25
  • 32
  • OK, it works by using `t('.page_title')`. But, I am very curious, *why the `I18n.t('.page_title')` statement doesn't work in view files?* – Backo Jun 01 '12 at 07:22
  • 1
    I think that's why: "Second, it’ll scope the key by the current partial if the key starts with a period. So if you call translate(".foo") from the people/index.html.erb template, you’ll actually be calling I18n.translate("people.index.foo")." From the translation helper method documentation: http://api.rubyonrails.org/classes/ActionView/Helpers/TranslationHelper.html#method-i-translate – Kulgar Jun 01 '12 at 07:29
  • 1
    I18n library itself does not expand a dot, but the helper method #t in views does and calls I18n.t with the full translation key. – Simon Perepelitsa Jun 01 '12 at 07:29
  • 1
    Thanks for the explanation. Unfortunately RailsGuides not mention about this. – Mik Jun 01 '12 at 07:39