Class: LocalchI18n::TranslationFileExport

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/localch_i18n/translation_file_export.rb

Instance Attribute Summary collapse

Attributes included from Util

#translator

Instance Method Summary collapse

Methods included from Util

#auto_translate?, #flatten_translations_hash, #load_translations_for, #row_to_hash

Constructor Details

#initialize(source_dir, source_file, output_dir, locales, options = {}) ⇒ TranslationFileExport

Returns a new instance of TranslationFileExport.



8
9
10
11
12
13
14
15
16
17
# File 'lib/localch_i18n/translation_file_export.rb', line 8

def initialize(source_dir, source_file, output_dir, locales, options = {})
  @source_dir = source_dir
  @source_file = source_file
  @auto_translate = options[:auto_translate]

  @output_file = File.join(output_dir, source_file.gsub('.yml', '.csv'))
  @locales = locales.map {|l| l.to_s.downcase }
  
  @translations = {}
end

Instance Attribute Details

#current_localeObject (readonly)

Returns the value of attribute current_locale.



6
7
8
# File 'lib/localch_i18n/translation_file_export.rb', line 6

def current_locale
  @current_locale
end

#main_localeObject (readonly)

Returns the value of attribute main_locale.



6
7
8
# File 'lib/localch_i18n/translation_file_export.rb', line 6

def main_locale
  @main_locale
end

#translationsObject

Returns the value of attribute translations.



5
6
7
# File 'lib/localch_i18n/translation_file_export.rb', line 5

def translations
  @translations
end

Instance Method Details

#exportObject



20
21
22
23
# File 'lib/localch_i18n/translation_file_export.rb', line 20

def export
  load_translations
  write_to_csv
end

#load_language(locale) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/localch_i18n/translation_file_export.rb', line 64

def load_language(locale)
  
  puts "    #{@source_file}: load translations for '#{locale}'"
  
  input_file = File.join(@source_dir, locale, @source_file)
  
  # puts "  input file: #{input_file}"
  load_translations_for input_file, locale
end

#load_translationsObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/localch_i18n/translation_file_export.rb', line 52

def load_translations
  @locales.each do |locale|
    @current_locale = locale
    translation_hash = load_language(locale)
    unless translation_hash.blank?
      @translations[locale] = flatten_translations_hash(translation_hash)
    else
      puts "Error: No translations for locale - #{locale}"
    end
  end
end

#write_to_csvObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/localch_i18n/translation_file_export.rb', line 25

def write_to_csv
  @main_locale = main_locale = @locales.include?('en') ? 'en' : @locales.first
  
  puts "    #{@source_file}: write CSV to '#{@output_file}' \n\n"
  
  FasterCSV.open(@output_file, "wb") do |csv|
    csv << (["key"] + @locales)
            
    if @translations.empty? || !@translations[main_locale] || @translations[main_locale].keys.empty?
      puts %Q{Translations #{@source_file} for #{main_locale} could not be processed, likely due to a YAML syntax error. 
Please try again with the --normalize option. 

The problem could also be due to an invalid locale code. Please check the i18n.available_locales setting in config/application.rb} 
      exit(0)
    end

    @translations[main_locale].keys.each do |key|
      values = @locales.map do |locale|
        @translations[locale][key] if @translations[locale]
      end
      csv << values.unshift(key)
    end
  end
  
end