Parent

Included Modules

R18n::Loader::YAML

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')

Attributes

dir[R]

Dir with translations.

Public Class Methods

new(dir) click to toggle source

Create new loader for dir with YAML translations.

# File lib/r18n-core/yaml_loader.rb, line 39
def initialize(dir)
  @dir = File.expand_path(dir)
  detect_yaml_private_type
end

Public Instance Methods

==(loader) click to toggle source

Is another loader load YAML translations from same dir.

# File lib/r18n-core/yaml_loader.rb, line 68
def ==(loader)
  self.class == loader.class and self.dir == loader.dir
end
available() click to toggle source

Array of locales, which has translations in dir.

# File lib/r18n-core/yaml_loader.rb, line 45
def available
  Dir.glob(File.join(@dir, '**/*.yml')).
    map { |i| File.basename(i, '.yml') }.uniq.
    map { |i| R18n.locale(i) }
end
hash() click to toggle source

YAML loader with same dir will be have same hash.

# File lib/r18n-core/yaml_loader.rb, line 63
def hash
  self.class.hash + @dir.hash
end
load(locale) click to toggle source

Return Hash with translations for locale.

# File lib/r18n-core/yaml_loader.rb, line 52
def load(locale)
  initialize_types

  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) click to toggle source

Wrap YAML private types to Typed.

# File lib/r18n-core/yaml_loader.rb, line 73
def transform(a_hash)
  R18n::Utils.hash_map(a_hash) do |key, value|
    if value.is_a? Hash
      value = transform(value)
    elsif @private_type_class and value.is_a? @private_type_class
      v = value.value
      if v.respond_to?(:force_encoding) and v.encoding != __ENCODING__
        v = v.force_encoding(__ENCODING__)
      end
      value = Typed.new(value.type_id, v)
    end
    [key, value]
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.