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

(Tuis.) #1

268 | Chapter 8: i18n and L10n


As a final touch, we will add a language bar that enables the user to switch back and
forth between our supported locales. We create a partial for the language bar, and
update the master layout to include the language bar at the bottom of each page:


app/views/layouts/_language_bar.html.erb



<%= LOCALES.keys.map { |code|
link_to_unless code == Locale.language_code, code, url_for(:locale => code)
}.join(" | ") %>


Usingurl_for( )in this manner relies on the fact that it will fill in any
missing required URL parameters from the URL of the current page.
Therefore,url_for(:locale => 'es')will point to the Spanish version
of the current page.

app/views/layouts/people.html.erb
(...)



<%= yield %>
<%= render :partial => "layouts/language_bar" %>

(...)

And with that, the localization is complete. All static resources have been translated
into Spanish, and we have added all necessary features to allow users to choose a
locale. See Figures 8-7 through 8-10 for screenshots of the completed application.


Figure 8-7. Spanish translation: blank state of an empty address book

Free download pdf