class FastGettext::TranslationRepository::Yaml

Responsibility:

- find and store yaml files
- provide access to translations in yaml files

Constants

MAX_FIND_DEPTH

Public Class Methods

new(name, options = {}) click to toggle source
# File lib/fast_gettext/translation_repository/yaml.rb, line 12
def initialize(name, options = {})
  super
  reload
end

Public Instance Methods

available_locales() click to toggle source
# File lib/fast_gettext/translation_repository/yaml.rb, line 17
def available_locales
  @files.keys
end
plural(*keys) click to toggle source
# File lib/fast_gettext/translation_repository/yaml.rb, line 21
def plural(*keys)
  ['one', 'other', 'plural2', 'plural3'].map do |name|
    self[yaml_dot_notation(keys.first, name)]
  end
end
pluralisation_rule() click to toggle source
# File lib/fast_gettext/translation_repository/yaml.rb, line 27
def pluralisation_rule
  return unless rule = self['pluralisation_rule']

  ->(n) do # rubocop:disable Lint/UnusedBlockArgument n can be used from pluralisation_rule code
    eval(rule) # rubocop:disable Security/Eval TODO remove eval
  end
end
reload() click to toggle source
# File lib/fast_gettext/translation_repository/yaml.rb, line 35
def reload
  find_and_store_files(@options)
  super
end

Protected Instance Methods

add_yaml_key(result, prefix, hash) click to toggle source
# File lib/fast_gettext/translation_repository/yaml.rb, line 71
def add_yaml_key(result, prefix, hash)
  hash.each_pair do |key, value|
    if value.is_a?(Hash)
      add_yaml_key(result, yaml_dot_notation(prefix, key), value)
    else
      result[yaml_dot_notation(prefix, key)] = value
    end
  end
  result
end
current_translations() click to toggle source
# File lib/fast_gettext/translation_repository/yaml.rb, line 57
def current_translations
  @files[FastGettext.locale] || super
end
find_and_store_files(options) click to toggle source
# File lib/fast_gettext/translation_repository/yaml.rb, line 44
def find_and_store_files(options)
  @files = {}
  path = options[:path] || 'config/locales'
  Dir["#{path}/*.yml"].each do |yaml_file|
    # Only take into account the last dot separeted part of the file name,
    # excluding the extension file name
    # that is, we suppose it to be named `qq.yml` or `foo.qq.yml` where
    # `qq` stands for a locale name
    locale = File.basename(yaml_file, '.yml').split('.').last
    (@files[locale] ||= {}).merge! load_yaml(yaml_file, locale)
  end
end
load_yaml(file, locale) click to toggle source

Given a yaml file return a hash of key -> translation

# File lib/fast_gettext/translation_repository/yaml.rb, line 62
def load_yaml(file, locale)
  yaml = YAML.load_file(file)
  yaml_hash_to_dot_notation(yaml.fetch(locale))
end
yaml_dot_notation(a, b) click to toggle source
# File lib/fast_gettext/translation_repository/yaml.rb, line 82
def yaml_dot_notation(a, b) # rubocop:disable Naming/UncommunicativeMethodParamName
  a ? "#{a}.#{b}" : b
end
yaml_hash_to_dot_notation(yaml_hash) click to toggle source
# File lib/fast_gettext/translation_repository/yaml.rb, line 67
def yaml_hash_to_dot_notation(yaml_hash)
  add_yaml_key({}, nil, yaml_hash)
end