3 Yet another localization library. Maybe with the most agreeable API?
7 It's simple. Your default language, by default, is English (:en).
12 Gibberish looks in RAILS_ROOT/lang/*.yml for translation files. Say you have RAILS_ROOT/lang/es.yml,
13 right? Gibberish will detect that you know about the :es language and will serve up translations
14 defined in that file if requested to do so.
16 Here's a real simple example file (it's just "key: translation"):
21 And, as follows, a real simple example session:
25 >> Gibberish.current_language
27 >> Gibberish.current_language = :es
31 >> Gibberish.current_language = nil
36 It even works with simple interpolation:
38 >> "Hey, {name}!"[:hey_name, 'Chris']
40 >> "{name} is from {place}"[:hey_place, 'Chris', 'the Dreamworld']
41 => "Chris is from the Dreamworld"
43 Notice we don't use hashes (#) like normal Ruby interpolation. Also, the names of the variables
44 in the brackets don't really mean much. Interpolation is done in order -- the first argument replaces
45 the first variable in brackets, the second the second, etc.
47 This of course works with your translations:
51 hey_name: ¡Hola {name}!
53 >> "Hey, {name}!"[:hey_name, 'Chris']
55 >> Gibberish.current_language = :es
57 >> "Hey, {name}!"[:hey_name, 'Cristóbal']
60 Neat. What other methods do we get?
62 The classic around_filter:
64 class ApplicationController < ActionController::Base
65 around_filter :set_language
69 Gibberish.use_language(session[:language]) { yield }
73 For the duration of the block, :es is set as the language of choice. After the block is run everything
74 returns to normal. Rad.
76 Finally, some checking methods, if you need them:
78 >> Gibberish.default_language?
80 >> Gibberish.current_language = :es
82 >> Gibberish.current_language
84 >> Gibberish.default_language?
87 Languages are loaded by default at Rails startup. In dev mode, language YAML files are reloaded when
88 modified. No need to reboot the server.
90 >> Gibberish.load_languages!
91 => [:es, :fr, :de, :kl]
92 >> Gibberish.languages
93 => [:es, :fr, :de, :kl]
99 By default, Ruby returns nil when a symbol is passed to String's [] method. Some of Rails, it seems, depends
100 on this behavior. Yes, I am changing !!core Ruby behavior!! The humanity!
102 To deal with this assumption, Gibberish has a reserved_keys array. It, by default, contains :limit (so Rails
103 migrations don't explode on you.) To add to this array, just pass it more keys:
105 >> Gibberish.add_reserved_key :another_key
106 => [:limit, :another_key]
107 >> Gibberish.add_reserved_keys :more, :keys
108 => [:limit, :another_key, :more, :keys]
110 You've been warned. It really shouldn't affect you, though.
113 => chris[at]ozmm[dot]org