Class: POEditor::Core

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

Constant Summary collapse

POEDITOR_BASE_URL =
"https://api.poeditor.com/v2/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Core

Returns a new instance of Core.

Parameters:



15
16
17
18
19
20
21
# File 'lib/Core.rb', line 15

def initialize(configuration)
  unless configuration.is_a? Configuration
    raise POEditor::Exception.new \
      "`configuration` should be an `Configuration`"
  end
  @configuration = configuration
end

Instance Attribute Details

#configurationPOEditor::Configuration

Returns The configuration for export.

Returns:



12
13
14
# File 'lib/Core.rb', line 12

def configuration
  @configuration
end

Instance Method Details

#api(action, api_token, options = {}) ⇒ Net::HTTPResponse

Request POEditor API

Parameters:

  • action (String)
  • api_token (String)
  • options (Hash{Sting => Object}) (defaults to: {})

Returns:

  • (Net::HTTPResponse)

    The response object of API request

See Also:



32
33
34
35
36
# File 'lib/Core.rb', line 32

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_poeditor_language(language) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/Core.rb', line 101

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) ⇒ Object

Export translation for specific language

Parameters:

  • api_key (String)
  • project_jd (String)
  • language (String)
  • type (String)
  • tags (Array<String>) (defaults to: nil)
  • filters (Array<String>) (defaults to: nil)

Returns:

  • Downloaded translation content



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/Core.rb', line 69

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)

  case type
  when "apple_strings"
    content.gsub!(/(%(\d+\$)?)s/, '\1@')  # %s -> %@
  when "android_strings"
    content.gsub!(/(%(\d+\$)?)@/, '\1s')  # %@ -> %s
  end

  unless content.end_with? "\n"
    content += "\n"
  end
  return content
end

#paths_for_language(language) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/Core.rb', line 123

def paths_for_language(language)
  if @configuration.path_copy[language]
    [@configuration.path_copy[language], @configuration.path.gsub("{LANGUAGE}", language)]
  elsif @configuration.path_replace[language]
    [@configuration.path_replace[language]]
  else
    [@configuration.path.gsub("{LANGUAGE}", language)]
  end
end

#pullObject

Pull translations



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/Core.rb', line 39

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) ⇒ Object

Write translation file



114
115
116
117
118
119
120
121
# File 'lib/Core.rb', line 114

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) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/Core.rb', line 133

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