I18n in Rails 3 with admin namespace

I am developing a Rails application on the recommendation of the Rails Guides to create a tree of folders and files with translations. My folder tree looks like this:

|-defaults |---es.rb |---en.rb |-models |---book |-----es.rb |-----en.rb |-views |---defaults |-----es.rb |-----en.rb |---books |-----es.rb |-----en.rb |---users |-----es.rb |-----en.rb |---navigation |-----es.rb |-----en.rb 

The content in config / locales / views / books / en.yml looks like this:

 es: books: index: title: "Título" 

A inside the application / views / books / template.html.erb template, like this (note the point):

 <%= t '.title' %> 

When I don't have a namespace, my translations in the views work fine, but with the “admin” namespace that I use in my backend, this does not work. Does anyone know what the problem is?

+6
source share
3 answers

I use <%= t '.title' %> because I use "Lazy" Lookup http://guides.rubyonrails.org/i18n.html#lazy-lookup .

Rails implements a convenient way to find locales within views. When you have the following dictionary:

 es: books: index: title: "Título" 

you can find the value of books.index.title inside the app / views / books / index.html.erb template (note the point):

 <%= t '.title' %> 
+2
source

You just need to include the namespace in yaml:

 es: admin: books: index: title: "Título" 

You will then access the line with <%= t '.title' %> in your view. I just tested this with Rails 4.0.x

0
source

You only need to use the scope operator: as described here http://guides.rubyonrails.org/i18n.html#looking-up-translations

(I'm not sure why you use <%= t '.title' %> with a dot - you should either use a string that represents the field name ( <%= t 'title' %> ) or a character ( <%= t :title %> ).)

-1
source

Source: https://habr.com/ru/post/910730/


All Articles