Class: Inochi::Phrases
- Inherits:
-
Object
- Object
- Inochi::Phrases
- Defined in:
- lib/inochi/init.rb
Overview
Interface to translations of human text used in a project.
Instance Attribute Summary collapse
-
#locale ⇒ Object
The locale into which the #[] method will translate phrases.
Instance Method Summary collapse
-
#[](phrase, *words) ⇒ Object
Translates the given phrase into the target locale (see #locale and #locale=) and then substitutes the given placeholder arguments into the translation (see Kernel#sprintf).
-
#initialize(project_install_dir) ⇒ Phrases
constructor
A new instance of Phrases.
-
#method_missing(meth, *args) ⇒ Object
Provides access to translations in any language, regardless of the target locale (see #locale and #locale=).
-
#phrases ⇒ Object
Returns all phrases that underwent (or attempted) translation via this object.
Constructor Details
#initialize(project_install_dir) ⇒ Phrases
Returns a new instance of Phrases.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/inochi/init.rb', line 175 def initialize project_install_dir # load language translations dynamically lang_dir = File.join(project_install_dir, 'lang') @phrases_by_language = Hash.new do |cache, language| # store the phrase upon failure so that # the phrases() method will know about it phrases = Hash.new {|h,k| h[k] = nil } lang_file = File.join(lang_dir, "#{language}.yaml") lang_data = YAML.load_file(lang_file) rescue nil phrases.merge! lang_data if lang_data cache[language] = phrases end # detect user's preferred locale self.locale = ENV['LC_ALL'] || ENV['LC_MESSAGES'] || ENV['LANG'] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
Provides access to translations in any language, regardless of the target locale (see #locale and #locale=).
For example, you can access Japanese translations via the #jp method even if the target locale is French.
234 235 236 237 238 239 240 241 |
# File 'lib/inochi/init.rb', line 234 def method_missing meth, *args # ISO 639 language codes come in alpha-2 and alpha-3 forms if meth.to_s =~ /^[a-z]{2,3}$/ translate meth, *args else super end end |
Instance Attribute Details
#locale ⇒ Object
The locale into which the #[] method will translate phrases.
196 197 198 |
# File 'lib/inochi/init.rb', line 196 def locale @locale end |
Instance Method Details
#[](phrase, *words) ⇒ Object
Translates the given phrase into the target locale (see #locale and #locale=) and then substitutes the given placeholder arguments into the translation (see Kernel#sprintf).
If a translation is not available for the given phrase, then the given phrase will be used as-is, untranslated.
223 224 225 |
# File 'lib/inochi/init.rb', line 223 def [] phrase, *words translate @language, phrase, *words end |
#phrases ⇒ Object
Returns all phrases that underwent (or attempted) translation via this object.
210 211 212 |
# File 'lib/inochi/init.rb', line 210 def phrases @phrases_by_language.values.map {|h| h.keys }.flatten.uniq.sort end |