1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
require 'fast_gettext/translation_repository/base'
require 'yaml'
module FastGettext
module TranslationRepository
# Responsibility:
# - find and store yaml files
# - provide access to translations in yaml files
class Yaml < Base
def initialize(name,options={})
super
reload
end
def available_locales
@files.keys
end
def plural(*keys)
['one', 'other', 'plural2', 'plural3'].map do |name|
self[yaml_dot_notation(keys.first, name)]
end
end
def pluralisation_rule
self['pluralisation_rule'] ? lambda{|n| eval(self['pluralisation_rule']) } : nil
end
def reload
find_and_store_files(@options)
super
end
protected
MAX_FIND_DEPTH = 10
def find_and_store_files(options)
@files = {}
path = options[:path] || 'config/locales'
Dir["#{path}/??.yml"].each do |yaml_file|
locale = yaml_file.match(/([a-z]{2})\.yml$/)[1]
@files[locale] = load_yaml(yaml_file, locale)
end
end
def current_translations
@files[FastGettext.locale] || super
end
# Given a yaml file return a hash of key -> translation
def load_yaml(file, locale)
yaml = YAML.load_file(file)
yaml_hash_to_dot_notation(yaml[locale])
end
def yaml_hash_to_dot_notation(yaml_hash)
add_yaml_key({}, nil, yaml_hash)
end
def add_yaml_key(result, prefix, hash)
hash.each_pair do |key, value|
if value.kind_of?(Hash)
add_yaml_key(result, yaml_dot_notation(prefix, key), value)
else
result[yaml_dot_notation(prefix, key)] = value
end
end
result
end
def yaml_dot_notation(a,b)
a ? "#{a}.#{b}" : b
end
end
end
end
|