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
@@ -1,116 +1,94 @@
1
- require 'yaml'
2
-
3
- module Translatomatic::ResourceFile
4
- # YAML resource file
5
- # @see http://www.yaml.org/
6
- class YAML < Base
7
-
8
- # (see Translatomatic::ResourceFile::Base.extensions)
9
- def self.extensions
10
- %w{yml yaml}
11
- end
12
-
13
- # (see Translatomatic::ResourceFile::Base#initialize)
14
- def initialize(path, locale = nil)
15
- super(path, locale)
16
- @valid = true
17
- @data = {}
18
- @properties = @path.exist? ? read : {}
19
- end
20
-
21
- # (see Translatomatic::ResourceFile::Base#locale_path)
22
- # @note localization files in rails use the following file name
23
- # convention: config/locales/en.yml.
24
- def locale_path(locale)
25
- if path.to_s.match(/config\/locales\/[-\w]+.yml$/)
26
- # rails style
27
- filename = locale.to_s + path.extname
28
- path.dirname + filename
29
- else
30
- super(locale)
31
- end
32
- end
33
-
34
- # (see Translatomatic::ResourceFile::Base#set)
35
- def set(key, value)
36
- super(key, value)
37
-
38
- hash = @data
39
- path = key.to_s.split(/\./)
40
- last_key = path.pop
41
- path.each { |i| hash = (hash[i] ||= {}) }
42
- hash[last_key] = value
43
- end
44
-
45
- # (see Translatomatic::ResourceFile::Base#save)
46
- def save(target = path, options = {})
47
- if @data
48
- data = @data
49
- data = data.transform_keys { locale.language } if ruby_i18n?
50
- out = data.to_yaml
51
- out.sub!(/^---\n/m, '')
52
- out = comment(created_by) + "\n" + out unless options[:no_created_by]
53
- target.write(out)
54
- end
55
- end
56
-
57
- # (see Translatomatic::ResourceFile::Base#supports_variable_interpolation?)
58
- def supports_variable_interpolation?
59
- true
60
- end
61
-
62
- # (see Translatomatic::ResourceFile::Base#create_variable)
63
- def create_variable(name)
64
- return "%{#{name}}"
65
- end
66
-
67
- # (see Translatomatic::ResourceFile::Base#variable_regex)
68
- def variable_regex
69
- /\%\s*\{.*?\}/
70
- end
71
-
72
- private
73
-
74
- # true if this resource file looks like a ruby i18n data file.
75
- def ruby_i18n?
76
- if @data && @data.length == 1
77
- lang = @data.keys[0]
78
- Translatomatic::Locale.new(lang).valid?
79
- end
80
- end
81
-
82
- def comment(text)
83
- "# #{text}\n"
84
- end
85
-
86
- def read
87
- begin
88
- @data = ::YAML.load_file(@path) || {}
89
- flatten_data(@data)
90
- rescue Exception => e
91
- log.error t("resource.error", message: e.message)
92
- @valid = false
93
- {}
94
- end
95
- end
96
-
97
- def flatten_data(data)
98
- result = {}
99
- unless data.kind_of?(Hash)
100
- @valid = false
101
- return {}
102
- end
103
- data.each do |key, value|
104
- if value.kind_of?(Hash)
105
- children = flatten_data(value)
106
- children.each do |ck, cv|
107
- result[key + "." + ck] = cv
108
- end
109
- else
110
- result[key] = value
111
- end
112
- end
113
- result
114
- end
115
- end
116
- end
1
+ require 'yaml'
2
+
3
+ module Translatomatic::ResourceFile
4
+ # YAML resource file
5
+ # @see http://www.yaml.org/
6
+ class YAML < Base
7
+
8
+ # (see Translatomatic::ResourceFile::Base.extensions)
9
+ def self.extensions
10
+ %w{yml yaml}
11
+ end
12
+
13
+ # (see Translatomatic::ResourceFile::Base.is_key_value?)
14
+ def self.is_key_value?
15
+ true
16
+ end
17
+
18
+ # (see Translatomatic::ResourceFile::Base.supports_variable_interpolation?)
19
+ def self.supports_variable_interpolation?
20
+ true
21
+ end
22
+
23
+ # (see Translatomatic::ResourceFile::Base#locale_path)
24
+ # @note localization files in rails use the following file name
25
+ # convention: config/locales/en.yml.
26
+ def locale_path(locale)
27
+ if path.to_s.match(/config\/locales\/[-\w]+.yml$/)
28
+ # rails style
29
+ filename = locale.to_s + path.extname
30
+ path.dirname + filename
31
+ else
32
+ super(locale)
33
+ end
34
+ end
35
+
36
+ # (see Translatomatic::ResourceFile::Base#set)
37
+ def set(key, value)
38
+ super(key, value)
39
+
40
+ hash = @data
41
+ path = key.to_s.split(/\./)
42
+ last_key = path.pop
43
+ path.each { |i| hash = (hash[i] ||= {}) }
44
+ hash[last_key] = value
45
+ end
46
+
47
+ # (see Translatomatic::ResourceFile::Base#save)
48
+ def save(target = path, options = {})
49
+ if @data
50
+ data = @data
51
+ data = data.transform_keys { locale.language } if ruby_i18n?
52
+ out = data.to_yaml
53
+ out.sub!(/^---\n/m, '')
54
+ out = comment(created_by) + "\n" + out unless options[:no_created_by]
55
+ target.write(out)
56
+ end
57
+ end
58
+
59
+ # (see Translatomatic::ResourceFile::Base#create_variable)
60
+ def create_variable(name)
61
+ return "%{#{name}}"
62
+ end
63
+
64
+ # (see Translatomatic::ResourceFile::Base#variable_regex)
65
+ def variable_regex
66
+ /\%\s*\{.*?\}/
67
+ end
68
+
69
+ private
70
+
71
+ def init
72
+ # yaml data
73
+ @data = {}
74
+ end
75
+
76
+ def load
77
+ @data = ::YAML.load_file(path.to_s) || {}
78
+ @properties = flatten(@data)
79
+ end
80
+
81
+ # true if this resource file looks like a ruby i18n data file.
82
+ def ruby_i18n?
83
+ if @data && @data.length == 1
84
+ lang = @data.keys[0]
85
+ Translatomatic::Locale.new(lang).valid?
86
+ end
87
+ end
88
+
89
+ def comment(text)
90
+ "# #{text}\n"
91
+ end
92
+
93
+ end
94
+ end
@@ -1,78 +1,87 @@
1
-
2
- module Translatomatic
3
- # Provides methods to create resource files of various types.
4
- module ResourceFile
5
- class << self
6
- include Translatomatic::Util
7
- end
8
-
9
- # Load a resource file. If locale is not specified, the locale of the
10
- # file will be determined from the filename, or else the current default
11
- # locale will be used.
12
- # @param path [String] Path to the resource file
13
- # @param locale [String] Locale of the resource file
14
- # @return [Translatomatic::ResourceFile::Base] The resource file, or nil
15
- # if the file type is unsupported.
16
- def self.load(path, locale = nil)
17
- path = path.kind_of?(Pathname) ? path : Pathname.new(path)
18
- modules.each do |mod|
19
- # match on entire filename to support extensions containing locales
20
- if extension_match(mod, path)
21
- log.debug(t("resource.loading", file: path,
22
- name: mod.name.demodulize))
23
- file = mod.new(path, locale)
24
- return file if file.valid?
25
- end
26
- end
27
- nil
28
- end
29
-
30
- # Find all resource files under the given directory. Follows symlinks.
31
- # @param path [String, Pathname] The path to search from
32
- # @return [Array<Translatomatic::ResourceFile>] Resource files found
33
- def self.find(path, options = {})
34
- files = []
35
- include_dot_directories = options[:include_dot_directories]
36
- path = Pathname.new(path) unless path.kind_of?(Pathname)
37
- path.find do |file|
38
- if !include_dot_directories && file.basename.to_s[0] == ?.
39
- Find.prune
40
- else
41
- resource = load(file)
42
- files << resource if resource
43
- end
44
- end
45
- files
46
- end
47
-
48
- # Find all configured resource file classes
49
- # @return [Array<Class>] Available resource file classes
50
- def self.modules
51
- self.constants.map { |c| self.const_get(c) }.select do |klass|
52
- klass.is_a?(Class) && klass != Base
53
- end
54
- end
55
-
56
- private
57
-
58
- def self.extension_match(mod, path)
59
- filename = path.basename.to_s.downcase
60
- mod.extensions.each do |extension|
61
- # don't match end of line in case file has locale extension
62
- return true if filename.match(/\.#{extension}/)
63
- end
64
- false
65
- end
66
- end
67
- end
68
-
69
- require 'translatomatic/resource_file/base'
70
- require 'translatomatic/resource_file/yaml'
71
- require 'translatomatic/resource_file/properties'
72
- require 'translatomatic/resource_file/text'
73
- require 'translatomatic/resource_file/xml'
74
- require 'translatomatic/resource_file/html'
75
- require 'translatomatic/resource_file/markdown'
76
- require 'translatomatic/resource_file/xcode_strings'
77
- require 'translatomatic/resource_file/plist'
78
- require 'translatomatic/resource_file/resw'
1
+
2
+ module Translatomatic
3
+ # Provides methods to create resource files of various types.
4
+ module ResourceFile
5
+ class << self
6
+ include Translatomatic::Util
7
+ end
8
+
9
+ # Load a resource file. If locale is not specified, the locale of the
10
+ # file will be determined from the filename, or else the current default
11
+ # locale will be used.
12
+ # @param path [String] Path to the resource file
13
+ # @param locale [String] Locale of the resource file
14
+ # @return [Translatomatic::ResourceFile::Base] The resource file, or nil
15
+ # if the file type is unsupported.
16
+ def self.load(path, locale = nil)
17
+ path = path.kind_of?(Pathname) ? path : Pathname.new(path)
18
+ types_for_path(path).each do |klass|
19
+ log.debug(t("file.loading", file: path, name: klass.name.demodulize))
20
+ return klass.new(path, locale: locale)
21
+ end
22
+ nil
23
+ end
24
+
25
+ # Create a new resource file
26
+ def self.create(path, locale = nil)
27
+ klass = const_get(klass_name)
28
+ klass.new
29
+ end
30
+
31
+ # Find all resource files under the given directory. Follows symlinks.
32
+ # @param path [String, Pathname] The path to search from
33
+ # @return [Array<Translatomatic::ResourceFile>] Resource files found
34
+ def self.find(path, options = {})
35
+ files = []
36
+ include_dot_directories = options[:include_dot_directories]
37
+ path = Pathname.new(path) unless path.kind_of?(Pathname)
38
+ path.find do |file|
39
+ if !include_dot_directories && file.basename.to_s[0] == ?.
40
+ Find.prune
41
+ else
42
+ resource = load(file)
43
+ files << resource if resource
44
+ end
45
+ end
46
+ files
47
+ end
48
+
49
+ # Find all configured resource file classes
50
+ # @return [Array<Class>] Available resource file classes
51
+ def self.types
52
+ @types ||= self.constants.map { |c| self.const_get(c) }.select do |klass|
53
+ klass.is_a?(Class) && klass != Base
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ # find classes that can load the given path by file extension
60
+ def self.types_for_path(path)
61
+ path = path.kind_of?(Pathname) ? path : Pathname.new(path)
62
+ types.select { |klass| klass.enabled? && extension_match(klass, path) }
63
+ end
64
+
65
+ def self.extension_match(klass, path)
66
+ filename = path.basename.to_s.downcase
67
+ klass.extensions.each do |extension|
68
+ # don't match end of line in case file has locale extension
69
+ return true if filename.match(/\.#{extension}/)
70
+ end
71
+ false
72
+ end
73
+ end
74
+ end
75
+
76
+ require 'translatomatic/resource_file/base'
77
+ require 'translatomatic/resource_file/yaml'
78
+ require 'translatomatic/resource_file/properties'
79
+ require 'translatomatic/resource_file/text'
80
+ require 'translatomatic/resource_file/xml'
81
+ require 'translatomatic/resource_file/html'
82
+ require 'translatomatic/resource_file/markdown'
83
+ require 'translatomatic/resource_file/xcode_strings'
84
+ require 'translatomatic/resource_file/plist'
85
+ require 'translatomatic/resource_file/resw'
86
+ require 'translatomatic/resource_file/subtitle'
87
+ require 'translatomatic/resource_file/csv'