Class: I18nDocs::Translations

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil, tmp_folder = nil) ⇒ Translations

Returns a new instance of Translations.



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

def initialize(config_file = nil, tmp_folder = nil)
  @config_file = config_file
  @tmp_folder  = tmp_folder

  @csv_files = {}

  load_config
  load_locales
end

Instance Attribute Details

#config_fileObject

Returns the value of attribute config_file.



8
9
10
# File 'lib/i18n_docs/translations.rb', line 8

def config_file
  @config_file
end

#csv_filesObject

Returns the value of attribute csv_files.



8
9
10
# File 'lib/i18n_docs/translations.rb', line 8

def csv_files
  @csv_files
end

#localesObject

Returns the value of attribute locales.



8
9
10
# File 'lib/i18n_docs/translations.rb', line 8

def locales
  @locales
end

#tmp_folderObject

Returns the value of attribute tmp_folder.



8
9
10
# File 'lib/i18n_docs/translations.rb', line 8

def tmp_folder
  @tmp_folder
end

Instance Method Details

#clean_upObject



53
54
55
56
57
58
# File 'lib/i18n_docs/translations.rb', line 53

def clean_up
  # remove all tmp files
  @csv_files.each do |_target_file, csv_file|
    File.unlink(csv_file)
  end
end

#download(url, destination_file) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/i18n_docs/translations.rb', line 60

def download(url, destination_file)
  puts "Download '#{url}' to '#{destination_file}'"
  doc_data = URI.open(url).read.force_encoding('UTF-8')
  if (subs = @settings['substitutions'])
    subs.each do |sub|
      doc_data.gsub! sub['from'], sub['to']
    end
  end
  File.open(destination_file, 'w') do |dst|
    dst.write(doc_data)
  end
end

#download_filesObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/i18n_docs/translations.rb', line 30

def download_files
  files = @settings['files']
  files.each do |target_file, url|
    # ensure .yml filename
    target_file += '.yml' if target_file !~ /\.yml$/
    # download file to tmp directory
    tmp_file = File.basename(target_file).gsub('.yml', '.csv')
    tmp_file = File.join(@tmp_folder, tmp_file)
    download(url, tmp_file)
    @csv_files[target_file] = tmp_file
  end
end

#load_configObject



25
26
27
28
# File 'lib/i18n_docs/translations.rb', line 25

def load_config
  @settings = {}
  @settings = YAML.load_file(config_file) if File.exist?(config_file)
end

#load_localesObject



20
21
22
23
# File 'lib/i18n_docs/translations.rb', line 20

def load_locales
  @locales = []
  @locales = I18n.available_locales if defined?(I18n)
end

#store_translationsObject



43
44
45
46
47
48
49
50
51
# File 'lib/i18n_docs/translations.rb', line 43

def store_translations
  @csv_files.each do |target_file, csv_file|
    converter = CsvToYaml.new(csv_file, target_file, @locales)
    converter.process
    converter.write_files
  end

  @csv_files
end