ios_localizer 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +1 -0
- data/Rakefile +1 -0
- data/bin/ios_localizer +34 -0
- data/ios_localizer.gemspec +27 -0
- data/lib/ios_localizer.rb +121 -0
- data/lib/ios_localizer/version.rb +3 -0
- metadata +126 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Daniel Olshansky
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/ios_localizer
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "ios_localizer"
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
# Initialise default constants
|
7
|
+
|
8
|
+
key = nil
|
9
|
+
proj_dir = Dir.pwd
|
10
|
+
source = "en"
|
11
|
+
skip = []
|
12
|
+
|
13
|
+
# Get command line variables
|
14
|
+
|
15
|
+
OptionParser.new do |opts|
|
16
|
+
opts.banner = "Usage: your_app [options]"
|
17
|
+
opts.on('-k [ARG]', '--key [ARG]', "Mandatory Google Translate key") do |v|
|
18
|
+
key = v
|
19
|
+
end
|
20
|
+
opts.on('-s [ARG]', '--source [ARG]', "Specify the source language") do |v|
|
21
|
+
source = v
|
22
|
+
end
|
23
|
+
opts.on('-n [ARG]', '--nolang [ARG]', "Specify the languages gem should avoid converting") do |v|
|
24
|
+
skip << v
|
25
|
+
end
|
26
|
+
end.parse!
|
27
|
+
|
28
|
+
# Raise appropriate exceptions
|
29
|
+
|
30
|
+
raise OptionParser::MissingArgument if key.nil?
|
31
|
+
|
32
|
+
# Pass parameter to script
|
33
|
+
|
34
|
+
IosLocalizer.localize(key, proj_dir, source, skip)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ios_localizer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "ios_localizer"
|
8
|
+
gem.version = IosLocalizer::VERSION
|
9
|
+
gem.authors = ["Daniel Olshansky", "Amandeep Grewal"]
|
10
|
+
gem.email = ["olshansky.daniel@gmail.com", "me@amandeep.ca"]
|
11
|
+
gem.description = %q{Uses Google Translate's REST API to properly translate the source Localizable.strings file to each of the specified languages}
|
12
|
+
gem.summary = %q{Localizes iOS applications!}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = ["ios_localizer"] #gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "uri"
|
21
|
+
gem.add_development_dependency "net/https"
|
22
|
+
gem.add_development_dependency "json"
|
23
|
+
gem.add_development_dependency "cgi"
|
24
|
+
gem.add_development_dependency "htmlentities"
|
25
|
+
gem.add_development_dependency "optparse"
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require "ios_localizer/version"
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
require 'net/https'
|
5
|
+
require 'json'
|
6
|
+
require 'cgi'
|
7
|
+
require 'htmlentities'
|
8
|
+
|
9
|
+
module IosLocalizer
|
10
|
+
|
11
|
+
class HelperMethods
|
12
|
+
# setup regular expressions
|
13
|
+
|
14
|
+
$regex = /^".*" = "(.*)";$/
|
15
|
+
|
16
|
+
# Helper functions
|
17
|
+
|
18
|
+
def getDataFromURL(url)
|
19
|
+
uri = URI.parse(url.strip)
|
20
|
+
http = Net::HTTP.new(uri.host,uri.port)
|
21
|
+
http.use_ssl = true
|
22
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
23
|
+
lr = http.request(Net::HTTP::Get.new(uri.request_uri))
|
24
|
+
JSON.parse (lr.body)
|
25
|
+
end
|
26
|
+
|
27
|
+
def directory_exists?(directory)
|
28
|
+
File.directory?(directory)
|
29
|
+
end
|
30
|
+
|
31
|
+
def extract_word(line)
|
32
|
+
$regex.match(line)[1]
|
33
|
+
end
|
34
|
+
|
35
|
+
def should_line_be_translated (line)
|
36
|
+
$regex.match(line) != nil
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
def IosLocalizer.localize(key, proj_dir, source, skip)
|
42
|
+
|
43
|
+
h = HelperMethods.new
|
44
|
+
|
45
|
+
source_strings_file = proj_dir + "/" + source + ".lproj/Localizable.strings"
|
46
|
+
|
47
|
+
#Get languages
|
48
|
+
|
49
|
+
lurl = 'https://www.googleapis.com/language/translate/v2/languages?key=' + key
|
50
|
+
|
51
|
+
ldata = h.getDataFromURL (lurl)
|
52
|
+
|
53
|
+
if ldata.has_key? 'Error'
|
54
|
+
raise "web service error"
|
55
|
+
end
|
56
|
+
|
57
|
+
languages = Array.new
|
58
|
+
|
59
|
+
ldata["data"]["languages"].each do |language|
|
60
|
+
unless language["language"].eql? source
|
61
|
+
unless skip.include? language["language"]
|
62
|
+
lproj_dir = proj_dir + "/" +language["language"] +".lproj"
|
63
|
+
if h.directory_exists?(lproj_dir)
|
64
|
+
languages << language["language"]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
#Generate source language file
|
71
|
+
|
72
|
+
gen_strings_command = "find " + proj_dir + "/ -name *.m -print0 | xargs -0 genstrings -o " + proj_dir + "/" + source + ".lproj"
|
73
|
+
system(gen_strings_command)
|
74
|
+
|
75
|
+
#Get words that need to be translated
|
76
|
+
|
77
|
+
words = Array.new
|
78
|
+
comment_str = "/*"
|
79
|
+
src = File.open(source_strings_file, "rb:UTF-16LE")
|
80
|
+
while (line = src.gets)
|
81
|
+
line = line.encode('UTF-8')
|
82
|
+
if h.should_line_be_translated(line)
|
83
|
+
words << h.extract_word(line)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
#Prepare Translate Query
|
88
|
+
|
89
|
+
turl = "https://www.googleapis.com/language/translate/v2?key=" + key + "&source=" + source
|
90
|
+
words.each do |word|
|
91
|
+
turl += "&q=" + CGI::escape(word)
|
92
|
+
end
|
93
|
+
|
94
|
+
#Translate Words & write new file
|
95
|
+
|
96
|
+
coder = HTMLEntities.new
|
97
|
+
|
98
|
+
languages.each do |lang|
|
99
|
+
lang_strings_file = proj_dir + "/" + lang + ".lproj/Localizable.strings"
|
100
|
+
dest = File.open(lang_strings_file, "w")
|
101
|
+
|
102
|
+
translate_url = turl + "&target=" + lang
|
103
|
+
tdata = h.getDataFromURL(translate_url)
|
104
|
+
|
105
|
+
i = 0
|
106
|
+
src = File.open(source_strings_file, "rb:UTF-16LE")
|
107
|
+
while (line = src.gets)
|
108
|
+
line = line.encode('UTF-8')
|
109
|
+
if h.should_line_be_translated(line)
|
110
|
+
extracted_word = (h.extract_word(line))
|
111
|
+
line = line.reverse.sub(extracted_word.reverse, coder.decode(tdata["data"]["translations"][i]["translatedText"]).reverse).reverse
|
112
|
+
dest.syswrite(line)
|
113
|
+
i += 1
|
114
|
+
else
|
115
|
+
dest.syswrite(line)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ios_localizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Olshansky
|
9
|
+
- Amandeep Grewal
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-09-09 00:00:00.000000000 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: uri
|
18
|
+
requirement: &70338768892080 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ! '>='
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '0'
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *70338768892080
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: net/https
|
29
|
+
requirement: &70338768891600 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *70338768891600
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: json
|
40
|
+
requirement: &70338768891100 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *70338768891100
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: cgi
|
51
|
+
requirement: &70338768890640 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *70338768890640
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: htmlentities
|
62
|
+
requirement: &70338768890140 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *70338768890140
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: optparse
|
73
|
+
requirement: &70338768889660 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
type: :development
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: *70338768889660
|
82
|
+
description: Uses Google Translate's REST API to properly translate the source Localizable.strings
|
83
|
+
file to each of the specified languages
|
84
|
+
email:
|
85
|
+
- olshansky.daniel@gmail.com
|
86
|
+
- me@amandeep.ca
|
87
|
+
executables:
|
88
|
+
- ios_localizer
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- .gitignore
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/ios_localizer
|
98
|
+
- ios_localizer.gemspec
|
99
|
+
- lib/ios_localizer.rb
|
100
|
+
- lib/ios_localizer/version.rb
|
101
|
+
has_rdoc: true
|
102
|
+
homepage: ''
|
103
|
+
licenses: []
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.6.2
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: Localizes iOS applications!
|
126
|
+
test_files: []
|