Class: LocalchI18n::CsvToYaml
- Inherits:
-
Object
- Object
- LocalchI18n::CsvToYaml
- Defined in:
- lib/localch_i18n/csv_to_yaml.rb
Instance Attribute Summary collapse
-
#input_file ⇒ Object
readonly
Returns the value of attribute input_file.
-
#locales ⇒ Object
readonly
Returns the value of attribute locales.
-
#output_file ⇒ Object
readonly
Returns the value of attribute output_file.
-
#translations ⇒ Object
readonly
Returns the value of attribute translations.
Instance Method Summary collapse
-
#initialize(input_file, output_file, locales = []) ⇒ CsvToYaml
constructor
A new instance of CsvToYaml.
- #process ⇒ Object
- #process_row(row_hash) ⇒ Object
- #store_translation(keys, locale, value) ⇒ Object
- #write_files ⇒ Object
Constructor Details
#initialize(input_file, output_file, locales = []) ⇒ CsvToYaml
Returns a new instance of CsvToYaml.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/localch_i18n/csv_to_yaml.rb', line 7 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_file ⇒ Object (readonly)
Returns the value of attribute input_file.
5 6 7 |
# File 'lib/localch_i18n/csv_to_yaml.rb', line 5 def input_file @input_file end |
#locales ⇒ Object (readonly)
Returns the value of attribute locales.
5 6 7 |
# File 'lib/localch_i18n/csv_to_yaml.rb', line 5 def locales @locales end |
#output_file ⇒ Object (readonly)
Returns the value of attribute output_file.
5 6 7 |
# File 'lib/localch_i18n/csv_to_yaml.rb', line 5 def output_file @output_file end |
#translations ⇒ Object (readonly)
Returns the value of attribute translations.
5 6 7 |
# File 'lib/localch_i18n/csv_to_yaml.rb', line 5 def translations @translations end |
Instance Method Details
#process ⇒ Object
32 33 34 35 36 |
# File 'lib/localch_i18n/csv_to_yaml.rb', line 32 def process FasterCSV.foreach(@input_file, :headers => true) do |row| process_row(row.to_hash) end end |
#process_row(row_hash) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/localch_i18n/csv_to_yaml.rb', line 38 def process_row(row_hash) key = row_hash.delete('key') key_elements = key.split('.') @locales.each do |locale| raise "Locale missing for key #{key}! (locales in app: #{@locales} / locales in file: #{row_hash.keys.to_s})" if !row_hash.has_key?(locale) store_translation(key_elements, locale, row_hash[locale]) end end |
#store_translation(keys, locale, value) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/localch_i18n/csv_to_yaml.rb', line 49 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 == '_' tree = keys[0...-1] leaf = keys.last data_hash = tree.inject(@translations[locale]) do |memo, k| if memo.has_key?(k) memo[k] else memo[k] = {} end end data_hash[leaf] = value end |
#write_files ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/localch_i18n/csv_to_yaml.rb', line 20 def write_files @locales.each do |locale| output_file_path = defined?(Rails) ? Rails.root.join('config', 'locales', locale, @output_file) : "#{locale}_#{@output_file}" 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 |