Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
Rails L10n | 265

Now, we need to modify the views to use theString#tmethod, so that the views can
be translated. This can be a tedious process; here is one resulting view:


app/views/people/_form.html.erb




<%= @person_form.text_field :first_name %>


<p>
<label for="person_last_name"><%= "Last name".t %></label>
<%= @person_form.text_field :last_name %>
</p>

<p>
<label for="person_home_phone"><%= "Home phone".t %></label>
<%= @person_form.text_field :home_phone %>
</p>

<p>
<label for="person_office_phone"><%= "Office phone".t %></label>
<%= @person_form.text_field :office_phone %>
</p>

<p>
<label for="person_mobile_phone"><%= "Mobile phone".t %></label>
<%= @person_form.text_field :mobile_phone %>
</p>

<p>
<label for="person_address"><%= "Address".t %></label>
<%= @person_form.text_area :address, :size => '30x5' %>
</p>

<p>
<label for="person_country"><%= "Country".t %></label>
<%= @person_form.text_field :country %>
</p>

Next, we need to integrate Globalize into the controllers. We will use an initializer to
set up Globalize and some locale settings for the application.


config/initializers/globalize.rb
include Globalize
Locale.set_base_language 'en-US'
LOCALES = {
'en' => 'en-US',
'es' => 'es-MX'
}.freeze


TheLocale.set_base_languagemethod tells Globalize that our views are in U.S.
English, and it need not bother with translation if the user’s locale is the same.

Free download pdf