Class: Babelfish
- Inherits:
-
Object
- Object
- Babelfish
- Defined in:
- lib/babelfish.rb,
lib/babelfish/version.rb,
lib/babelfish/phrase/node.rb,
lib/babelfish/phrase/parser.rb,
lib/babelfish/phrase/literal.rb,
lib/babelfish/phrase/compiler.rb,
lib/babelfish/phrase/variable.rb,
lib/babelfish/phrase/pluralizer.rb,
lib/babelfish/phrase/parser_base.rb,
lib/babelfish/phrase/plural_forms.rb,
lib/babelfish/phrase/string_to_compile.rb,
lib/babelfish/phrase/plural_forms_parser.rb
Defined Under Namespace
Modules: Phrase
Constant Summary collapse
- VERSION =
'1.0.1'.freeze
Instance Attribute Summary collapse
-
#_cfg ⇒ Object
Returns the value of attribute _cfg.
-
#compiler ⇒ Object
Returns the value of attribute compiler.
-
#default_locale ⇒ Object
Returns the value of attribute default_locale.
-
#dictionaries ⇒ Object
Returns the value of attribute dictionaries.
-
#dirs ⇒ Object
Returns the value of attribute dirs.
-
#fallback_cache ⇒ Object
Returns the value of attribute fallback_cache.
-
#fallbacks ⇒ Object
Returns the value of attribute fallbacks.
-
#file_filter ⇒ Object
Returns the value of attribute file_filter.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#suffix ⇒ Object
Returns the value of attribute suffix.
-
#watch ⇒ Object
Returns the value of attribute watch.
-
#watchers ⇒ Object
Returns the value of attribute watchers.
Instance Method Summary collapse
- #_built_config(cfg) ⇒ Object
- #_flat_hash_keys(hash, prefix, store) ⇒ Object
- #_load_dictionary(dictname, lang, file) ⇒ Object
- #addPhrase(locale, phrase, translation, flatten_level = Float::INFINITY) ⇒ Object
- #detect_locale(locale) ⇒ Object
- #has_any_value(dictname_key, custom_locale = nil) ⇒ Object
-
#initialize(cfg = {}) ⇒ Babelfish
constructor
A new instance of Babelfish.
- #load_dictionaries(filter) ⇒ Object
- #locale ⇒ Object
- #locale=(new_locale) ⇒ Object
- #look_for_watchers ⇒ Object
- #on_watcher_change ⇒ Object
- #phrase_need_compilation(phrase, key) ⇒ Object
- #prepare_to_compile ⇒ Object
- #set_fallback(locale, fallback_locales) ⇒ Object
- #t(dictname_key, params = nil, custom_locale = nil) ⇒ Object
- #t_or_undef(dictname_key, params = nil, custom_locale = nil) ⇒ Object
- #watch? ⇒ Boolean
Constructor Details
#initialize(cfg = {}) ⇒ Babelfish
Returns a new instance of Babelfish.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/babelfish.rb', line 29 def initialize(cfg = {}) cfg = { _cfg: cfg, }.merge( _built_config(cfg) ) cfg.keys.each do |key| send("#{key}=", cfg[key]) if respond_to?("#{key}=") end self.parser = Babelfish::Phrase::Parser.new self.compiler = Babelfish::Phrase::Compiler.new load_dictionaries( file_filter ) self.locale = default_locale end |
Instance Attribute Details
#_cfg ⇒ Object
Returns the value of attribute _cfg.
14 15 16 |
# File 'lib/babelfish.rb', line 14 def _cfg @_cfg end |
#compiler ⇒ Object
Returns the value of attribute compiler.
13 14 15 |
# File 'lib/babelfish.rb', line 13 def compiler @compiler end |
#default_locale ⇒ Object
Returns the value of attribute default_locale.
10 11 12 |
# File 'lib/babelfish.rb', line 10 def default_locale @default_locale end |
#dictionaries ⇒ Object
Returns the value of attribute dictionaries.
10 11 12 |
# File 'lib/babelfish.rb', line 10 def dictionaries @dictionaries end |
#dirs ⇒ Object
Returns the value of attribute dirs.
10 11 12 |
# File 'lib/babelfish.rb', line 10 def dirs @dirs end |
#fallback_cache ⇒ Object
Returns the value of attribute fallback_cache.
11 12 13 |
# File 'lib/babelfish.rb', line 11 def fallback_cache @fallback_cache end |
#fallbacks ⇒ Object
Returns the value of attribute fallbacks.
11 12 13 |
# File 'lib/babelfish.rb', line 11 def fallbacks @fallbacks end |
#file_filter ⇒ Object
Returns the value of attribute file_filter.
12 13 14 |
# File 'lib/babelfish.rb', line 12 def file_filter @file_filter end |
#parser ⇒ Object
Returns the value of attribute parser.
13 14 15 |
# File 'lib/babelfish.rb', line 13 def parser @parser end |
#suffix ⇒ Object
Returns the value of attribute suffix.
10 11 12 |
# File 'lib/babelfish.rb', line 10 def suffix @suffix end |
#watch ⇒ Object
Returns the value of attribute watch.
12 13 14 |
# File 'lib/babelfish.rb', line 12 def watch @watch end |
#watchers ⇒ Object
Returns the value of attribute watchers.
12 13 14 |
# File 'lib/babelfish.rb', line 12 def watchers @watchers end |
Instance Method Details
#_built_config(cfg) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/babelfish.rb', line 16 def _built_config(cfg) { dictionaries: {}, dirs: [ "./locales" ], fallbacks: {}, fallback_cache: {}, suffix: cfg[:suffix] || 'yml', default_locale: cfg[:default_locale] || 'en_US', watch: cfg[:watch] || 0, watchers: {}, }.merge( cfg || {} ) end |
#_flat_hash_keys(hash, prefix, store) ⇒ Object
249 250 251 252 253 254 255 256 257 258 |
# File 'lib/babelfish.rb', line 249 def _flat_hash_keys( hash, prefix, store ) hash.each_pair do | key, value | if value.kind_of?(Hash) _flat_hash_keys( value, "#{prefix}#{key}.", store ) else store["#{prefix}#{key}"] = value.kind_of?(Symbol) ? value.to_s : value end end return true end |
#_load_dictionary(dictname, lang, file) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/babelfish.rb', line 112 def _load_dictionary( dictname, lang, file ) dictionaries[lang] ||= {} yaml = YAML.load_file( file ) _flat_hash_keys( yaml, "#{dictname}.", dictionaries[lang] ) return unless watch? watchers[file] = File.mtime(file) end |
#addPhrase(locale, phrase, translation, flatten_level = Float::INFINITY) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/babelfish.rb', line 260 def addPhrase( locale, phrase, translation, flatten_level = Float::INFINITY) fl = Float::INFINITY case flatten_level when FalseClass fl = 0 when TrueClass fl = Float::INFINITY when FixNum, Float fl = flatten_level.to_i fl = 0 if fl < 0 else fl = Float::INFINITY; end if translation.kind_of?(Hash) && fl > 0 translation.each_pair do | key, val | addPhrase( locale, "#{phrase}.#{key}", val, fl - 1 ) end return end if phrase_need_compilation(translation) dictionaries[locale][phrase] = Babelfish::Phrase::StringToCompile.new(translation) else dictionaries[locale][phrase] = translation end self.fallback_cache = {} end |
#detect_locale(locale) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/babelfish.rb', line 70 def detect_locale(locale) return locale if dictionaries.has_key?(locale) alt_locale = dictionaries.keys.find { |loc| loc =~ /^#{Regexp.escape(locale)}[\-_]/i } if alt_locale && dictionaries.has_key?(alt_locale) # Lets locale dictionary will refer to alt locale dictinary. # This speeds up all subsequent calls of t/detect/exists on this locale. dictionaries[locale] = dictionaries[alt_locale] fallback_cache[locale] = fallback_cache[alt_locale] if fallback_cache.has_key?(alt_locale) fallbacks[locale] = fallbacks[alt_locale] if fallbacks.has_key?(alt_locale) return locale end return default_locale if dictionaries.has_key?(default_locale) raise "bad locale: #{locale} and bad default_locale: #{default_locale}." end |
#has_any_value(dictname_key, custom_locale = nil) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/babelfish.rb', line 216 def has_any_value( dictname_key, custom_locale = nil ) # disallow non-ASCII keys raise("wrong dictname_key: #{dictname_key}") if dictname_key =~ /\P{ASCII}/; look_for_watchers if watch? _locale = custom_locale ? detect_locale( custom_locale ) : self.locale return true if dictionaries[_locale].has_key?(dictname_key) fallback_cache[_locale] ||= {} return ! fallback_cache[_locale][dictname_key].nil? if fallback_cache[_locale].has_key?(dictname_key) fallback_locales = fallbacks[_locale] || [] return true if fallback_locales.find do |fallback| ! dictionaries[fallback][dictname_key].nil? end return false end |
#load_dictionaries(filter) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/babelfish.rb', line 89 def load_dictionaries(filter) dirs.each do |dir| fdir = File.absolute_path( dir ) Find.find(dir) do |file| file_path = File.absolute_path(file) next unless FileTest.file? file_path return if filter && !filter( file_path ) directories, base = File.split( file_path ) tmp = base.split('.') cur_suffix = tmp.pop return if cur_suffix != suffix locale = tmp.pop dictname = tmp.join('.') subdir = directories if subdir =~ /^#{Regexp.escape(fdir)}[\\\/](.+)$/ dictname = "#{$1}#{dictname}" end _load_dictionary( dictname, locale, file ) end end prepare_to_compile end |
#locale ⇒ Object
45 46 47 |
# File 'lib/babelfish.rb', line 45 def locale @locale end |
#locale=(new_locale) ⇒ Object
49 50 51 |
# File 'lib/babelfish.rb', line 49 def locale=(new_locale) @locale = detect_locale( new_locale ) end |
#look_for_watchers ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/babelfish.rb', line 140 def look_for_watchers ok = true watchers.each_pair do | file, mtime | new_mtime = File.mtime(file) if mtime.nil? || new_mtime.nil? || new_mtime != mtime ok = false break end end return if ok; on_watcher_change end |
#on_watcher_change ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/babelfish.rb', line 127 def on_watcher_change _cfg.keys.each do |key| send("#{key}=", nil) if respond_to?("#{key}=") end new_cfg = _built_config( _cfg ) new_cfg.keys.each do |key| send("#{key}=", new_cfg[key]) if respond_to?("#{key}=") end load_dictionaries self.locale = default_locale end |
#phrase_need_compilation(phrase, key) ⇒ Object
122 123 124 125 |
# File 'lib/babelfish.rb', line 122 def phrase_need_compilation( phrase, key ) raise "L10N: #{key} is undef" if phrase.nil? return phrase.kind_of?(String) && phrase =~ /(?:\(\(|\#\{|\\\\)/ end |
#prepare_to_compile ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/babelfish.rb', line 57 def prepare_to_compile dictionaries.each_pair do |locale, dic| dic.each_pair do |key, value| if phrase_need_compilation( value, key ) dic[key] = Babelfish::Phrase::StringToCompile.new(value) # lazy compile #dic[key] = compiler.compile( parser.parse(value, locale) ); end end end true end |
#set_fallback(locale, fallback_locales) ⇒ Object
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/babelfish.rb', line 238 def set_fallback( locale, fallback_locales ) return unless fallback_locales && fallback_locales.size _locale = detect_locale( locale ) fallbacks[_locale] = fallback_locales fallback_cache.delete(_locale) return true end |
#t(dictname_key, params = nil, custom_locale = nil) ⇒ Object
212 213 214 |
# File 'lib/babelfish.rb', line 212 def t( dictname_key, params = nil, custom_locale = nil ) t_or_undef( dictname_key, params, custom_locale ) || "[#{dictname_key}]"; end |
#t_or_undef(dictname_key, params = nil, custom_locale = nil) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/babelfish.rb', line 153 def t_or_undef( dictname_key, params = nil, custom_locale = nil ) # disallow non-ASCII keys raise("wrong dictname_key: #{dictname_key}") if dictname_key =~ /\P{ASCII}/; look_for_watchers if watch? _locale = custom_locale ? detect_locale( custom_locale ) : self.locale r = dictionaries[_locale][dictname_key] unless r.nil? if r.kind_of?(Babelfish::Phrase::StringToCompile) dictionaries[_locale][dictname_key] = r = compiler.compile( parser.parse( r, _locale ), ) end # fallbacks else fallback_cache[_locale] ||= {} # Cache can contain undef, as unexistent value. if fallback_cache[_locale].has_key?(dictname_key) r = fallback_cache[_locale][dictname_key] else fallback_locales = fallbacks[_locale] || [] fallback_locales.each do |fallback| r = dictionaries[fallback][dictname_key] unless r.nil? if r.kind_of?(Babelfish::Phrase::StringToCompile) dictionaries[fallback][dictname_key] = r = compiler.compile( parser.parse( r, fallback ), ) end break end end fallback_cache[_locale][dictname_key] = r; end end if r.kind_of?(Proc) flat_params = {} # Convert parameters hash to flat form like "key.subkey" unless params.nil? # Scalar interpreted as { count => scalar, value => scalar }. unless params.kind_of?(Hash) flat_params = { 'count' => params, 'value' => params, } else _flat_hash_keys( params, '', flat_params ) end end return r.call( flat_params ); end return r end |
#watch? ⇒ Boolean
53 54 55 |
# File 'lib/babelfish.rb', line 53 def watch? !! watch end |