Reputation: 12273
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.
Upvotes: 22
Views: 3196
Reputation: 5474
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 %>
Upvotes: 46