Class: TranslatrToolkit::TranslatrToolkit

Inherits:
Object
  • Object
show all
Defined in:
lib/translatr_toolkit.rb

Overview

Utility methods for interfacing between Translatr and OneSky.

Instance Method Summary collapse

Constructor Details

#initialize(onesky_api_key, onesky_api_secret, onesky_project_id) ⇒ TranslatrToolkit

Constructor

Parameters:

  • onesky_api_key (String)

    OneSky API key

  • onesky_api_secret (String)

    OneSky API secret key

  • onesky_project_id (String)

    OneSky Project ID



15
16
17
18
# File 'lib/translatr_toolkit.rb', line 15

def initialize(onesky_api_key, onesky_api_secret, onesky_project_id)
  client = Onesky::Client.new(onesky_api_key, onesky_api_secret)
  @project = client.project(onesky_project_id)
end

Instance Method Details

#download_gettext_localizations(lang_code_map, dest_dir) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/translatr_toolkit.rb', line 32

def download_gettext_localizations(lang_code_map, dest_dir)
  # Downloads PO files for provided languages from OneSky.
  # @param lang_code_map [Hash]   Hash that maps OneSky language codes to another scheme
  # @param dest_dir      [String] Directory to save PO files to
  lang_code_map.each do |onesky_locale, local_locale|
    po_file_path = File.join(dest_dir, "#{local_locale || onesky_locale}.po")

    puts "Downloading PO file for '#{onesky_locale}' " + \
         (local_locale != onesky_locale ? "(Tumblr locale '#{local_locale}') " : "") + \
         "to #{po_file_path}"

    resp = @project.export_translation(
      source_file_name: "en.pot",
      locale: onesky_locale,
      export_file_name: po_file_path
    )

    File.open(po_file_path, "w+") { |file| file.write(resp) }
  end
end

#upload_template_to_onesky(path_to_source_pot) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/translatr_toolkit.rb', line 20

def upload_template_to_onesky(path_to_source_pot)
  # Uploads a POT file to OneSky.
  # @param path_to_source_pot [String] Path to the POT file
  puts "Uploading #{path_to_source_pot} to OneSky"
  resp = @project.upload_file(
    file: path_to_source_pot,
    file_format: "GNU_POT"
  )
  puts "Response code: #{resp.code}"
  puts "Body: #{resp.body}"
end