Class: LocalchI18n::Translations

Inherits:
Object
  • Object
show all
Defined in:
lib/localch_i18n/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.



12
13
14
15
16
17
18
19
20
# File 'lib/localch_i18n/translations.rb', line 12

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.



10
11
12
# File 'lib/localch_i18n/translations.rb', line 10

def config_file
  @config_file
end

#csv_filesObject

Returns the value of attribute csv_files.



10
11
12
# File 'lib/localch_i18n/translations.rb', line 10

def csv_files
  @csv_files
end

#localesObject

Returns the value of attribute locales.



10
11
12
# File 'lib/localch_i18n/translations.rb', line 10

def locales
  @locales
end

#tmp_folderObject

Returns the value of attribute tmp_folder.



10
11
12
# File 'lib/localch_i18n/translations.rb', line 10

def tmp_folder
  @tmp_folder
end

Instance Method Details

#clean_upObject



53
54
55
56
57
58
# File 'lib/localch_i18n/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
# File 'lib/localch_i18n/translations.rb', line 60

def download(url, destination_file)
  puts "Download '#{url}' to '#{destination_file}'"
  File.open(destination_file, 'w') do |dst|
    dst.write(open(url).read)
  end
end

#download_filesObject



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

def download_files
  files = @settings['files']
  files.each do |target_file, url|
    # 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



27
28
29
30
# File 'lib/localch_i18n/translations.rb', line 27

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

#load_localesObject



22
23
24
25
# File 'lib/localch_i18n/translations.rb', line 22

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/localch_i18n/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