Class: R18n::Loader::YAML
- Inherits:
-
Object
- Object
- R18n::Loader::YAML
- Includes:
- YamlMethods
- 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')
Constant Summary collapse
- FILE_EXT =
'y{,a}ml'
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Dir with translations.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Is another ‘loader` load YAML translations from same `dir`.
-
#available ⇒ Object
‘Array` of locales, which has translations in `dir`.
-
#hash ⇒ Object
YAML loader with same ‘dir` will be have same `hash`.
-
#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.
Methods included from YamlMethods
#detect_yaml_private_type, #initialize_types
Constructor Details
#initialize(dir) ⇒ YAML
Create new loader for ‘dir` with YAML translations.
41 42 43 44 |
# File 'lib/r18n-core/yaml_loader.rb', line 41 def initialize(dir) @dir = File.(dir) detect_yaml_private_type 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
#==(other) ⇒ Object
Is another ‘loader` load YAML translations from same `dir`.
72 73 74 |
# File 'lib/r18n-core/yaml_loader.rb', line 72 def ==(other) self.class == other.class && dir == other.dir end |
#available ⇒ Object
‘Array` of locales, which has translations in `dir`.
47 48 49 50 51 |
# File 'lib/r18n-core/yaml_loader.rb', line 47 def available Dir.glob(File.join(@dir, "**/*.#{FILE_EXT}")) .map { |i| File.basename(i, '.*').downcase }.uniq .map { |i| R18n.locale(i) } end |
#hash ⇒ Object
YAML loader with same ‘dir` will be have same `hash`.
67 68 69 |
# File 'lib/r18n-core/yaml_loader.rb', line 67 def hash self.class.hash + @dir.hash end |
#load(locale) ⇒ Object
Return ‘Hash` with translations for `locale`.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/r18n-core/yaml_loader.rb', line 54 def load(locale) initialize_types translations = {} Dir.glob( File.join(@dir, "**/#{locale.code}.#{FILE_EXT}"), File::FNM_CASEFOLD ).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.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/r18n-core/yaml_loader.rb', line 77 def transform(a_hash) a_hash.transform_values do |value| if value.is_a? Hash value = transform(value) elsif defined?(@private_type_class) && value.is_a?(@private_type_class) v = value.value if v.respond_to?(:force_encoding) && v.encoding != __ENCODING__ v = v.force_encoding(__ENCODING__) end value = Typed.new(value.type_id, v) end value end end |