Class: I18nDocs::CsvToYaml

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_docs/csv_to_yaml.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_file, output_file, locales = []) ⇒ CsvToYaml

Returns a new instance of CsvToYaml.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/i18n_docs/csv_to_yaml.rb', line 9

def initialize(input_file, output_file, locales = [])
  @input_file = input_file
  @output_file = File.basename(output_file)
  @locales = locales.map(&:to_s)

  # init translation hash
  @translations = {}
  @locales.each do |locale|
    @translations[locale] = {}
  end
end

Instance Attribute Details

#input_fileObject (readonly)

Returns the value of attribute input_file.



3
4
5
# File 'lib/i18n_docs/csv_to_yaml.rb', line 3

def input_file
  @input_file
end

#localesObject (readonly)

Returns the value of attribute locales.



3
4
5
# File 'lib/i18n_docs/csv_to_yaml.rb', line 3

def locales
  @locales
end

#output_fileObject (readonly)

Returns the value of attribute output_file.



3
4
5
# File 'lib/i18n_docs/csv_to_yaml.rb', line 3

def output_file
  @output_file
end

#translationsObject (readonly)

Returns the value of attribute translations.



3
4
5
# File 'lib/i18n_docs/csv_to_yaml.rb', line 3

def translations
  @translations
end

Class Method Details

.root_pathObject



5
6
7
# File 'lib/i18n_docs/csv_to_yaml.rb', line 5

def self.root_path
  @root_path ||= defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd)
end

Instance Method Details

#processObject



34
35
36
37
38
# File 'lib/i18n_docs/csv_to_yaml.rb', line 34

def process
  CSV.foreach(@input_file, headers: true) do |row|
    process_row(row.to_hash)
  end
end

#process_row(row_hash) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/i18n_docs/csv_to_yaml.rb', line 40

def process_row(row_hash)
  key = row_hash.delete('key')
  return unless key

  key_elements = key.split('.')
  @locales.each do |locale|
    unless row_hash.key?(locale)
      raise "Locale missing for key #{key}! (locales in app: #{@locales} / locales in file: #{row_hash.keys})"
    end
    store_translation(key_elements, locale, row_hash[locale])
  end
end

#store_translation(keys, locale, value) ⇒ Object



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
78
# File 'lib/i18n_docs/csv_to_yaml.rb', line 53

def store_translation(keys, locale, value)
  return nil if value.nil? # we don't store keys that don't have a valid value
  # Google Spreadsheet does not export empty strings and therefore we use '_' as a replacement char.
  value = '' if value == '_'

  keys.each(&:strip!)
  tree = keys[0...-1]
  leaf = keys.last
  data_hash = tree.inject(@translations[locale]) do |memo, k|
    if memo.is_a? Hash
      if memo.key?(k)
        memo[k]
      else
        memo[k] = {}
      end
    else
      raise "Error around key '#{keys.join '.'}': Expected #{memo.inspect} to be a Hash"
    end
  end

  if data_hash.is_a? String
    raise "Error around key '#{keys.join '.'}': Expected #{data_hash.inspect} to be a Hash"
  end

  data_hash[leaf] = value
end

#write_filesObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/i18n_docs/csv_to_yaml.rb', line 21

def write_files
  @locales.each do |locale|
    output_file_path = self.class.root_path.join('config', 'locales', locale, @output_file)
    FileUtils.mkdir_p File.dirname(output_file_path)

    File.open(output_file_path, 'w') do |file|
      final_translation_hash = { locale => @translations[locale] }
      file.puts YAML.dump(final_translation_hash)
    end
    puts "File '#{@output_file}' for language '#{locale}' written to disc (#{output_file_path})"
  end
end