Class: R18n::Loader::YAML
- Inherits:
-
Object
- Object
- R18n::Loader::YAML
- Defined in:
- lib/r18n-core/yaml_loader.rb
Overview
Loader for translations in YAML format. Them should have name like en.yml
(English) or en-US.yml (USA English dialect) with language/country code (RFC 3066).
R18n::I18n.new('en', R18n::Loader::YAML.new('dir/with/translations'))
YAML loader is default loader, so you can just set constructor parameter to R18n::I18n.new
:
R18n::I18n.new('en', 'dir/with/translations')
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Dir with translations.
Instance Method Summary collapse
-
#==(loader) ⇒ Object
Is another
loader
load YAML translations from samedir
. -
#available ⇒ Object
Array of locales, which has translations in
dir
. -
#hash ⇒ Object
YAML loader with same
dir
will be have samehash
. -
#initialize(dir) ⇒ YAML
constructor
Create new loader for
dir
with YAML translations. -
#load(locale) ⇒ Object
Return Hash with translations for
locale
. -
#transform(a_hash) ⇒ Object
Wrap YAML private types to Typed.
Constructor Details
#initialize(dir) ⇒ YAML
Create new loader for dir
with YAML translations.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/r18n-core/yaml_loader.rb', line 41 def initialize(dir) @dir = File.(dir) @private_type_class = if defined?(JRUBY_VERSION) ::YAML::Yecht::PrivateType elsif '1.8.' == RUBY_VERSION[0..3] ::YAML::PrivateType else ::Syck::PrivateType end end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Dir with translations.
38 39 40 |
# File 'lib/r18n-core/yaml_loader.rb', line 38 def dir @dir end |
Instance Method Details
#==(loader) ⇒ Object
Is another loader
load YAML translations from same dir
.
85 86 87 |
# File 'lib/r18n-core/yaml_loader.rb', line 85 def ==(loader) self.class == loader.class and self.dir == loader.dir end |
#available ⇒ Object
Array of locales, which has translations in dir
.
53 54 55 56 57 |
# File 'lib/r18n-core/yaml_loader.rb', line 53 def available Dir.glob(File.join(@dir, '**/*.yml')). map { |i| File.basename(i, '.yml') }.uniq. map { |i| R18n::Locale.load(i) } end |
#hash ⇒ Object
YAML loader with same dir
will be have same hash
.
80 81 82 |
# File 'lib/r18n-core/yaml_loader.rb', line 80 def hash self.class.hash + @dir.hash end |
#load(locale) ⇒ Object
Return Hash with translations for locale
.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/r18n-core/yaml_loader.rb', line 60 def load(locale) if '1.8.' != RUBY_VERSION[0..3] and 'psych' == ::YAML::ENGINE.yamler Filters.by_type.keys.each do |type| next unless type.is_a? String # Yeah, I add R18n’s types to global, send me patch if you really # use YAML types too ;). Psych.add_domain_type('yaml.org,2002', type) do |full_type, value| Typed.new(type, value) end end end translations = {} Dir.glob(File.join(@dir, "**/#{locale.code.downcase}.yml")).each do |i| Utils.deep_merge!(translations, ::YAML::load_file(i) || {}) end transform(translations) end |
#transform(a_hash) ⇒ Object
Wrap YAML private types to Typed.
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/r18n-core/yaml_loader.rb', line 90 def transform(a_hash) R18n::Utils.hash_map(a_hash) do |key, value| [key, case value when @private_type_class Typed.new(value.type_id, value.value) when Hash transform(value) else value end] end end |