Prev | Current Page 380 | Next

Brad Ediger

"Advanced Rails"

The traditional software package used for
localizing interface text is GNU gettext.*
gettext
gettext uses literal strings from the program??™s source as keys; translators write files
that provide translations for each of the strings. There are several steps to using gettext
in an application. We will use Ruby-Gettext, which is a mostly compatible Ruby
version of GNU gettext.
First, we install the gettext gem:
$ sudo gem install gettext
Successfully installed gettext-1.10.0
Next, we create a very basic skeleton application that loads the gettext gem, binds to
the text domain (application name) hello, and displays a greeting:
hello.rb
#!/usr/local/bin/ruby -w
require 'rubygems'
require 'gettext'
include GetText
bindtextdomain('hello')
puts _("Hello, world!")
* http://www.gnu.org/software/gettext/
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 without
having to do any localization:
$ echo $LC_CTYPE
en_US.UTF-8
$ ./hello.rb
Hello, world!
The developer now creates a .pot file 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 .


Pages:
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392