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

(Tuis.) #1
Rails L10n | 251

The_( )function is gettext’s standard method for localization. All literal text that is
to be localized should be wrapped in a call to this method. Our locale is set to U.S.
English, so upon running the program, we see the default U.S. English version with-
out having to do any localization:


$ echo $LC_CTYPE
en_US.UTF-8
$ ./hello.rb
Hello, world!

The developer now creates a.potfile from the source. This extracts all text to be
translated from the program and puts it in a template, which the translator will work
from. The GNU gettext program to create.pot-files isxgettext; the Ruby-gettext ver-
sion is calledrgettext.


$ rgettext hello.rb -o hello.pot

The resultinghello.potfile has several lines of boilerplate, followed by the extracted
strings for translation—in this case, only one:


hello.pot


SOME DESCRIPTIVE TITLE.


Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER


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


FIRST AUTHOR EMAIL@ADDRESS, YEAR.



#, fuzzy
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:20-0500\n"
"Last-Translator: FULL NAME EMAIL@ADDRESS\n"
"Language-Team: LANGUAGE [email protected]\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"


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

This file is ready to be translated. The translator receiveshello.potfrom the developer
and copies it into a directory corresponding to the destination locale. Ruby-gettextpro-
vides tools for this, but we will use the GNU gettextmsginit; the Ruby-gettext
documentation recommends using it if it is available.


$ mkdir -p locale/es
$ cd locale/es/
$ LANG=es_MX msginit -i ../../hello.pot -o hello.po
(...)
Couldn't find out about your email address.
Please enter your email address.
Free download pdf