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.
-
.open_file_or_remote(path) ⇒ String
Opens a local file or a remote URL using the provided patf.
-
.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
check_options_errors!, reset_api_client!
Class Method Details
.download_files ⇒ Hash
Downloads files from Lokalise using the specified options. Utilizes exponential backoff if “too many requests” error is received
33 34 35 36 37 38 39 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 33 def download_files with_exp_backoff(LokaliseRails.max_retries_import) do api_client.download_files project_id_with_branch, LokaliseRails.import_opts end rescue StandardError => e raise e.class, "There was an error when trying to download files: #{e.}" end |
.fetch_zip_entries(zip) ⇒ Object
Iterates over ZIP entries. Each entry may be a file or folder.
53 54 55 56 57 58 59 60 61 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 53 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 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 15 def import! 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
44 45 46 47 48 49 50 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 44 def open_and_process_zip(path) Zip::File.open_buffer(open_file_or_remote(path)) do |zip| fetch_zip_entries(zip) { |entry| process!(entry) } end rescue StandardError => e raise e.class, "There was an error when trying to process the downloaded files: #{e.}" end |
.open_file_or_remote(path) ⇒ String
Opens a local file or a remote URL using the provided patf
92 93 94 95 96 97 98 99 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 92 def open_file_or_remote(path) parsed_path = URI.parse(path) if parsed_path&.scheme&.include?('http') parsed_path.open else File.open path 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
80 81 82 83 84 85 86 87 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 80 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
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/lokalise_rails/task_definition/importer.rb', line 64 def process!(zip_entry) data = data_from zip_entry 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 LokaliseRails.translations_converter.call(data) end rescue StandardError => e raise e.class, "Error when trying to process #{zip_entry&.name}: #{e.}" end |