Skip to content
Skye Shaw edited this page Jun 12, 2015 · 5 revisions

Gettext is different than the typical I18n usage as it does not require creating aliases (keys) for your translations.

Rails

See gettext_i18n_rails

Standalone

Translation Files

Only PO files are required. I18n has its own PO file parser. The filename(s) must be named after the locale, e.g., en.po, pt.po, etc...

Here's a very basic PO file, pt.po:

# Some optional headers
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"

msgid "bye"
msgstr "tchau"

msgid "bunny rabbit adventure"
msgstr "a aventura da coelhinha" 

msgid "time for bed!"
msgstr "nana nenê!"

For more info on the format see the PO File section of the gettext manual. Also checkout Wikipedia's gettext page.

I18n Code

I18n::Backend::Simple.include(I18n::Backend::Gettext)
I18n.load_path << Dir["*.po"]  # Load all PO file in current directory
I18n.locale = :pt 
puts I18n.t "bye" # tchau
puts I18n.t "bunny rabbit adventure" # a aventura da coelhinha
# Or 
puts I18n.t "bye", :locale => "pt" # tchau

To use the standard gettext functions instead of I18n:

include I18n::Gettext::Helpers
# Setup backends, etc...
puts _("time for bed!") # nana nenê!

For more info see I18n::Gettext::Helpers's docs.

Clone this wiki locally