Class: AnnoTranslate::TranslationsExporter

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_prefix, export_to = nil) ⇒ TranslationsExporter

Returns a new instance of TranslationsExporter.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/import_export.rb', line 14

def initialize(file_prefix, export_to=nil)
  # Format pertinent paths for files
  here = File.expand_path(File.dirname(__FILE__))
  config = File.expand_path(File.join(here, "..", "..", "config"))
  @locales_folder = File.join(config, "locales")
  @base_yml_file = File.join(@locales_folder, "en.yml")
  @prefix = file_prefix
  @translations_support = File.join(config, "translations")
  @duplicates_file = File.join(@translations_support, "#{@prefix}_shared_strings.yml")
  @export_folder = export_to ? export_to : File.join(@translations_support, "export")

  @base_locale = YAML.load_file(@base_yml_file)
  @cache = YAML.load_file(@duplicates_file)

  FileUtils.rm_f Dir["#{@export_folder}/*.csv"]

  # Create supported foreign languages collection
  @foreign_languages = FOREIGN_LOCALES.keys.map do |code|
    source_yml = File.join(@locales_folder, "#{code}.yml")
    dest_csv = File.join(@export_folder, "#{@prefix}.#{code}.csv")
    {code: code, name: FOREIGN_LOCALES[code], yml: source_yml, csv: dest_csv}
  end
end

Class Method Details

.export(file_prefix, export_to = nil) ⇒ Object



9
10
11
12
# File 'lib/import_export.rb', line 9

def self.export(file_prefix, export_to=nil)
  exporter = self.new(file_prefix, export_to)
  exporter.export_translations
end

Instance Method Details

#export_translationsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/import_export.rb', line 38

def export_translations
  # Load English strings first to use as golden copy of all translatable strings/keys
  load_english_strings

  # Generate CSV export files for each foreign language
  @foreign_languages.each do |lang|
    puts "Exporting #{lang[:name]} (#{lang[:code]}) translations"
    puts "  from:  #{lang[:yml]}" if File.exist? lang[:yml]
    puts "  using: #{@base_yml_file}"
    puts "  to:    #{lang[:csv]}"

    # Export keys/translations to the proper CSV file
    CSV.open(lang[:csv], "wb", encoding: 'UTF-8') do |csv|
      csv << ["Key", "String#", "English Version", "#{lang[:name]} Translation"]
      index = 0
      load_translations(lang).each do |id, translation|
        csv << [id, index+1, @english[index].last, translation]
        index += 1
      end
    end
  end
end