Prev | Current Page 386 | Next

Brad Ediger

"Advanced Rails"

If you omit the
string key, one will be generated for you:
lang/es.yml
hello_world: ??Hola, mundo!
hello.rb
Gibberish.use_language :es
puts "Hello, world!"[:hello_world]
# >> ??Hola, mundo!
puts "Hello, world!"[]
# >> ??Hola, mundo!
This method is resilient to small changes (capitalization, punctuation, and anything
else that is removed when folding the string into a symbol), but if this feature is overused,
you will run into the same fragility problems that gettext presents.
Globalize
For those with heavy localization needs, the Globalize plugin (http://www.globalizerails.
org/) is the best thing since sliced bread. We will first examine the features of
Rails L10n | 255
Globalize that compete with gettext, Gibberish, and the other text-based localization
libraries commonly used with Rails. Later, we will examine some of the other
features that make Globalize so compelling.
As is the custom, Globalize provides a simple method to access translations of a
string; it can be called as String#t or Object#_ (the latter is provided for gettext
compatibility):
<% Locale.set("es-MX") %>
<%=h "Hello, World!".t %>
==> Hello, World!
<%=h _("Hello, World!") %>
==> Hello, World!
However, there is a twist. Unlike gettext and Gibberish, which both use text files to
store translations, Globalize uses the database. Since we had no Mexican Spanish
translation for ???Hello, World!???, Globalize passed it through, storing the ???Hello,
World!??? tag in the database for future reference when you need to translate.


Pages:
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398