Class: LokaliseRails::TaskDefinition::Exporter

Inherits:
Base
  • Object
show all
Defined in:
lib/lokalise_rails/task_definition/exporter.rb

Class Method Summary collapse

Methods inherited from Base

opt_errors, reset_api_client!

Class Method Details

.each_fileObject

Processes each translation file in the specified directory



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lokalise_rails/task_definition/exporter.rb', line 35

def each_file
  return unless block_given?

  loc_path = LokaliseRails.locales_path
  Dir["#{loc_path}/**/*"].sort.each do |f|
    full_path = Pathname.new f

    next unless file_matches_criteria? full_path

    relative_path = full_path.relative_path_from Pathname.new(loc_path)

    yield full_path, relative_path
  end
end

.export!Array

Performs translation file export from Rails to Lokalise and returns an array of queued processes

Returns:

  • (Array)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lokalise_rails/task_definition/exporter.rb', line 12

def export!
  errors = opt_errors

  if errors.any?
    errors.each { |e| $stdout.puts e }
    return errors
  end

  queued_processes = []
  each_file do |full_path, relative_path|
    queued_processes << api_client.upload_file(
      project_id_with_branch, opts(full_path, relative_path)
    )
  rescue StandardError => e
    $stdout.puts "Error while trying to upload #{full_path}: #{e.inspect}"
  end

  $stdout.print 'Task complete!'

  queued_processes
end

.file_matches_criteria?(full_path) ⇒ Boolean

Checks whether the specified file has to be processed or not

Parameters:

  • full_path (Pathname)

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/lokalise_rails/task_definition/exporter.rb', line 71

def file_matches_criteria?(full_path)
  full_path.file? && proper_ext?(full_path) &&
    !LokaliseRails.skip_file_export.call(full_path)
end

.opts(full_p, relative_p) ⇒ Hash

Generates export options

Parameters:

  • full_p (Pathname)
  • relative_p (Pathname)

Returns:

  • (Hash)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lokalise_rails/task_definition/exporter.rb', line 55

def opts(full_p, relative_p)
  content = File.read full_p

  initial_opts = {
    data: Base64.strict_encode64(content.strip),
    filename: relative_p,
    lang_iso: LokaliseRails.lang_iso_inferer.call(content)
  }

  initial_opts.merge LokaliseRails.export_opts
end