Module: Traduction::I18n::I18nMethods

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

Instance Method Summary collapse

Instance Method Details

#diff_locales(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/traduction/i18n.rb', line 6

def diff_locales(options = {})
  from_prefix, from_content = options[:from].to_a
  to_prefix, to_content = options[:to].to_a
  prefix = options[:prefix]
  header = options[:header]
  format = options[:format] || :csv

  from = YAML::load(from_content)[from_prefix]
  to = YAML::load(to_content)[to_prefix]

  data = diff_yaml(from, to, prefix: prefix)

  generate_csv(data, header: header)
end

#diff_yaml(from, to, options = {}, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/traduction/i18n.rb', line 49

def diff_yaml(from, to, options = {}, &block)
  prefix = options[:prefix] || nil
  data = []

  from.diff_more(to, :ignore_values => true).flatten_keys(prefix).each do |k,v|
    data << [k,v]
  end

  data
end

#generate_csv(data, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/traduction/i18n.rb', line 21

def generate_csv(data, options = {})
  header = options[:header] || nil

  CSV.generate(Traduction::CSV_FORMAT) do |csv|
    unless header.nil?
      case header
      when String
        csv << [header]
      when Array
        csv << header
      end
    end
    data.each { |m| csv << m } if data.present?
  end
end

#load_default_localeObject



37
38
39
40
41
42
43
# File 'lib/traduction/i18n.rb', line 37

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



45
46
47
# File 'lib/traduction/i18n.rb', line 45

def locale_file(locale)
  File.join('config', 'locales', "#{locale}.yml")
end