Module: Traduction::I18n::I18nMethods

Included in:
Traduction::I18n
Defined in:
lib/traduction/i18n.rb

Instance Method Summary collapse

Instance Method Details

#diff_all(options = {}) ⇒ Object



21
22
23
24
25
# File 'lib/traduction/i18n.rb', line 21

def diff_all(options = {})
  diff_method(options.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(options = {})
  prefix = options[:prefix]
  dont_ignore_values = options[:dont_ignore_values] || false

  diff_method(options) 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(options = {}, &block)
  from_prefix, from_content = options[:from].to_a
  to_prefix, to_content = options[:to].to_a
  header = options[:header]
  format = options[:format] || :csv
  flatten = options[: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, options = {}, &block)
  prefix = options[:prefix] || nil
  ignore_values = !(options[: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, options = {})
  header = options[:header] || nil
  flatten = options[: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_localeObject



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