class POEditor::Core

Constants

DO_NOT_CHANGE_MANUALLY_MESSAGE
POEDITOR_BASE_URL

Attributes

configuration[RW]

@return [POEditor::Configuration] The configuration for export

Public Class Methods

new(configuration) click to toggle source

@param configuration [POEditor::Configuration]

# File lib/Core.rb, line 16
def initialize(configuration)
  unless configuration.is_a? Configuration
    raise POEditor::Exception.new \
      "`configuration` should be an `Configuration`"
  end
  @configuration = configuration
end

Public Instance Methods

api(action, api_token, options={}) click to toggle source

Request POEditor API

@param action [String] @param api_token [String] @param options [Hash{Sting => Object}]

@return [Net::HTTPResponse] The response object of API request

@see poeditor.com/api_reference/ POEditor API Reference

# File lib/Core.rb, line 33
def api(action, api_token, options={})
  uri = URI(POEDITOR_BASE_URL + "#{action}")
  options["api_token"] = api_token
  return Net::HTTP.post_form(uri, options)
end
convert_to_android_bcp_47(translation_path, language) click to toggle source
# File lib/Core.rb, line 152
def convert_to_android_bcp_47(translation_path, language)
    # added to follow b+ convention of BCP-47 on Android
    if translation_path.include? "b+"
      return language.gsub("-", "+")
    end
    return language
end
convert_to_poeditor_language(language) click to toggle source
# File lib/Core.rb, line 106
def convert_to_poeditor_language(language)
  language = language.downcase
  android_region_language_regexp =  /(?<=[a-z]{2}-)[r](?=[a-z]{2})/i
  chinese_regions_regexp = /(zh-)\K(hans|hant)/i
  if language =~ android_region_language_regexp
    return language.gsub(android_region_language_regexp, "")
  elsif language =~ chinese_regions_regexp
    return language.gsub(chinese_regions_regexp, {'hans' => 'cn', 'hant' => 'tw'})
  end
  language
end
export(api_key:, project_id:, language:, type:, tags:nil, filters:nil) click to toggle source

Export translation for specific language

@param api_key [String] @param project_jd [String] @param language [String] @param type [String] @param tags [Array<String>] @param filters [Array<String>]

@return Downloaded translation content

# File lib/Core.rb, line 70
def export(api_key:, project_id:, language:, type:, tags:nil, filters:nil)
  options = {
    "id" => project_id,
    "language" => convert_to_poeditor_language(language),
    "type" => type,
    "tags" => (tags || []).join(","),
    "filters" => (filters || []).join(","),
  }
  response = self.api("projects/export", api_key, options)
  data = JSON(response.body)
  unless data["response"]["status"] == "success"
    code = data["response"]["code"]
    message = data["response"]["message"]
    raise POEditor::Exception.new "#{message} (#{code})"
  end

  download_uri = URI(data["result"]["url"])
  content = Net::HTTP.get(download_uri)
  content = content.force_encoding 'UTF-8'

  case type
  when "apple_strings"
    content.prepend("/* #{DO_NOT_CHANGE_MANUALLY_MESSAGE} */\n")
    content.gsub!(/(%(\d+\$)?)s/, '\1@')  # %s -> %@
  when "android_strings"
    content = content.gsub(/<resources>/, "<!-- #{DO_NOT_CHANGE_MANUALLY_MESSAGE} -->\n\\0")
    content.gsub!(/(%(\d+\$)?)@/, '\1s')  # %@ -> %s
    content.gsub!(/\.\.\./, '…')  # ... -> … (three dots -> single character for elipsis)
  end

  unless content.end_with? "\n"
    content += "\n"
  end
  return content
end
paths_for_language(language) click to toggle source
# File lib/Core.rb, line 128
def paths_for_language(language)
  if @configuration.path_copy[language]
    language = convert_to_android_bcp_47(@configuration.path_copy[language], language)
    [@configuration.path_copy[language], @configuration.path.gsub("{LANGUAGE}", language)]
  elsif @configuration.path_replace[language]
    [@configuration.path_replace[language]]
  else
    language = convert_to_android_bcp_47(@configuration.path, language)
    [@configuration.path.gsub("{LANGUAGE}", language)]
  end
end
pull() click to toggle source

Pull translations

# File lib/Core.rb, line 40
def pull()
  UI.puts "\nExport translations"
  for language in @configuration.languages
    UI.puts "  - Exporting '#{language}'"
    content = self.export(:api_key => @configuration.api_key,
                          :project_id => @configuration.project_id,
                          :language => language,
                          :type => @configuration.type,
                          :tags => @configuration.tags,
                          :filters => @configuration.filters)
    write(language, content)

    for alias_to, alias_from in @configuration.language_alias
      if language == alias_from
        write(alias_to, content)
      end
    end
  end
end
write(language, content) click to toggle source

Write translation file

# File lib/Core.rb, line 119
def write(language, content)
  if content.to_s.strip.empty?
    UI.puts "      #{"\xe2\x9c\x95".red} Ignoring language: #{language} because there are no any translations."
    return
  end
  paths = paths_for_language(language)
  paths.each { | path | write_translation_to_path(path, content)}
end
write_translation_to_path(path, content) click to toggle source
# File lib/Core.rb, line 140
def write_translation_to_path(path, content)
  if path.to_s.empty?
    raise POEditor::Exception.new "#{path} doesn't exist"
  end
  dirname = File.dirname(path)
  unless File.directory?(dirname)
    FileUtils.mkdir_p(dirname)
  end
  File.write(path, content)
  UI.puts "      #{"\xe2\x9c\x93".green} Saved at '#{path}'"
end