Rails has an internationalization system built into it.

Your best source is the official docs, but we describe it here, too.

The View

You should have some text in your view, whether it’s HAML or ERB or another format.

%p Hello world

In your view, use the I18n.t method (short for I18n.translate) to mark phrases or keys which should be translated:

%p= I18n.t(:hello_world)

The Config Files

Rails is looking up your keys in config/locales/en.yml

You can make a simple English YAML file for the “hello world” translation like this:

en:
  hello_world: "Hello world"
  good_bye: "good bye"

You can make another language file, such as config/locales/es.yml, looking like this:

es:
  hello_world: "Hola mundo"
  good_bye: "adios"

The application will use the value in the English form, unless you set another default language:

I18n.default_locale = :es

Interpolation

For a customized greeting such as “Your name is -Nick-“ your first instinct might be to write this code:

greeting = I18n.t(:your_name_is) + user.name # not good

en:
  your_name_is: 'Your name is '

In many languages, the order of the subject, verb, and object in a sentence is different from the order in English. To translate phrases and sentences, it’s better for you to pass the original phrase and the value to I18n:

greeting = I18n.t(:your_name_is, name: user.name)

en:
  your_name_is: "Your name is %{name}" </code.

In other languages, such as Japanese, the order can be different.

ja:
  your_name_is: "なまえが%{name}です。"

Plurals

In many languages, plurals work differently from English. For example, we would say “there are zero apples” but other languages may use the singular “apple” for zero, or may have different plurals for two or one hundred.

how_many_apples = I18n.t(:apple_count, number: apples.length)

en:
  apple_count:     zero: "You have no apples."     one: "You have one apple."     other: "You have #{count} apples."

For languages where Rails does not handle pluralization well, you can create custom rules in a file named config/initializers/pluralization.rb or config/locales/plurals.rb.

The Controller

To set the language, you can review the Rails reference for using URL parameters, subdomains, and other hints to set the language.