Class: I18nDocs::Translations
- Inherits:
-
Object
- Object
- I18nDocs::Translations
- Defined in:
- lib/i18n_docs/translations.rb
Instance Attribute Summary collapse
-
#config_file ⇒ Object
Returns the value of attribute config_file.
-
#csv_files ⇒ Object
Returns the value of attribute csv_files.
-
#locales ⇒ Object
Returns the value of attribute locales.
-
#tmp_folder ⇒ Object
Returns the value of attribute tmp_folder.
Instance Method Summary collapse
- #clean_up ⇒ Object
- #download(url, destination_file) ⇒ Object
- #download_files ⇒ Object
-
#initialize(config_file = nil, tmp_folder = nil) ⇒ Translations
constructor
A new instance of Translations.
- #load_config ⇒ Object
- #load_locales ⇒ Object
- #store_translations ⇒ Object
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_file ⇒ Object
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_files ⇒ Object
Returns the value of attribute csv_files.
8 9 10 |
# File 'lib/i18n_docs/translations.rb', line 8 def csv_files @csv_files end |
#locales ⇒ Object
Returns the value of attribute locales.
8 9 10 |
# File 'lib/i18n_docs/translations.rb', line 8 def locales @locales end |
#tmp_folder ⇒ Object
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_up ⇒ Object
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_files ⇒ Object
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_config ⇒ Object
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_locales ⇒ Object
20 21 22 23 |
# File 'lib/i18n_docs/translations.rb', line 20 def load_locales @locales = [] @locales = I18n.available_locales if defined?(I18n) end |