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