translatomatic 0.1.2 → 0.1.3

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 (79) hide show
  1. checksums.yaml +4 -4
  2. data/.translatomatic/config.yml +18 -0
  3. data/.travis.yml +33 -33
  4. data/Gemfile +6 -4
  5. data/README.de.md +53 -18
  6. data/README.es.md +55 -20
  7. data/README.fr.md +54 -19
  8. data/README.it.md +58 -23
  9. data/README.ja.md +54 -19
  10. data/README.ko.md +58 -23
  11. data/README.md +167 -141
  12. data/README.ms.md +51 -16
  13. data/README.pt.md +58 -23
  14. data/README.ru.md +53 -18
  15. data/README.sv.md +53 -18
  16. data/README.zh.md +53 -18
  17. data/bin/translatomatic +6 -6
  18. data/bin/travis +24 -26
  19. data/config/locales/translatomatic/de.yml +22 -11
  20. data/config/locales/translatomatic/en.yml +21 -12
  21. data/config/locales/translatomatic/es.yml +22 -11
  22. data/config/locales/translatomatic/fr.yml +22 -12
  23. data/config/locales/translatomatic/it.yml +22 -11
  24. data/config/locales/translatomatic/ja.yml +22 -11
  25. data/config/locales/translatomatic/ko.yml +22 -11
  26. data/config/locales/translatomatic/ms.yml +22 -11
  27. data/config/locales/translatomatic/pt.yml +22 -11
  28. data/config/locales/translatomatic/ru.yml +22 -11
  29. data/config/locales/translatomatic/sv.yml +22 -11
  30. data/config/locales/translatomatic/zh.yml +22 -11
  31. data/db/migrate/201712170000_initial.rb +25 -25
  32. data/lib/translatomatic/cli/base.rb +81 -73
  33. data/lib/translatomatic/cli/config.rb +110 -81
  34. data/lib/translatomatic/cli/main.rb +85 -72
  35. data/lib/translatomatic/cli/translate.rb +141 -106
  36. data/lib/translatomatic/cli.rb +8 -8
  37. data/lib/translatomatic/config.rb +302 -155
  38. data/lib/translatomatic/converter.rb +28 -260
  39. data/lib/translatomatic/database.rb +134 -134
  40. data/lib/translatomatic/define_options.rb +22 -0
  41. data/lib/translatomatic/escaped_unicode.rb +0 -0
  42. data/lib/translatomatic/extractor/base.rb +16 -16
  43. data/lib/translatomatic/extractor/ruby.rb +6 -6
  44. data/lib/translatomatic/extractor.rb +5 -5
  45. data/lib/translatomatic/file_translator.rb +269 -0
  46. data/lib/translatomatic/http_request.rb +162 -162
  47. data/lib/translatomatic/locale.rb +76 -76
  48. data/lib/translatomatic/logger.rb +23 -23
  49. data/lib/translatomatic/model/locale.rb +25 -25
  50. data/lib/translatomatic/model/text.rb +19 -19
  51. data/lib/translatomatic/model.rb +1 -1
  52. data/lib/translatomatic/option.rb +37 -41
  53. data/lib/translatomatic/progress_updater.rb +13 -13
  54. data/lib/translatomatic/resource_file/base.rb +269 -192
  55. data/lib/translatomatic/resource_file/csv.rb +37 -0
  56. data/lib/translatomatic/resource_file/html.rb +54 -47
  57. data/lib/translatomatic/resource_file/markdown.rb +50 -55
  58. data/lib/translatomatic/resource_file/plist.rb +153 -19
  59. data/lib/translatomatic/resource_file/po.rb +107 -0
  60. data/lib/translatomatic/resource_file/properties.rb +91 -90
  61. data/lib/translatomatic/resource_file/resw.rb +50 -30
  62. data/lib/translatomatic/resource_file/subtitle.rb +75 -0
  63. data/lib/translatomatic/resource_file/text.rb +24 -30
  64. data/lib/translatomatic/resource_file/xcode_strings.rb +75 -80
  65. data/lib/translatomatic/resource_file/xml.rb +98 -91
  66. data/lib/translatomatic/resource_file/yaml.rb +94 -116
  67. data/lib/translatomatic/resource_file.rb +87 -78
  68. data/lib/translatomatic/string.rb +188 -188
  69. data/lib/translatomatic/tmx/document.rb +99 -99
  70. data/lib/translatomatic/translation_result.rb +63 -63
  71. data/lib/translatomatic/{converter_stats.rb → translation_stats.rb} +17 -17
  72. data/lib/translatomatic/translator/base.rb +1 -1
  73. data/lib/translatomatic/translator/google.rb +2 -0
  74. data/lib/translatomatic/translator.rb +10 -2
  75. data/lib/translatomatic/util.rb +45 -45
  76. data/lib/translatomatic/version.rb +7 -7
  77. data/lib/translatomatic.rb +52 -49
  78. data/translatomatic.gemspec +3 -2
  79. metadata +25 -5
