Class: LokaliseRails::TaskDefinition::Importer
- Defined in:
- lib/lokalise_rails/task_definition/importer.rb
Class Method Summary collapse
-
.download_files ⇒ Hash
Downloads files from Lokalise using the specified options.
-
.fetch_zip_entries(zip) ⇒ Object
Iterates over ZIP entries.
-
.import! ⇒ Boolean
Performs translation files import from Lokalise to Rails app.
-
.open_and_process_zip(path) ⇒ Object
Opens ZIP archive (local or remote) with translations and processes its entries.
-
.proceed_when_safe_mode? ⇒ Boolean
Checks whether the user wishes to proceed when safe mode is enabled and the target directory is not empty.
-
.process!(zip_entry) ⇒ Object
Processes ZIP entry by reading its contents and creating the corresponding translation file.
Methods inherited from Base
Class Method Details
.download_files ⇒ Hash
Downloads files from Lokalise using the specified options
37 38 39 40 41 42 43 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 37 def download_files opts = LokaliseRails.import_opts api_client.download_files project_id_with_branch, opts rescue StandardError => e $stdout.puts "There was an error when trying to download files: #{e.inspect}" end |
.fetch_zip_entries(zip) ⇒ Object
Iterates over ZIP entries. Each entry may be a file or folder.
55 56 57 58 59 60 61 62 63 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 55 def fetch_zip_entries(zip) return unless block_given? zip.each do |entry| next unless proper_ext? entry.name yield entry end end |
.import! ⇒ Boolean
Performs translation files import from Lokalise to Rails app
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 15 def import! errors = opt_errors if errors.any? errors.each { |e| $stdout.puts e } return false end unless proceed_when_safe_mode? $stdout.print 'Task cancelled!' return false end open_and_process_zip download_files['bundle_url'] $stdout.print 'Task complete!' true end |
.open_and_process_zip(path) ⇒ Object
Opens ZIP archive (local or remote) with translations and processes its entries
48 49 50 51 52 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 48 def open_and_process_zip(path) Zip::File.open_buffer(URI.open(path)) do |zip| fetch_zip_entries(zip) { |entry| process!(entry) } end end |
.proceed_when_safe_mode? ⇒ Boolean
Checks whether the user wishes to proceed when safe mode is enabled and the target directory is not empty
82 83 84 85 86 87 88 89 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 82 def proceed_when_safe_mode? return true unless LokaliseRails.import_safe_mode && !Dir.empty?(LokaliseRails.locales_path.to_s) $stdout.puts "The target directory #{LokaliseRails.locales_path} is not empty!" $stdout.print 'Enter Y to continue: ' answer = $stdin.gets answer.to_s.strip == 'Y' end |
.process!(zip_entry) ⇒ Object
Processes ZIP entry by reading its contents and creating the corresponding translation file
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 66 def process!(zip_entry) data = YAML.safe_load zip_entry.get_input_stream.read subdir, filename = subdir_and_filename_for zip_entry.name full_path = "#{LokaliseRails.locales_path}/#{subdir}" FileUtils.mkdir_p full_path File.open(File.join(full_path, filename), 'w+:UTF-8') do |f| f.write data.to_yaml end rescue StandardError => e $stdout.puts "Error when trying to process #{zip_entry&.name}: #{e.inspect}" end |