Class: POEditor::Core
- Inherits:
-
Object
- Object
- POEditor::Core
- Defined in:
- lib/poeditor/core.rb
Instance Attribute Summary collapse
-
#configuration ⇒ POEditor::Configuration
The configuration for export.
Instance Method Summary collapse
-
#api(action, api_token, options = {}) ⇒ Net::HTTPResponse
Request POEditor API.
- #convert_to_poeditor_language(language) ⇒ Object
-
#export(api_key:, project_id:, language:, type:, tags: nil) ⇒ Object
Export translation for specific language.
-
#initialize(configuration) ⇒ Core
constructor
A new instance of Core.
- #path_for_language(language) ⇒ Object
-
#pull ⇒ Object
Pull translations.
-
#write(language, content) ⇒ Object
Write translation file.
Constructor Details
#initialize(configuration) ⇒ Core
Returns a new instance of Core.
10 11 12 13 14 15 16 |
# File 'lib/poeditor/core.rb', line 10 def initialize(configuration) unless configuration.is_a? Configuration raise POEditor::Exception.new \ "`configuration` should be an `Configuration`" end @configuration = configuration end |
Instance Attribute Details
#configuration ⇒ POEditor::Configuration
Returns The configuration for export.
7 8 9 |
# File 'lib/poeditor/core.rb', line 7 def configuration @configuration end |
Instance Method Details
#api(action, api_token, options = {}) ⇒ Net::HTTPResponse
Request POEditor API
27 28 29 30 31 32 |
# File 'lib/poeditor/core.rb', line 27 def api(action, api_token, ={}) uri = URI("https://poeditor.com/api/") ["action"] = action ["api_token"] = api_token return Net::HTTP.post_form(uri, ) end |
#convert_to_poeditor_language(language) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/poeditor/core.rb', line 94 def convert_to_poeditor_language(language) if language.downcase.match(/zh.+(hans|cn)/) 'zh-CN' elsif language.downcase.match(/zh.+(hant|tw)/) 'zh-TW' else language end end |
#export(api_key:, project_id:, language:, type:, tags: nil) ⇒ Object
Export translation for specific language
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/poeditor/core.rb', line 63 def export(api_key:, project_id:, language:, type:, tags:nil) = { "id" => project_id, "language" => convert_to_poeditor_language(language), "type" => type, "tags" => ( || []).join(","), } response = self.api("export", api_key, ) data = JSON(response.body) unless data["response"]["status"] == "success" code = data["response"]["code"] = data["response"]["message"] raise POEditor::Exception.new "#{} (#{code})" end download_uri = URI(data["item"]) 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 |
#path_for_language(language) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/poeditor/core.rb', line 114 def path_for_language(language) if @configuration.path_replace[language] @configuration.path_replace[language] else @configuration.path.gsub("{LANGUAGE}", language) end end |
#pull ⇒ Object
Pull translations
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/poeditor/core.rb', line 35 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.) 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
105 106 107 108 109 110 111 112 |
# File 'lib/poeditor/core.rb', line 105 def write(language, content) path = path_for_language(language) unless File.exist?(path) raise POEditor::Exception.new "#{path} doesn't exist" end File.write(path, content) UI.puts " #{"\xe2\x9c\x93".green} Saved at '#{path}'" end |