@@ -0,0 +1,75 @@
1
+ module Translatomatic::ResourceFile
2
+ # Subtitle resource file.
3
+ # requires 'titlekit' gem
4
+ class Subtitle < Base
5
+
6
+ # (see Translatomatic::ResourceFile::Base.extensions)
7
+ def self.extensions
8
+ %w{srt ass ssa}
9
+ end
10
+
11
+ # (see Translatomatic::ResourceFile::Base.enabled?)
12
+ def self.enabled?
13
+ @enabled ||= begin
14
+ require 'titlekit'
15
+ true
16
+ rescue LoadError
17
+ false
18
+ end
19
+ end
20
+
21
+ # (see Translatomatic::ResourceFile::Base#set)
22
+ def set(key, value)
23
+ super(key, value)
24
+ if @subtitle_map.include?(key)
25
+ @subtitle_map[key][:lines] = value
26
+ end
27
+ end
28
+
29
+ # (see Translatomatic::ResourceFile::Base#save)
30
+ def save(target = path, options = {})
31
+ export(target)
32
+ end
33
+
34
+ private
35
+
36
+ def init
37
+ @subtitle_map = {}
38
+ @subtitles = []
39
+ end
40
+
41
+ def load
42
+ @subtitles = import(@path)
43
+ init_subtitle_map
44
+ init_properties
45
+ end
46
+
47
+ def init_subtitle_map
48
+ # map of key1 => subtitle, key2 => subtitle, ...
49
+ @keynum = 1
50
+ @subtitles.each_with_index do |subtitle, i|
51
+ key = "key#{@keynum}"
52
+ @keynum += 1
53
+ @subtitle_map[key] = subtitle
54
+ end
55
+ end
56
+
57
+ def init_properties
58
+ @properties = @subtitle_map.transform_values { |i| i[:lines] }
59
+ end
60
+
61
+ def import(path)
62
+ import_export_class(path).import(read_contents(path))
63
+ end
64
+
65
+ def export(target, options = {})
66
+ content = import_export_class(target).export(@subtitles) || ''
67
+ target.write(content.chomp)
68
+ end
69
+
70
+ def import_export_class(path)
71
+ class_name = path.extname.sub(/^\./, '').upcase
72
+ Titlekit.const_get(class_name)
73
+ end
74
+ end
75
+ end
@@ -1,30 +1,24 @@
1
- module Translatomatic::ResourceFile
2
- # Text resource file
3
- class Text < Base
4
-
5
- # (see Translatomatic::ResourceFile::Base.extensions)
6
- def self.extensions
7
- %w{txt text}
8
- end
9
-
10
- # (see Translatomatic::ResourceFile::Base#initialize)
11
- def initialize(path, locale = nil)
12
- super(path, locale)
13
- @valid = true
14
- @properties = @path.exist? ? read(@path) : {}
15
- end
16
-
17
- # (see Translatomatic::ResourceFile::Base#save)
18
- def save(target = path, options = {})
19
- values = @properties.values.collect { |i| i.strip + "\n" }
20
- target.write(values.join)
21
- end
22
-
23
- private
24
-
25
- def read(path)
26
- text = path.read
27
- { "text" => text }
28
- end
29
- end
30
- end
1
+ module Translatomatic::ResourceFile
2
+ # Text resource file
3
+ class Text < Base
4
+
5
+ # (see Translatomatic::ResourceFile::Base.extensions)
6
+ def self.extensions
7
+ %w{txt text}
8
+ end
9
+
10
+ # (see Translatomatic::ResourceFile::Base#save)
11
+ def save(target = path, options = {})
12
+ values = @properties.values.collect { |i| i.strip + "\n" }
13
+ target.write(values.join)
14
+ end
15
+
16
+ private
17
+
18
+ def load
19
+ text = read_contents(@path)
20
+ @properties = { "text" => text }
21
+ end
22
+
23
+ end
24
+ end
@@ -1,80 +1,75 @@
1
- module Translatomatic::ResourceFile
2
-
3
- # @!visibility private
4
- module XCodeStringsLocalePath
5
-
6
- # (see Translatomatic::ResourceFile::Base#locale_path)
7
- # @note localization files in XCode use the following file name
8
- # convention: Project/locale.lproj/filename
9
- def locale_path(locale)
10
- if path.to_s.match(/\/([-\w]+).lproj\/.+$/)
11
- # xcode style
12
- filename = path.basename
13
- path.parent.parent + (locale.to_s + ".lproj") + filename
14
- else
15
- super(locale)
16
- end
17
- end
18
- end
19
-
20
- # XCode strings resource file
21
- # @see https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html
22
- class XCodeStrings < Base
23
- include Translatomatic::ResourceFile::XCodeStringsLocalePath
24
-
25
- # (see Translatomatic::ResourceFile::Base.extensions)
26
- def self.extensions
27
- %w{strings}
28
- end
29
-
30
- # (see Translatomatic::ResourceFile::Base#initialize)
31
- def initialize(path, locale = nil)
32
- super(path, locale)
33
- @valid = true
34
- @properties = @path.exist? ? read(@path) : {}
35
- end
36
-
37
- # (see Translatomatic::ResourceFile::Base#save)
38
- def save(target = path, options = {})
39
- out = ""
40
- out += comment(created_by) unless options[:no_created_by]
41
- properties.each do |key, value|
42
- key = escape(key)
43
- value = escape(value)
44
- out += %Q{"#{key}" = "#{value}";\n}
45
- end
46
- target.write(out)
47
- end
48
-
49
- private
50
-
51
- def comment(text)
52
- "/* #{text} */\n"
53
- end
54
-
55
- def read(path)
56
- result = {}
57
- content = path.read
58
- uncommented = content.gsub(/\/\*.*?\*\//, '')
59
- key_values = uncommented.scan(/"(.*?[^\\])"\s*=\s*"(.*?[^\\])"\s*;/m)
60
- key_values.each do |entry|
61
- key, value = entry
62
- result[unescape(key)] = unescape(value)
63
- end
64
-
65
- if uncommented.strip.length > 0 && key_values.length == 0
66
- @valid = false
67
- end
68
- result
69
- end
70
-
71
- def unescape(string)
72
- string ? string.gsub(/\\(["'])/) { |i| i } : ''
73
- end
74
-
75
- def escape(string)
76
- string ? string.gsub(/["']/) { |i| "\\#{i}" } : ''
77
- end
78
-
79
- end
80
- end
1
+ module Translatomatic::ResourceFile
2
+
3
+ # @!visibility private
4
+ module XCodeStringsLocalePath
5
+
6
+ # (see Translatomatic::ResourceFile::Base#locale_path)
7
+ # @note localization files in XCode use the following file name
8
+ # convention: locale.lproj/filename
9
+ def locale_path(locale)
10
+ if path.to_s.match(/\b([-\w]+).lproj\/.+$/)
11
+ # xcode style
12
+ filename = path.basename
13
+ path.parent.parent + (locale.to_s + ".lproj") + filename
14
+ else
15
+ super(locale)
16
+ end
17
+ end
18
+ end
19
+
20
+ # XCode strings resource file
21
+ # @see https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html
22
+ class XCodeStrings < Base
23
+ include Translatomatic::ResourceFile::XCodeStringsLocalePath
24
+
25
+ # (see Translatomatic::ResourceFile::Base.extensions)
26
+ def self.extensions
27
+ %w{strings}
28
+ end
29
+
30
+ # (see Translatomatic::ResourceFile::Base.is_key_value?)
31
+ def self.is_key_value?
32
+ true
33
+ end
34
+
35
+ # (see Translatomatic::ResourceFile::Base#save)
36
+ def save(target = path, options = {})
37
+ out = ""
38
+ out += comment(created_by) unless options[:no_created_by]
39
+ properties.each do |key, value|
40
+ key = escape(key)
41
+ value = escape(value)
42
+ out += %Q{"#{key}" = "#{value}";\n}
43
+ end
44
+ target.write(out)
45
+ end
46
+
47
+ private
48
+
49
+ def load
50
+ result = {}
51
+ content = read_contents(@path)
52
+ uncommented = content.gsub(/\/\*.*?\*\//, '')
53
+ key_values = uncommented.scan(/"(.*?[^\\])"\s*=\s*"(.*?[^\\])"\s*;/m)
54
+ key_values.each do |entry|
55
+ key, value = entry
56
+ result[unescape(key)] = unescape(value)
57
+ end
58
+
59
+ @properties = result
60
+ end
61
+
62
+ def comment(text)
63
+ "/* #{text} */\n"
64
+ end
65
+
66
+ def unescape(string)
67
+ string ? string.gsub(/\\(["'])/) { |i| i } : ''
68
+ end
69
+
70
+ def escape(string)
71
+ string ? string.gsub(/["']/) { |i| "\\#{i}" } : ''
72
+ end
73
+
74
+ end
75
+ end
@@ -1,96 +1,103 @@
1
- module Translatomatic::ResourceFile
1
+ module Translatomatic::ResourceFile
2
2
  # XML resource file
3
- class XML < Base
4
-
5
- # (see Translatomatic::ResourceFile::Base.extensions)
6
- def self.extensions
7
- %w{xml}
8
- end
9
-
10
- # (see Translatomatic::ResourceFile::Base#initialize)
11
- def initialize(path, locale = nil)
12
- super(path, locale)
13
- @valid = true
14
- @nodemap = {}
15
- @properties = @path.exist? ? read(@path) : {}
16
- end
17
-
18
- # (see Translatomatic::ResourceFile::Base#set)
19
- def set(key, value)
20
- super(key, value)
21
- @nodemap[key].content = value if @nodemap.include?(key)
22
- end
23
-
24
- # (see Translatomatic::ResourceFile::Base#save)
25
- def save(target = path, options = {})
26
- if @doc
27
- add_created_by unless options[:no_created_by]
28
- target.write(@doc.to_xml)
29
- end
30
- end
31
-
32
- private
33
-
34
- def comment(text)
35
- @doc.create_comment(text)
36
- end
3
+ class XML < Base
4
+
5
+ # (see Translatomatic::ResourceFile::Base.extensions)
6
+ def self.extensions
7
+ %w{xml}
8
+ end
9
+
10
+ # (see Translatomatic::ResourceFile::Base#set)
11
+ def set(key, value)
12
+ super(key, value)
13
+ if @nodemap.include?(key)
14
+ @nodemap[key].content = value
15
+ else
16
+ create_node(key, value)
17
+ end
18
+ end
19
+
20
+ # (see Translatomatic::ResourceFile::Base#save)
21
+ def save(target = path, options = {})
22
+ if @doc
23
+ add_created_by unless options[:no_created_by]
24
+ target.write(@doc.to_xml(indent: 2))
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def init
31
+ @nodemap = {}
32
+ @doc = empty_doc
33
+ end
34
+
35
+ def load
36
+ # parse xml with nokogiri
37
+ @doc = read_doc
38
+ init_nodemap
39
+ init_properties
40
+ end
41
+
42
+ def comment(text)
43
+ @doc.create_comment(text)
44
+ end
37
45
 
38
46
  def add_created_by
39
47
  @created_by ||= @doc.root.add_previous_sibling(comment(created_by))
40
48
  end
41
-
42
- # initialize nodemap from nokogiri document
43
- # returns property hash
44
- def init_nodemap(doc)
45
- if !doc.errors.empty?
46
- log.error(doc.errors)
47
- @valid = false
48
- {}
49
- else
50
- # map of key1 => node, key2 => node, ...
51
- @nodemap = create_nodemap(doc)
52
- # map of key => node content
53
- @nodemap.transform_values { |v| v.content }
54
- end
55
- end
56
-
57
- # parse xml
58
- def read(path)
59
- begin
60
- # parse xml with nokogiri
61
- @doc = read_doc(path)
62
- init_nodemap(@doc)
63
- rescue Exception => e
64
- log.error t("resource.error", message: e.message)
65
- @valid = false
66
- {}
67
- end
68
- end
69
-
70
- def read_doc(path)
71
- Nokogiri::XML(path.open) do |config|
72
- config.noblanks
73
- end
74
- end
75
-
76
- def create_nodemap(doc)
77
- result = {}
78
- text_nodes = doc.search(text_nodes_xpath)
79
- idx = 1
80
- text_nodes.each do |node|
81
- next if whitespace?(node.content)
82
- result["key#{idx}"] = node
83
- idx += 1
84
- end
85
- result
86
- end
87
-
88
- def text_nodes_xpath
89
- '//text()'
90
- end
91
-
92
- def whitespace?(text)
93
- text == nil || text.strip.length == 0
94
- end
95
- end # class
96
- end # module
49
+
50
+ def init_properties
51
+ @properties = @nodemap.transform_values { |i| i ? i.content : nil }
52
+ end
53
+
54
+ # initialize nodemap and properties hash from nokogiri document
55
+ def init_nodemap
56
+ # map of key1 => node, key2 => node, ...
57
+ @keynum = 1
58
+ text_nodes = @doc.search(text_nodes_xpath)
59
+ text_nodes.each { |node| add_node(node) }
60
+ end
61
+
62
+ def read_doc
63
+ doc = Nokogiri::XML(@path.open) do |config|
64
+ config.noblanks
65
+ end
66
+ parsing_error(doc.errors[0]) if doc.errors.present?
67
+ doc
68
+ end
69
+
70
+ def create_node(key, value)
71
+ # separate nodes by whitespace
72
+ text_node = Nokogiri::XML::Text.new("\n", @doc)
73
+ @doc.root.add_child(text_node)
74
+
75
+ # create the key/value node
76
+ node = Nokogiri::XML::Node.new(key, @doc)
77
+ node.content = value
78
+ @doc.root.add_child(node)
79
+
80
+ @nodemap[key] = node
81
+ @properties[key] = node.content
82
+ end
83
+
84
+ def add_node(node)
85
+ return if whitespace?(node.content)
86
+ key = "key#{@keynum}"
87
+ @nodemap[key] = node
88
+ @keynum += 1
89
+ end
90
+
91
+ def empty_doc
92
+ Nokogiri::XML("<root />")
93
+ end
94
+
95
+ def text_nodes_xpath
96
+ '//text()'
97
+ end
98
+
99
+ def whitespace?(text)
100
+ text == nil || text.strip.length == 0
101
+ end
102
+ end # class
103
+ end # module