Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails strategies for internationalization of large amounts of text (and some html)

Tags:

I'm creating a website that will be in English, Chinese and Korean. There are large portions of layout and text on certain pages, for example the "About" page. This page will have many headers, many paragraphs of text, and some layout.

What's the recommended way to internationalize my website? All the examples I've seen are for short bits of text or text for buttons/links and the like.

Is my only option to have a ton of key/value pairs in my locales yaml files? Or is there a better way of doing this? Currently I have a key/value in my yaml with the key ending in _html so I can have html in the key but it all has to be on ONE line so it's quite ugly, hard to maintain and error prone.

like image 966
Dty Avatar asked Feb 07 '12 07:02

Dty


People also ask

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.

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 i18n and l10n?

Internationalization (i18n) is the process of developing products in such a way that they can be localized for languages and cultures easily. Localization (l10n), is the process of adapting applications and text to enable their usability in a particular cultural or linguistic market.

Why is internationalization called i18n?

Internationalization (i18n) Example Internationalization is also called i18n (because of the number of letters, 18, between “i” and “n”). Internationalization ensures your software is localizable and is typically done by software developers and engineers.


1 Answers

If you would like to localize large chunks of code and content, you may simply use partials. Partials respect the I18n conventions, as do views and templates.

# app/views/about.html.erb <%= render :partial => 'about_contents' %>  # app/views/_about_contents.en.html.erb <h1>About us</h1> <p>Some large content...</p>  # app/views/_about_contents.fr.html.erb <h1>A propos</h1> <p>Un contenu quelconque...</p> 

For labels, small texts, date formats, etc. you could keep on using I18n.t / locale files.

UPDATE :

If the content to be localized contains formatting code, you may also use content_for to yield the text contents from your partial and avoid code duplication for layout and shared markups.

# app/views/about.html.erb <%= render :partial => 'about_contents' %> <div class="whatever">   <p><%= yield :content_one %></p> </div> </div class="whatever_two">   <p><%= yield :content_two %></p> </div>  # app/views/_about_contents.en.html.erb <% content_for :content_one do %> Some large content... <% end %> <% content_for :content_two do %> Some other large content... <% end %>  # app/views/_about_contents.fr.html.erb <% content_for :content_one do %> Un contenu quelconque... <% end %> <% content_for :content_two do %> Un autre contenu quelconque... <% end %> 
like image 85
Jef Avatar answered Oct 16 '22 18:10

Jef