Module: LocalchI18n::Util
- Defined in:
- lib/localch_i18n/util.rb
Instance Attribute Summary collapse
-
#translator ⇒ Object
Returns the value of attribute translator.
Instance Method Summary collapse
- #auto_translate? ⇒ Boolean
-
#flatten_translations_hash(translations, options = {:parent_key => [] }) ⇒ Object
options: - parent_key = [] - auto_translate.
- #load_translations_for(input_file, locale) ⇒ Object
- #row_to_hash(key, value) ⇒ Object
Instance Attribute Details
#translator ⇒ Object
Returns the value of attribute translator.
59 60 61 |
# File 'lib/localch_i18n/util.rb', line 59 def translator @translator end |
Instance Method Details
#auto_translate? ⇒ Boolean
55 56 57 |
# File 'lib/localch_i18n/util.rb', line 55 def auto_translate? false end |
#flatten_translations_hash(translations, options = {:parent_key => [] }) ⇒ Object
options:
- parent_key = []
- auto_translate
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/localch_i18n/util.rb', line 33 def flatten_translations_hash(translations, = {:parent_key => [] }) flat_hash = {} parent_key = [:parent_key] || [] translations.each do |key, t| current_key = parent_key.dup << key if t.is_a?(Hash) # descend ||= {} .merge!(:parent_key => current_key) flat_hash.merge!(flatten_translations_hash(t, )) else # leaf -> store as value for key flat_hash[current_key.join('.')] = t end end if [:auto_translate] || auto_translate? translator.auto_translate(flat_hash) else flat_hash end end |
#load_translations_for(input_file, locale) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/localch_i18n/util.rb', line 3 def load_translations_for input_file, locale translations = {} translations = YAML.load_file(input_file) if File.exists?(input_file) # Hack to fix "bug" when 'no' for Norway encountered. # Parser turns it into false as the key no = translations[false] translations['no'] = no puts " No translations found!" and return if translations.empty? puts " Missing or bad translations root key:" and return if !translations[locale] translations[locale] end |
#row_to_hash(key, value) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/localch_i18n/util.rb', line 17 def row_to_hash(key, value) res = {} keys = key.split('.') keys << value h = keys.reverse.inject(res) do |a, n| if n != keys.last { n => a } else n end end end |