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

(Tuis.) #1

250 | Chapter 8: i18n and L10n


>>> document.characterSet
"UTF-8"

>>> escape("café")
"caf%E9"

>>> encodeURI("café")
"caf%C3%A9"

Rails L10n


For an application to be truly ready for worldwide visitors, internationalization is just
the beginning. It is vital for an application with global reach to correctly accept, pro-
cess, and store UTF-8 data. But it is also important, when supporting users from differ-
ent regions and locales, to localize the interface and any applicable data to the users’
locales. This can involve any of several things, which we will cover in this section.


Interface/Resource Translation


The way the term “localization” is most often used, it refers to translating interface
text and resources into users’ languages. 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 get-
text 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/

Free download pdf