class LokaliseRails::TaskDefinition::Importer

Public Class Methods

download_files() click to toggle source

Downloads files from Lokalise using the specified options. Utilizes exponential backoff if “too many requests” error is received

@return [Hash]

# 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.message}"
end
fetch_zip_entries(zip) { |entry| ... } click to toggle source

Iterates over ZIP entries. Each entry may be a file or folder.

# 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!() click to toggle source

Performs translation files import from Lokalise to Rails app

@return [Boolean]

# File lib/lokalise_rails/task_definition/importer.rb, line 15
def import!
  check_options_errors!

  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) click to toggle source

Opens ZIP archive (local or remote) with translations and processes its entries

@param path [String]

# 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.message}"
end
open_file_or_remote(path) click to toggle source

Opens a local file or a remote URL using the provided patf

@return [String]

# 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?() click to toggle source

Checks whether the user wishes to proceed when safe mode is enabled and the target directory is not empty

@return [Boolean]

# 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) click to toggle source

Processes ZIP entry by reading its contents and creating the corresponding translation file

# 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.message}"
end

Private Class Methods

data_from(zip_entry) click to toggle source
# File lib/lokalise_rails/task_definition/importer.rb, line 103
def data_from(zip_entry)
  LokaliseRails.translations_loader.call zip_entry.get_input_stream.read
end