translatomatic 0.1.0 → 0.1.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.
- checksums.yaml +5 -5
- data/.gitattributes +1 -0
- data/.gitignore +15 -12
- data/.rspec +3 -3
- data/.travis.yml +32 -50
- data/CODE_OF_CONDUCT.md +74 -74
- data/Gemfile +29 -5
- data/Guardfile +48 -0
- data/LICENSE.txt +21 -21
- data/README.de.md +92 -0
- data/README.es.md +92 -0
- data/README.fr.md +92 -0
- data/README.it.md +92 -0
- data/README.ja.md +92 -0
- data/README.md +96 -74
- data/Rakefile +6 -6
- data/bin/setup +8 -8
- data/bin/translatomatic +6 -6
- data/bin/travis +26 -0
- data/db/database.yml +9 -9
- data/db/migrate/201712170000_initial.rb +24 -23
- data/lib/translatomatic/cli.rb +204 -80
- data/lib/translatomatic/config.rb +12 -26
- data/lib/translatomatic/converter.rb +206 -142
- data/lib/translatomatic/converter_stats.rb +27 -27
- data/lib/translatomatic/database.rb +139 -99
- data/lib/translatomatic/escaped_unicode.rb +90 -90
- data/lib/translatomatic/extractor/base.rb +14 -0
- data/lib/translatomatic/extractor/ruby.rb +5 -0
- data/lib/translatomatic/extractor.rb +4 -0
- data/lib/translatomatic/http_request.rb +133 -0
- data/lib/translatomatic/locale.rb +52 -0
- data/lib/translatomatic/logger.rb +28 -0
- data/lib/translatomatic/model/locale.rb +21 -22
- data/lib/translatomatic/model/text.rb +17 -13
- data/lib/translatomatic/model.rb +4 -4
- data/lib/translatomatic/option.rb +24 -24
- data/lib/translatomatic/progress_updater.rb +15 -0
- data/lib/translatomatic/resource_file/base.rb +169 -137
- data/lib/translatomatic/resource_file/html.rb +46 -28
- data/lib/translatomatic/resource_file/markdown.rb +54 -0
- data/lib/translatomatic/resource_file/plist.rb +30 -29
- data/lib/translatomatic/resource_file/properties.rb +72 -60
- data/lib/translatomatic/resource_file/resw.rb +30 -0
- data/lib/translatomatic/resource_file/text.rb +29 -28
- data/lib/translatomatic/resource_file/xcode_strings.rb +71 -65
- data/lib/translatomatic/resource_file/xml.rb +79 -59
- data/lib/translatomatic/resource_file/yaml.rb +82 -80
- data/lib/translatomatic/resource_file.rb +76 -74
- data/lib/translatomatic/string.rb +160 -0
- data/lib/translatomatic/tmx/document.rb +100 -0
- data/lib/translatomatic/tmx/translation_unit.rb +19 -0
- data/lib/translatomatic/tmx.rb +4 -0
- data/lib/translatomatic/translation_result.rb +75 -57
- data/lib/translatomatic/translator/base.rb +83 -47
- data/lib/translatomatic/translator/frengly.rb +57 -64
- data/lib/translatomatic/translator/google.rb +31 -30
- data/lib/translatomatic/translator/microsoft.rb +33 -32
- data/lib/translatomatic/translator/my_memory.rb +64 -55
- data/lib/translatomatic/translator/yandex.rb +39 -37
- data/lib/translatomatic/translator.rb +63 -63
- data/lib/translatomatic/util.rb +15 -24
- data/lib/translatomatic/version.rb +4 -3
- data/lib/translatomatic.rb +32 -27
- data/translatomatic.gemspec +43 -45
- metadata +52 -18
- data/Gemfile.lock +0 -137
@@ -0,0 +1,30 @@
|
|
1
|
+
module Translatomatic::ResourceFile
|
2
|
+
class RESW < XML
|
3
|
+
|
4
|
+
# (see Translatomatic::ResourceFile::Base.extensions)
|
5
|
+
def self.extensions
|
6
|
+
%w{resw}
|
7
|
+
end
|
8
|
+
|
9
|
+
# (see Translatomatic::ResourceFile::Base#locale_path)
|
10
|
+
def locale_path(locale)
|
11
|
+
# e.g. strings/en-US/resources.resw
|
12
|
+
dir = path.dirname
|
13
|
+
dir.parent + locale.to_s + path.basename
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def create_nodemap(doc)
|
19
|
+
result = {}
|
20
|
+
key_values = doc.search('//data/@name|//text()')
|
21
|
+
key_values.each_slice(2) do |key, value|
|
22
|
+
key = key.value
|
23
|
+
value = value
|
24
|
+
result[key] = value
|
25
|
+
end
|
26
|
+
result
|
27
|
+
end
|
28
|
+
|
29
|
+
end # class
|
30
|
+
end # module
|
@@ -1,28 +1,29 @@
|
|
1
|
-
module Translatomatic::ResourceFile
|
2
|
-
class Text < Base
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
@
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
1
|
+
module Translatomatic::ResourceFile
|
2
|
+
class Text < Base
|
3
|
+
|
4
|
+
# (see Translatomatic::ResourceFile::Base.extensions)
|
5
|
+
def self.extensions
|
6
|
+
%w{txt text}
|
7
|
+
end
|
8
|
+
|
9
|
+
# (see Translatomatic::ResourceFile::Base#initialize)
|
10
|
+
def initialize(path, locale = nil)
|
11
|
+
super(path, locale)
|
12
|
+
@valid = true
|
13
|
+
@properties = @path.exist? ? read(@path) : {}
|
14
|
+
end
|
15
|
+
|
16
|
+
# (see Translatomatic::ResourceFile::Base#save)
|
17
|
+
def save(target = path, options = {})
|
18
|
+
values = @properties.values.collect { |i| i.strip + "\n" }
|
19
|
+
target.write(values.join)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def read(path)
|
25
|
+
text = path.read
|
26
|
+
{ "text" => text }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -1,65 +1,71 @@
|
|
1
|
-
module Translatomatic::ResourceFile
|
2
|
-
|
3
|
-
# XCode strings file
|
4
|
-
# @see https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html
|
5
|
-
class XCodeStrings < Base
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
@
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
#
|
20
|
-
#
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
1
|
+
module Translatomatic::ResourceFile
|
2
|
+
|
3
|
+
# XCode strings file
|
4
|
+
# @see https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html
|
5
|
+
class XCodeStrings < Base
|
6
|
+
|
7
|
+
# (see Translatomatic::ResourceFile::Base.extensions)
|
8
|
+
def self.extensions
|
9
|
+
%w{strings}
|
10
|
+
end
|
11
|
+
|
12
|
+
# (see Translatomatic::ResourceFile::Base#initialize)
|
13
|
+
def initialize(path, locale = nil)
|
14
|
+
super(path, locale)
|
15
|
+
@valid = true
|
16
|
+
@properties = @path.exist? ? read(@path) : {}
|
17
|
+
end
|
18
|
+
|
19
|
+
# (see Translatomatic::ResourceFile::Base#locale_path)
|
20
|
+
# @note localization files in XCode use the following file name
|
21
|
+
# convention: Project/locale.lproj/filename
|
22
|
+
def locale_path(locale)
|
23
|
+
if path.to_s.match(/\/([-\w]+).lproj\/.+.strings$/)
|
24
|
+
# xcode style
|
25
|
+
filename = path.basename
|
26
|
+
path.parent.parent + (locale.to_s + ".lproj") + filename
|
27
|
+
else
|
28
|
+
super(locale)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# (see Translatomatic::ResourceFile::Base#save)
|
33
|
+
def save(target = path, options = {})
|
34
|
+
out = ""
|
35
|
+
out += comment(created_by) unless options[:no_created_by]
|
36
|
+
properties.each do |key, value|
|
37
|
+
key = escape(key)
|
38
|
+
value = escape(value)
|
39
|
+
out += %Q{"#{key}" = "#{value}";\n}
|
40
|
+
end
|
41
|
+
target.write(out)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def comment(text)
|
47
|
+
"/* #{text} */\n"
|
48
|
+
end
|
49
|
+
|
50
|
+
def read(path)
|
51
|
+
result = {}
|
52
|
+
content = path.read
|
53
|
+
uncommented = content.gsub(/\/\*.*?\*\//, '')
|
54
|
+
key_values = uncommented.scan(/"(.*?[^\\])"\s*=\s*"(.*?[^\\])"\s*;/m)
|
55
|
+
key_values.each do |entry|
|
56
|
+
key, value = entry
|
57
|
+
result[unescape(key)] = unescape(value)
|
58
|
+
end
|
59
|
+
result
|
60
|
+
end
|
61
|
+
|
62
|
+
def unescape(string)
|
63
|
+
string ? string.gsub(/\\(["'])/) { |i| i } : ''
|
64
|
+
end
|
65
|
+
|
66
|
+
def escape(string)
|
67
|
+
string ? string.gsub(/["']/) { |i| "\\#{i}" } : ''
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -1,64 +1,84 @@
|
|
1
|
-
module Translatomatic::ResourceFile
|
2
|
-
class XML < Base
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
@
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
1
|
+
module Translatomatic::ResourceFile
|
2
|
+
class XML < Base
|
3
|
+
|
4
|
+
# (see Translatomatic::ResourceFile::Base.extensions)
|
5
|
+
def self.extensions
|
6
|
+
%w{xml}
|
7
|
+
end
|
8
|
+
|
9
|
+
# (see Translatomatic::ResourceFile::Base#initialize)
|
10
|
+
def initialize(path, locale = nil)
|
11
|
+
super(path, locale)
|
12
|
+
@valid = true
|
13
|
+
@properties = @path.exist? ? read(@path) : {}
|
14
|
+
end
|
15
|
+
|
16
|
+
# (see Translatomatic::ResourceFile::Base#set)
|
17
|
+
def set(key, value)
|
18
|
+
super(key, value)
|
19
|
+
@nodemap[key].content = value if @nodemap.include?(key)
|
20
|
+
end
|
21
|
+
|
22
|
+
# (see Translatomatic::ResourceFile::Base#save)
|
23
|
+
def save(target = path, options = {})
|
24
|
+
if @doc
|
25
|
+
add_created_by unless options[:no_created_by]
|
26
|
+
target.write(@doc.to_xml)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
26
30
|
private
|
27
31
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
@
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
32
|
+
def comment(text)
|
33
|
+
@doc.create_comment(text)
|
34
|
+
end
|
35
|
+
|
36
|
+
# initialize nodemap from nokogiri document
|
37
|
+
# returns property hash
|
38
|
+
def init_nodemap(doc)
|
39
|
+
# map of key1 => node, key2 => node, ...
|
40
|
+
@nodemap = create_nodemap(doc)
|
41
|
+
# map of key => node content
|
42
|
+
@nodemap.transform_values { |v| v.content }
|
43
|
+
end
|
44
|
+
|
45
|
+
# parse xml
|
46
|
+
def read(path)
|
47
|
+
begin
|
48
|
+
# parse xml with nokogiri
|
49
|
+
@doc = read_doc(path)
|
50
|
+
init_nodemap(@doc)
|
51
|
+
rescue Exception => e
|
52
|
+
log.error(e.message)
|
53
|
+
@valid = false
|
54
|
+
{}
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def read_doc(path)
|
59
|
+
Nokogiri::XML(path.open) do |config|
|
60
|
+
config.noblanks
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def create_nodemap(doc)
|
65
|
+
result = {}
|
53
66
|
text_nodes = doc.search(text_nodes_xpath)
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
67
|
+
idx = 1
|
68
|
+
text_nodes.each do |node|
|
69
|
+
next if whitespace?(node.content)
|
70
|
+
result["key#{idx}"] = node
|
71
|
+
idx += 1
|
72
|
+
end
|
73
|
+
result
|
74
|
+
end
|
75
|
+
|
76
|
+
def text_nodes_xpath
|
77
|
+
'//text()'
|
58
78
|
end
|
59
79
|
|
60
|
-
def
|
61
|
-
|
62
|
-
end
|
63
|
-
end # class
|
64
|
-
end # module
|
80
|
+
def whitespace?(text)
|
81
|
+
text == nil || text.strip.length == 0
|
82
|
+
end
|
83
|
+
end # class
|
84
|
+
end # module
|
@@ -1,80 +1,82 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
|
3
|
-
module Translatomatic::ResourceFile
|
4
|
-
class YAML < Base
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@
|
15
|
-
@
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
#
|
20
|
-
#
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
hash[
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
out.
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
end
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Translatomatic::ResourceFile
|
4
|
+
class YAML < Base
|
5
|
+
|
6
|
+
# (see Translatomatic::ResourceFile::Base.extensions)
|
7
|
+
def self.extensions
|
8
|
+
%w{yml yaml}
|
9
|
+
end
|
10
|
+
|
11
|
+
# (see Translatomatic::ResourceFile::Base#initialize)
|
12
|
+
def initialize(path, locale = nil)
|
13
|
+
super(path, locale)
|
14
|
+
@valid = true
|
15
|
+
@data = {}
|
16
|
+
@properties = @path.exist? ? read : {}
|
17
|
+
end
|
18
|
+
|
19
|
+
# (see Translatomatic::ResourceFile::Base#locale_path)
|
20
|
+
# @note localization files in rails use the following file name
|
21
|
+
# convention: config/locales/en.yml.
|
22
|
+
def locale_path(locale)
|
23
|
+
if path.to_s.match(/config\/locales\/[-\w]+.yml$/)
|
24
|
+
# rails style
|
25
|
+
filename = locale.to_s + path.extname
|
26
|
+
path.dirname + filename
|
27
|
+
else
|
28
|
+
super(locale)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# (see Translatomatic::ResourceFile::Base#set)
|
33
|
+
def set(key, value)
|
34
|
+
super(key, value)
|
35
|
+
|
36
|
+
hash = @data
|
37
|
+
path = key.split(/\./)
|
38
|
+
last_key = path.pop
|
39
|
+
path.each { |i| hash = (hash[i] ||= {}) }
|
40
|
+
hash[last_key] = value
|
41
|
+
end
|
42
|
+
|
43
|
+
# (see Translatomatic::ResourceFile::Base#save)
|
44
|
+
def save(target = path, options = {})
|
45
|
+
out = @data.to_yaml
|
46
|
+
out.sub!(/^---\n/m, '')
|
47
|
+
target.write(out)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def read
|
53
|
+
begin
|
54
|
+
@data = ::YAML.load_file(@path) || {}
|
55
|
+
flatten_data(@data)
|
56
|
+
rescue Exception => e
|
57
|
+
log.error(e.message)
|
58
|
+
@valid = false
|
59
|
+
{}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def flatten_data(data)
|
64
|
+
result = {}
|
65
|
+
unless data.kind_of?(Hash)
|
66
|
+
@valid = false
|
67
|
+
return {}
|
68
|
+
end
|
69
|
+
data.each do |key, value|
|
70
|
+
if value.kind_of?(Hash)
|
71
|
+
children = flatten_data(value)
|
72
|
+
children.each do |ck, cv|
|
73
|
+
result[key + "." + ck] = cv
|
74
|
+
end
|
75
|
+
else
|
76
|
+
result[key] = value
|
77
|
+
end
|
78
|
+
end
|
79
|
+
result
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|