Module: Traduction::I18n::I18nMethods
- Included in:
- Traduction::I18n
- Defined in:
- lib/traduction/i18n.rb
Instance Method Summary collapse
- #diff_all(options = {}) ⇒ Object
- #diff_locales(options = {}) ⇒ Object
- #diff_method(options = {}, &block) ⇒ Object
- #diff_yaml(from, to, options = {}, &block) ⇒ Object
- #generate_csv(data, options = {}) ⇒ Object
- #load_default_locale ⇒ Object
- #locale_file(locale) ⇒ Object
Instance Method Details
#diff_all(options = {}) ⇒ Object
21 22 23 24 25 |
# File 'lib/traduction/i18n.rb', line 21 def diff_all( = {}) diff_method(.merge(flatten: true)) do |from, to| from.flatten_keys.merge_hash(to.flatten_keys) end end |
#diff_locales(options = {}) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/traduction/i18n.rb', line 27 def diff_locales( = {}) prefix = [:prefix] dont_ignore_values = [:dont_ignore_values] || false diff_method() do |from, to| diff_yaml(from, to, prefix: prefix, dont_ignore_values: dont_ignore_values) end end |
#diff_method(options = {}, &block) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/traduction/i18n.rb', line 6 def diff_method( = {}, &block) from_prefix, from_content = [:from].to_a to_prefix, to_content = [:to].to_a header = [:header] format = [:format] || :csv flatten = [:flatten] || false from = YAML::load(from_content)[from_prefix] to = YAML::load(to_content)[to_prefix] data = yield from, to generate_csv(data, header: header, flatten: flatten) end |
#diff_yaml(from, to, options = {}, &block) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/traduction/i18n.rb', line 66 def diff_yaml(from, to, = {}, &block) prefix = [:prefix] || nil ignore_values = !([:dont_ignore_values] || false) data = [] from.diff_more(to, :ignore_values => ignore_values).flatten_keys(prefix).each do |k,v| data << [k,v] end data end |
#generate_csv(data, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/traduction/i18n.rb', line 36 def generate_csv(data, = {}) header = [:header] || nil flatten = [:flatten] || false CSV.generate(Traduction::CSV_FORMAT) do |csv| unless header.nil? case header when String csv << [header] when Array csv << header end end #binding.pry data.each { |m| csv << (flatten ? m.flatten : m) } if data.present? end end |
#load_default_locale ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/traduction/i18n.rb', line 54 def load_default_locale i18n_default_locale = ::I18n.default_locale { :key => i18n_default_locale.to_s, :file => locale_file(i18n_default_locale) } end |
#locale_file(locale) ⇒ Object
62 63 64 |
# File 'lib/traduction/i18n.rb', line 62 def locale_file(locale) File.join('config', 'locales', "#{locale}.yml") end |