views:

150

answers:

3

Using Ruby on Rails 3.0, I would like to have pages where posts have side-by-side translations. For example, a page would look like this

+---------------+---------------+-----------------+
| Hello         | Hola          | Bonjour         |
+---------------+---------------+-----------------+ ... (there could be many more languages)
| Hello, world. | Hola, mundo.  | Bonjour, monde. |
+---------------+---------------+-----------------+

What's an easy way to go about this? I don't believe this can be solved with I18N since that usually means setting one locale and translating an entire page to that language. I want multiple columns of languages (these languages and how many there are is also unknown). I know I could create lots of tables for translations like Posts, PostTitleTranslations, and PostBodyTranslations, but that doesn't seem like the best way to do it. Has anyone ever solved anything like this before?

A: 

You can override the locale in which to display a translation using the :locale option to I18n.t:

I18n.locale = :de             # set default locale to German
I18n.t('key')                 # display German translation for 'key'
I18n.t('key', :locale => :de) # display English translation for 'key' even though
                              # the default locale is still set to German

Knowing this, it should be easy to set up a table like the one in your example.

Andreas
Should it be I18n.t('key'), :locale => :en) ?
Hugo
:locale is an option for the I18n.t method
Andreas
A: 

As I understood you want to translate table's rows. You can do it with using simplest, and yet best practice I've found and used in 3 projects. Add PostTranslation model, with your fields, like title, text, etc. and also with the special columns: post_id (in your example) and locale.

It could be done using puret (which is supports rails3 from it's beginning) or with using plugin developed by me has_translations (right now I've tested it only on rails 2.3, but next which I will release in very sort time (version 0.4) will support rails3).

It's very easy to use. You can read README files straight forward by clicking the links.

If you will have any question, just write me a message on a github.

Dmitry Polushkin
A: 

For the sake of completeness

You need files for each locale:

config
|
+- locales
   |
   +- es.yml
      en.yml
      fr.yml

Example using es.yml

es:
  :hello "Hola"
  :world "mundo"

Like Andreas said before, using t('something'), :locale => :xx you can pick and choose what you want.

<p>
  <%= t('hello', :locale => :es) %>
  <br />
  <%= "#{t('hello', :locale => :es)}, #{t('world', :locale => :es)}.
</p>
Hugo