Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble on using the i18n gem with partial template files

I am using Ruby on Rails 3.1 and I would like to know how to correctly handle internationalization related to partial template files. That is, ...

... in my app/views/users/flag.html.erb file I have:

<%= t('.test_key1') %>
<%= render :partial => "/users/flag_form" %>

... in my app/views/users/_flag_form.html.erb file I have:

<%= t('.test_key2') %>

If in my config/locales/views/users/en.yml file (note: I am organizing files as stated in the official RoR guide) I use

en:
  users:
    flag:
      test_key1: Test 1 text
      test_key2: Test 2 text

the Test 1 text is displayed in the "main" template (app/views/users/flag.html.erb) but the Test 2 text isn't for the partial template (app/views/users/_flag_form.html.erb). How could\should I solve this issue so to properly display the Test 2 text?

like image 417
user12882 Avatar asked Jan 07 '12 05:01

user12882


People also ask

What is I18n t?

Internationalization (i18n) is the process of preparing software so that it can support local languages and cultural settings. An internationalized product supports the requirements of local markets around the world, functioning more appropriately based on local norms and better meeting in-country user expectations.

What is locale in Ruby?

Ruby-Locale is the pure ruby library which provides basic APIs for localization.

What is I18n 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.


2 Answers

config/locales/en.yml

en:
  users:
    flag:
      test_key1: Test 1 text
    flag_form:
      test_key2: Test 2 text

app/views/users/flag.html.erb

<%= t('.test_key1') %>
<%= render :partial => "/users/flag_form" %>

app/views/users/_flag_form.html.erb

<%= t('.test_key2') %>

NB:

  1. Rails path to the view must match YAML path to the symbol. You need to create an entry at YAML file that matches the name of the view. Omit the trailing underscore since it's a partial.
  2. Read more about lazy lookups
like image 83
Vadym Tyemirov Avatar answered Nov 15 '22 18:11

Vadym Tyemirov


One way would be to using scope, instead of "lazy loading" using the full stop. Something like this should work:

I18n.t :test_key2, :scope => 'users.flag'

or use:

I18n.t "users.flag.test_key2"

Lastly, you could even pass it to the partial as in

<%= render :partial => "/users/flag_form", :locals => { :test_key => t('.test_key1') } %>

You should also checkout the appendix section on this website as it might be listing something that I am missing: https://web.archive.org/web/20120619002316/http://www.unixgods.org/~tilo/Rails/where_is_Rails_trying_to_lookup_L10N_strings.html

like image 26
Wahaj Ali Avatar answered Nov 15 '22 19:11

Wahaj Ali