If String#[] is passed non-hash arguments, the second and
subsequent arguments are interpolated into the string, in order:
puts "Date of birth: {dob} ({age} years old)"[:dob, u.dob, u.age]
# >> Date of birth: 1/1/95 (12 years old)
* http://require.errtheblog.com/plugins/browser/gibberish/README
254 | Chapter 8: i18n and L10n
Alternatively, the second argument to String#[] can be a hash. The keys correspond
to the names of the interpolation variables, while the values provide the text to be
inserted. The following code is equivalent, but more descriptive and more resilient to
change in the translated text:
puts "Date of birth: {dob} ({age} years old)"[:dob, {:dob => u.dob, :age => u.age}]
# >> Date of birth: 1/1/95 (12 years old)
This example highlights one common problem with localization: it is never straight
translation of text. In most nontrivial situations, the translated text has some level of
dependence on the data that is more complicated than simple string interpolation
(???{age} years???). Later in this chapter, we will see how the Globalize Rails plugin handles
these situations.
Gibberish provides good Rails integration. You can automatically set a user??™s language
from a session variable with a simple around filter:
class ApplicationController < ActionController::Base
around_filter :set_language
private
def set_language
Gibberish.use_language(session[:language]) { yield }
end
end
Despite its simplicity, Gibberish has one more trick up its sleeve.
Pages:
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397