Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1

252 | Chapter 8: i18n and L10n


[email protected]

(...)
Creado hello.po.

Now that thehello.pofile is created, the translator will work through it and add
translations for each string:


locale/es/hello.po


Spanish translations for PACKAGE package.


Copyright (C) 2007 THE PACKAGE'S COPYRIGHT HOLDER


This file is distributed under the same license as the PACKAGE package.


Brad Ediger [email protected], 2007.



msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2007-10-19 12:20-0500\n"
"PO-Revision-Date: 2007-10-19 12:27-0500\n"
"Last-Translator: Brad Ediger [email protected]\n"
"Language-Team: Spanish\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"


#: hello.rb:9
msgid "Hello, world!"
msgstr "¡Hola, mundo!"

When all strings have been translated, the translator ships off the.pofiles to the
developer. The developer then creates.mofiles (binary translations of the.pofiles;
the binary versions are used directly by the application) usingrmsgfmtfrom Ruby-
gettext (ormsgfmtfrom GNU gettext). When doing local development, we set the
GETTEXT_PATHenvironment variable to look in ourlocaledirectory. If we didn’t do
that, we would have to install our.mofiles under a shared location such as/usr/
share/locale so that Ruby-gettext could find them at runtime.


$ export GETTEXT_PATH=locale
$ mkdir locale/es/LC_MESSAGES
$ rmsgfmt locale/es/hello.po -o locale/es/LC_MESSAGES/hello.mo

When we run the script with our locale set toes_MX.UTF-8, gettext searches a series
of paths underGETTEXT_PATH(includinglocale/es/LC_MESSAGES) for an appropri-
ate.mo-file. Finding the one we just created, it displays the properly localized text:


$ LC_ALL=es_MX.UTF-8 ./hello.rb
¡Hola, mundo!

This is a very simple example, but it illustrates many of the basic aspects of gettext.
There is a document available detailing how to integrate the Ruby bindings to gettext
into a Rails application. Seehttp://manuals.rubyonrails.com/read/chapter/105for the
full explanation and source files. This is essentially a do-it-yourself approach; it is a
thin layer on top of gettext, and you have to fill in some of the gaps (such as allowing

Free download pdf