xlocalize 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ab478e509c23afe6342a33614fa8178181b96bd4
4
+ data.tar.gz: eb06d69ff88c064ac6d7edd571f4f2c233235845
5
+ SHA512:
6
+ metadata.gz: 358137228061035959ff6a38fb12bffa575355d59f6a936d30449feeb0296d12911223271791c90275d2c4bb19df559492d3cbea737411ce89457d1b98f2a229
7
+ data.tar.gz: 0d681b7ddd8da25f7a27db4038abd733f4e18b931e44c8b63bef826e017369a29630dd25a71628b966cec7e5fc480b1c8198ff7925bb97e79c40fddfeb22ca31
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in xlocalize.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 viktoras
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Xlocalize
2
+
3
+ To see all commands execute:
4
+
5
+ ```ruby
6
+ xlocalize --help
7
+ ```
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'xlocalize'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install xlocalize
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/viktorasl/xlocalize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
38
+
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
43
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "xlocalize"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/bin/xlocalize ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'xlocalize'
4
+
5
+ Xlocalize::XlocalizeCLI.new.run
@@ -0,0 +1,104 @@
1
+ require 'xlocalize/webtranslateit'
2
+ require 'colorize'
3
+ require 'nokogiri'
4
+
5
+ module Xlocalize
6
+ class Executor
7
+
8
+ def locale_file_name(locale)
9
+ return "#{locale}.xliff"
10
+ end
11
+
12
+ def export_master(wti, project, target, excl_prefix, master_lang)
13
+ master_file_name = locale_file_name(master_lang)
14
+
15
+ system "xcodebuild -exportLocalizations -localizationPath ./ -project #{project}"
16
+ purelyze(master_file_name, target, excl_prefix)
17
+
18
+ # Pushing master file to WebtranslateIt
19
+ begin
20
+ puts "Uploading master file to WebtranslateIt"
21
+ File.open(master_file_name, "r") {|file|
22
+ wti.push_master(file)
23
+ puts "Done.".green
24
+ }
25
+ rescue => err
26
+ puts err.to_s.red
27
+ end if !wti.nil?
28
+ end
29
+
30
+ def purelyze(locale_file_name, target, excl_prefix)
31
+ target_prefix = "#{target}/"
32
+ doc = Nokogiri::XML(open(locale_file_name))
33
+
34
+ puts "Removing all files not matching required targets"
35
+ doc.xpath("//xmlns:file").each { |node|
36
+ fname = node["original"]
37
+ node.remove if !fname.start_with?(target_prefix) || !fname.include?(".lproj/")
38
+ }
39
+
40
+ puts "Removing trans-unit's having reserverd prefix in their sources"
41
+ doc.xpath("//xmlns:source").each { |node|
42
+ node.parent.remove if node.content.start_with?(excl_prefix)
43
+ }
44
+
45
+ puts "Removing all files having no trans-unit elements after removal"
46
+ doc.xpath("//xmlns:body").each { |node|
47
+ node.parent.remove if node.elements.count == 0
48
+ }
49
+
50
+ puts "Writing modified XLIFF file to #{locale_file_name}"
51
+ File.open(locale_file_name, "w") {|file| file.write(doc.to_xml) }
52
+ end
53
+
54
+ def download(wti, locales)
55
+ begin
56
+ locales.each do |locale|
57
+ puts "Downloading localized file for #{locale} translation"
58
+ File.open("#{locale}.xliff", "w") {|file|
59
+ wti.pull(file, locale)
60
+ puts "Done.".green
61
+ }
62
+ end
63
+ rescue => err
64
+ puts err.to_s.red
65
+ end
66
+ end
67
+
68
+ def import(locales)
69
+ locales.each do |locale|
70
+ doc = Nokogiri::XML(open("#{locale}.xliff"))
71
+
72
+ doc.xpath("//xmlns:file").each { |node|
73
+ file_name = node["original"]
74
+ parts = file_name.split('/')
75
+ name = ""
76
+ parts.each_with_index {|part, idx|
77
+ name += "/" if idx > 0
78
+ if part.end_with?(".lproj")
79
+ name += "#{locale}.lproj"
80
+ elsif idx+1 == parts.count
81
+ # TODO: join all parts till the last '.'
82
+ name += "#{part.split('.')[0]}.strings"
83
+ else
84
+ name += part
85
+ end
86
+ }
87
+
88
+ File.open(name, "w") {|file|
89
+ (node > "body > trans-unit").each {|trans_unit|
90
+ key = trans_unit["id"]
91
+ target = (trans_unit > "target").text
92
+ note = (trans_unit > "note").text
93
+ note = "(No Commment)" if note.length <= 0
94
+
95
+ file.write "/* #{note} */\n"
96
+ file.write "\"#{key}\" = #{target.inspect};\n\n"
97
+ }
98
+ }
99
+
100
+ }
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,4 @@
1
+ module Xlocalize
2
+ VERSION = "0.1.2"
3
+ DESCRIPTION = "Xcode localizations import/export helper tool"
4
+ end
@@ -0,0 +1,52 @@
1
+ require 'net/http'
2
+ require 'net/http/post/multipart'
3
+ require 'json'
4
+
5
+ module Xlocalize
6
+ class WebtranslateIt
7
+
8
+ attr_reader :http
9
+ attr_reader :key, :source_locale, :master_file_id
10
+
11
+ def initialize(key)
12
+ @key = key
13
+
14
+ @http = Net::HTTP.new("webtranslateit.com", 443)
15
+ @http.use_ssl = true
16
+
17
+ @http.request(Net::HTTP::Get.new("/api/projects/#{@key}")) {|response|
18
+ project = JSON.parse(response.body)["project"]
19
+ @source_locale = project["source_locale"]["code"]
20
+ project["project_files"].each {|file|
21
+ if file["locale_code"] == @source_locale
22
+ @master_file_id = file["id"]
23
+ break
24
+ end
25
+ }
26
+ raise "Could not find master file for source locale #{@source_locale}" if @master_file_id.nil?
27
+ }
28
+ end
29
+
30
+ def push_master(file, override = true)
31
+ request = Net::HTTP::Put::Multipart.new("/api/projects/#{@key}/files/#{@master_file_id}/locales/#{@source_locale}", {
32
+ "file" => UploadIO.new(file, "text/plain", file.path),
33
+ "merge" => !override,
34
+ "ignore_missing" => true,
35
+ "label" => "",
36
+ "low_priority" => false })
37
+
38
+ @http.request(request) {|res|
39
+ if !res.code.to_i.between?(200, 300)
40
+ raise JSON.parse(res.body)["error"]
41
+ end
42
+ }
43
+ end
44
+
45
+ def pull(file, locale)
46
+ http.request(Net::HTTP::Get.new("/api/projects/#{@key}/files/#{master_file_id}/locales/#{locale}")) {|response|
47
+ file.write(response.body)
48
+ }
49
+ end
50
+
51
+ end
52
+ end
data/lib/xlocalize.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'xlocalize/version'
2
+ require 'xlocalize/executor'
3
+ require 'xlocalize/webtranslateit'
4
+ require 'commander'
5
+
6
+ module Xlocalize
7
+ class XlocalizeCLI
8
+ include Commander::Methods
9
+
10
+ def run
11
+ program :name, 'Xlocalize'
12
+ program :version, Xlocalize::VERSION
13
+ program :description, Xlocalize::DESCRIPTION
14
+
15
+ command :export do |c|
16
+ c.syntax = 'xlocalize export [options]'
17
+ c.description = 'Export localized strings from Xcode project'
18
+ c.option '--wti_key STRING', String, 'Webtranslateit API key'
19
+ c.option '--project STRING', String, 'Path to project file'
20
+ c.option '--target STRING', String, 'Target in the project'
21
+ c.option '--excl_prefix STRING', String, 'Exclude strings having specified prefix'
22
+ c.option '--master_lang STRING', String, 'Master language of the project'
23
+ c.action do |args, options|
24
+ if options.project.nil? or
25
+ options.target.nil? or
26
+ options.excl_prefix.nil? or
27
+ options.master_lang.nil?
28
+ raise 'Missing parameter'
29
+ end
30
+
31
+ wti = WebtranslateIt.new(options.wti_key) if !options.wti_key.nil?
32
+ Executor.new.export_master(wti, options.project, options.target, options.excl_prefix, options.master_lang)
33
+ end
34
+ end
35
+
36
+ command :download do |c|
37
+ c.syntax = 'xlocalize download [options]'
38
+ c.description = 'Download localized strings from WebtranslateIt project'
39
+ c.option '--wti_key STRING', String, 'Webtranslateit API key'
40
+ c.option '--locales ARRAY', Array, 'Locales to download'
41
+ c.action do |args, options|
42
+ if options.wti_key.nil? or
43
+ options.locales.nil?
44
+ raise 'Missing parameter'
45
+ end
46
+
47
+ Executor.new.download(WebtranslateIt.new(options.wti_key), options.locales)
48
+ end
49
+ end
50
+
51
+ command :import do |c|
52
+ c.syntax = 'xlocalize import [options]'
53
+ c.description = 'Import localized strings to Xcode project'
54
+ c.option '--locales ARRAY', Array, 'Locales to import'
55
+ c.action do |args, options|
56
+ if options.locales.nil?
57
+ raise 'Missing parameter'
58
+ end
59
+
60
+ Executor.new.import(options.locales)
61
+ end
62
+ end
63
+
64
+ run!
65
+ end
66
+ end
67
+ end
data/xlocalize.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'xlocalize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "xlocalize"
8
+ spec.version = Xlocalize::VERSION
9
+ spec.authors = ["Viktoras Laukevičius"]
10
+ spec.email = ["viktoras.laukevicius@yahoo.com"]
11
+
12
+ spec.homepage = "https://github.com/viktorasl/xlocalize"
13
+ spec.summary = Xlocalize::DESCRIPTION
14
+ spec.license = "MIT"
15
+
16
+ spec.required_ruby_version = ">= 2.0.0"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency 'nokogiri'
23
+ spec.add_runtime_dependency 'commander'
24
+ spec.add_runtime_dependency 'colorize', '~> 0'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.10"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xlocalize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Viktoras Laukevičius
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: commander
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colorize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description:
84
+ email:
85
+ - viktoras.laukevicius@yahoo.com
86
+ executables:
87
+ - console
88
+ - setup
89
+ - xlocalize
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - ".gitignore"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - bin/xlocalize
102
+ - lib/xlocalize.rb
103
+ - lib/xlocalize/executor.rb
104
+ - lib/xlocalize/version.rb
105
+ - lib/xlocalize/webtranslateit.rb
106
+ - xlocalize.gemspec
107
+ homepage: https://github.com/viktorasl/xlocalize
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: 2.0.0
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.5.1
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Xcode localizations import/export helper tool
131
+ test_files: []