translatomatic 0.1.3 → 0.2.0

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 (139) hide show
  1. checksums.yaml +5 -5
  2. data/.gitattributes +20 -20
  3. data/.gitignore +19 -15
  4. data/.rspec +3 -3
  5. data/.rubocop.yml +28 -0
  6. data/.translatomatic/config.yml +4 -0
  7. data/.travis.yml +4 -6
  8. data/.yardopts +9 -9
  9. data/Gemfile +8 -4
  10. data/Guardfile +4 -5
  11. data/README.de.md +55 -50
  12. data/README.en.md +177 -0
  13. data/README.es.md +53 -48
  14. data/README.fr.md +53 -48
  15. data/README.it.md +54 -49
  16. data/README.ja.md +63 -58
  17. data/README.ko.md +59 -54
  18. data/README.md +17 -13
  19. data/README.ms.md +50 -45
  20. data/README.pt.md +54 -49
  21. data/README.ru.md +57 -52
  22. data/README.sv.md +51 -46
  23. data/README.zh.md +60 -55
  24. data/Rakefile +3 -3
  25. data/TODO.txt +6 -0
  26. data/bin/console +3 -3
  27. data/bin/translatomatic +4 -2
  28. data/config/i18n-tasks.yml +130 -0
  29. data/config/locales/translatomatic/de.yml +141 -99
  30. data/config/locales/translatomatic/en.yml +129 -89
  31. data/config/locales/translatomatic/es.yml +136 -99
  32. data/config/locales/translatomatic/fr.yml +139 -100
  33. data/config/locales/translatomatic/it.yml +135 -97
  34. data/config/locales/translatomatic/ja.yml +137 -98
  35. data/config/locales/translatomatic/ko.yml +138 -98
  36. data/config/locales/translatomatic/ms.yml +138 -100
  37. data/config/locales/translatomatic/pt.yml +137 -101
  38. data/config/locales/translatomatic/ru.yml +136 -98
  39. data/config/locales/translatomatic/sv.yml +134 -96
  40. data/config/locales/translatomatic/zh.yml +136 -97
  41. data/db/migrate/201712170000_initial.rb +2 -3
  42. data/lib/translatomatic.rb +40 -25
  43. data/lib/translatomatic/cli.rb +5 -1
  44. data/lib/translatomatic/cli/base.rb +61 -58
  45. data/lib/translatomatic/cli/common_options.rb +14 -11
  46. data/lib/translatomatic/cli/config.rb +96 -91
  47. data/lib/translatomatic/cli/database.rb +85 -23
  48. data/lib/translatomatic/cli/main.rb +158 -104
  49. data/lib/translatomatic/cli/thor.rb +29 -0
  50. data/lib/translatomatic/cli/translate.rb +134 -157
  51. data/lib/translatomatic/config.rb +10 -301
  52. data/lib/translatomatic/config/display.rb +78 -0
  53. data/lib/translatomatic/config/files.rb +60 -0
  54. data/lib/translatomatic/config/location_settings.rb +133 -0
  55. data/lib/translatomatic/config/options.rb +68 -0
  56. data/lib/translatomatic/config/selector.rb +127 -0
  57. data/lib/translatomatic/config/settings.rb +148 -0
  58. data/lib/translatomatic/converter.rb +40 -28
  59. data/lib/translatomatic/database.rb +127 -110
  60. data/lib/translatomatic/define_options.rb +4 -5
  61. data/lib/translatomatic/escaped_unicode.rb +86 -76
  62. data/lib/translatomatic/extractor.rb +5 -2
  63. data/lib/translatomatic/extractor/base.rb +12 -12
  64. data/lib/translatomatic/extractor/ruby.rb +7 -6
  65. data/lib/translatomatic/file_translator.rb +101 -244
  66. data/lib/translatomatic/flattenation.rb +39 -0
  67. data/lib/translatomatic/http.rb +13 -0
  68. data/lib/translatomatic/http/client.rb +144 -0
  69. data/lib/translatomatic/http/exception.rb +43 -0
  70. data/lib/translatomatic/http/file_param.rb +27 -0
  71. data/lib/translatomatic/http/param.rb +37 -0
  72. data/lib/translatomatic/http/request.rb +91 -0
  73. data/lib/translatomatic/i18n.rb +43 -0
  74. data/lib/translatomatic/locale.rb +71 -59
  75. data/lib/translatomatic/logger.rb +43 -28
  76. data/lib/translatomatic/metadata.rb +58 -0
  77. data/lib/translatomatic/model.rb +4 -2
  78. data/lib/translatomatic/model/locale.rb +5 -5
  79. data/lib/translatomatic/model/text.rb +5 -5
  80. data/lib/translatomatic/option.rb +57 -34
  81. data/lib/translatomatic/path_utils.rb +126 -0
  82. data/lib/translatomatic/progress_updater.rb +13 -16
  83. data/lib/translatomatic/provider.rb +101 -0
  84. data/lib/translatomatic/provider/base.rb +136 -0
  85. data/lib/translatomatic/provider/frengly.rb +55 -0
  86. data/lib/translatomatic/provider/google.rb +78 -0
  87. data/lib/translatomatic/provider/google_web.rb +50 -0
  88. data/lib/translatomatic/provider/microsoft.rb +144 -0
  89. data/lib/translatomatic/provider/my_memory.rb +75 -0
  90. data/lib/translatomatic/provider/yandex.rb +61 -0
  91. data/lib/translatomatic/resource_file.rb +59 -53
  92. data/lib/translatomatic/resource_file/base.rb +171 -237
  93. data/lib/translatomatic/resource_file/csv.rb +176 -24
  94. data/lib/translatomatic/resource_file/html.rb +21 -42
  95. data/lib/translatomatic/resource_file/key_value_support.rb +117 -0
  96. data/lib/translatomatic/resource_file/markdown.rb +36 -38
  97. data/lib/translatomatic/resource_file/plist.rb +121 -126
  98. data/lib/translatomatic/resource_file/po.rb +104 -82
  99. data/lib/translatomatic/resource_file/properties.rb +48 -77
  100. data/lib/translatomatic/resource_file/properties.treetop +87 -0
  101. data/lib/translatomatic/resource_file/resw.rb +56 -41
  102. data/lib/translatomatic/resource_file/subtitle.rb +86 -54
  103. data/lib/translatomatic/resource_file/text.rb +18 -18
  104. data/lib/translatomatic/resource_file/xcode_strings.rb +32 -63
  105. data/lib/translatomatic/resource_file/xcode_strings.treetop +85 -0
  106. data/lib/translatomatic/resource_file/xml.rb +94 -81
  107. data/lib/translatomatic/resource_file/yaml.rb +54 -68
  108. data/lib/translatomatic/retry_executor.rb +37 -0
  109. data/lib/translatomatic/slurp.rb +32 -0
  110. data/lib/translatomatic/string_batcher.rb +50 -0
  111. data/lib/translatomatic/string_escaping.rb +61 -0
  112. data/lib/translatomatic/text.rb +263 -0
  113. data/lib/translatomatic/text_collection.rb +66 -0
  114. data/lib/translatomatic/tmx.rb +5 -3
  115. data/lib/translatomatic/tmx/document.rb +107 -82
  116. data/lib/translatomatic/tmx/translation_unit.rb +19 -18
  117. data/lib/translatomatic/translation.rb +8 -28
  118. data/lib/translatomatic/translation/collection.rb +199 -0
  119. data/lib/translatomatic/translation/fetcher.rb +123 -0
  120. data/lib/translatomatic/translation/munging.rb +112 -0
  121. data/lib/translatomatic/translation/result.rb +50 -0
  122. data/lib/translatomatic/translation/sharer.rb +32 -0
  123. data/lib/translatomatic/translation/stats.rb +44 -0
  124. data/lib/translatomatic/translator.rb +91 -88
  125. data/lib/translatomatic/type_cast.rb +63 -0
  126. data/lib/translatomatic/util.rb +37 -33
  127. data/lib/translatomatic/version.rb +2 -2
  128. data/translatomatic.gemspec +57 -46
  129. metadata +136 -59
  130. data/lib/translatomatic/http_request.rb +0 -162
  131. data/lib/translatomatic/string.rb +0 -188
  132. data/lib/translatomatic/translation_result.rb +0 -86
  133. data/lib/translatomatic/translation_stats.rb +0 -31
  134. data/lib/translatomatic/translator/base.rb +0 -128
  135. data/lib/translatomatic/translator/frengly.rb +0 -62
  136. data/lib/translatomatic/translator/google.rb +0 -37
  137. data/lib/translatomatic/translator/microsoft.rb +0 -41
  138. data/lib/translatomatic/translator/my_memory.rb +0 -68
  139. data/lib/translatomatic/translator/yandex.rb +0 -56
@@ -1,302 +1,11 @@
1
- require 'singleton'
2
-
3
- # Translatomatic configuration.
4
- # Configuration settings may be specified in the following locations:
5
- #
6
- # - environment variables
7
- # - user configuration file $HOME/.translatomatic/config.yml
8
- # - project configuration file $PROJECT/.translatomatic/config.yml
9
- # - command line options
10
- #
11
- # Settings are read in the order given above, with last setting found
12
- # taking precedence over values read earlier.
13
-
14
- class Translatomatic::Config
15
-
16
- # @return [Logger] The logger instance
17
- attr_accessor :logger
18
-
19
- # @return [String] The default locale
20
- attr_accessor :default_locale
21
-
22
- # @return [String] The path to the user settings file
23
- attr_reader :user_settings_path
24
-
25
- # @return [String] The path to the project settings file
26
- attr_reader :project_settings_path
27
-
28
- # Change a configuration setting. The default context is project level
29
- # if a project configuration file exists, otherwise user level.
30
- # @param key [String] configuration key
31
- # @param value [String] new value for the configuration
32
- # @param context [Symbol] configuration context
33
- # @return [String] the new value
34
- def set(key, value, context = nil)
35
- key = check_valid_key(key)
36
- option = option(key)
37
- raise t("config.command_line_only") if option.command_line_only
38
- context ||= default_context
39
- context = :user if option.user_context_only || key.to_s.match(/api_key/)
40
- context = check_valid_context(context)
41
- @settings[context][key] = cast(value, option.type)
42
- save
43
- value
44
- end
45
-
46
- # Get a configuration setting
47
- # @param key [String] configuration key
48
- # @param context [Symbol] configuration context. May be nil.
49
- # @return [String] The configuration value. If context is nil, returns the
50
- # effective value by precedence, otherwise it returns the setting for
51
- # the given context.
52
- def get(key, context = nil)
53
- key = check_valid_key(key)
54
- option = option(key)
55
- value = option.default # set to default value
56
-
57
- if context.nil?
58
- # find the first setting in the following order
59
- CONTEXTS.each do |ctx|
60
- if @settings[ctx].include?(key)
61
- value = @settings[ctx][key]
62
- break
63
- end
64
- end
65
- else
66
- # context is set
67
- context = check_valid_context(context)
68
- if @settings[context].include?(key)
69
- value = @settings[context][key]
70
- end
71
- end
72
-
73
- # cast value to expected type
74
- cast(value, option.type)
75
- end
76
-
77
- # Remove a configuration setting
78
- # @param key [String] configuration key to remove
79
- # @param context [Symbol] configuration context
80
- # @return [void]
81
- def remove(key, context = nil)
82
- key = check_valid_key(key)
83
- context ||= default_context
84
- context = check_valid_context(context)
85
- @settings[context].delete(key)
86
- save
87
- end
88
-
89
- # Test if configuration includes the given key
90
- # @param key [String] configuration key
91
- # @return [String] The configuration value. If context is nil, checks
92
- # all contexts.
93
- # @return [boolean] true if the configuration key is set
94
- def include?(key, context = nil)
95
- key = check_valid_key(key)
96
- if context.nil?
97
- CONTEXTS.each do |ctx|
98
- return true if @settings[ctx].include?(key)
99
- end
100
- false
101
- else
102
- context = check_valid_context(context)
103
- @settings[context].include?(key)
104
- end
105
- end
106
-
107
- # Save configuration settings
108
- def save
109
- save_context(:user, @user_settings_path)
110
- save_context(:project, @project_settings_path)
111
- end
112
-
113
- # Load configuration from the config file(s)
114
- def load
115
- load_context_env
116
- load_context(:user, @user_settings_path)
117
- load_context(:project, @project_settings_path)
118
- end
119
-
120
- # Reset all configuration to the defaults
121
- def reset
122
- @settings = {}
123
- CONTEXTS.each { |context| @settings[context] = {} }
124
- end
125
-
126
- # @return [Array<Translatomatic::Option] all available options
127
- def self.options
128
- self.config_options.values
129
- end
130
-
131
- # The project path is found by searching for a '.translatomatic' directory
132
- # that is not within the user home directory. The search ascends upwards
133
- # from the current working directory.
134
- # @return The path to the current project, or nil if the current project
135
- # path is unknown.
136
- def project_path
137
- if @project_settings_path
138
- File.realpath(File.join(File.dirname(@project_settings_path), ".."))
139
- else
140
- nil
141
- end
142
- end
143
-
144
- private
145
-
146
- include Translatomatic::Util
147
-
148
- SETTINGS_DIR = ".translatomatic"
149
- SETTINGS_PATH = File.join(SETTINGS_DIR, "config.yml")
150
- USER_SETTINGS_PATH = File.join(Dir.home, SETTINGS_PATH)
151
-
152
- # valid context list in order of precedence
153
- CONTEXTS = [:project, :user, :env]
154
-
155
- def save_context(context, path)
156
- return unless path
157
- FileUtils.mkdir_p(File.dirname(path))
158
- File.open(path, "w") { |f| f.puts @settings[context].to_yaml }
159
- end
160
-
161
- # load configuration from the yaml file at path
162
- def load_context(context, path)
163
- return unless path && File.exist?(path)
164
- config = YAML.load_file(path) || {}
165
- load_context_config(context, config)
166
- end
167
-
168
- # load configuration from a hash
169
- def load_context_config(context, config = {})
170
- config.each do |key, value|
171
- key = key.to_sym
172
- next unless valid_key?(key)
173
- @settings[context][key] = value
174
- end
175
- end
176
-
177
- # load configuration from environment variables
178
- def load_context_env
179
- config = {}
180
- self.class.options.each do |option|
181
- if option.env_name && ENV.include?(option.env_name)
182
- config[option.name] = ENV[option.env_name]
183
- end
184
- end
185
- load_context_config(:env, config)
186
- end
187
-
188
- # @return [Hash<String,Translatomatic::Option>] options
189
- def self.config_options
190
- @config_options ||= begin
191
- # create mapping from option name to option object
192
- map = {}
193
- sources = [
194
- Translatomatic::CLI::CommonOptions,
195
- Translatomatic::CLI::Translate,
196
- Translatomatic::CLI::Config,
197
- Translatomatic::Translator.modules,
198
- Translatomatic::Database,
199
- Translatomatic::Converter
200
- ]
201
- sources.each do |source|
202
- source_options = Translatomatic::Option.options_from_object(source)
203
- source_options.each do |sopt|
204
- optname = sopt.name.to_sym
205
- raise "#{optname} already defined" if map.include?(optname)
206
- map[optname] = sopt
207
- end
208
- end
209
- map
210
- end
211
- end
212
-
213
- def cast(value, type)
214
- value = value[0] if value.kind_of?(Array) && type != :array
215
-
216
- case type
217
- when :boolean
218
- return true if ["true", "t", "yes", "on"].include?(value)
219
- return false if ["false", "f", "no", "off"].include?(value)
220
- return value ? true : false
221
- when :string
222
- value = value[0] if value.kind_of?(Array)
223
- return value.nil? ? value : value.to_s
224
- when :array
225
- if value.nil?
226
- value = []
227
- else
228
- value = [value] unless value.kind_of?(Array)
229
- value = value.collect { |i| i.split(/[, ]/) }.flatten.compact
230
- end
231
- end
232
- value
233
- end
234
-
235
- def option(key)
236
- self.class.config_options[key.to_sym]
237
- end
238
-
239
- def check_valid_key(key)
240
- key = key ? key.to_sym : nil
241
- raise t("config.invalid_key", key: key) unless valid_key?(key)
242
- key
243
- end
244
-
245
- def check_valid_context(context)
246
- context = context ? context.to_sym : nil
247
- valid = valid_context?(context)
248
- raise t("config.invalid_context", context: context) unless valid
249
- context
250
- end
251
-
252
- def valid_key?(key)
253
- self.class.config_options.include?(key)
254
- end
255
-
256
- def valid_context?(context)
257
- CONTEXTS.include?(context)
258
- end
259
-
260
- # override user settings path, used for testing
261
- def user_settings_path=(path)
262
- reset
263
- @user_settings_path = path
264
- load
265
- end
266
-
267
- # override project settings path, used for testing
268
- def project_settings_path=(path)
269
- reset
270
- @project_settings_path = path
271
- load
272
- end
273
-
274
- def initialize
275
- @logger = Translatomatic::Logger.new
276
- lang = (ENV['LANG'] || '').split(/\./)[0]
277
- @default_locale = Translatomatic::Locale.parse(lang).language || "en"
278
- @user_settings_path = USER_SETTINGS_PATH
279
- @project_settings_path = find_project_settings
280
- reset
281
- load
282
- end
283
-
284
- # find a .translatomatic directory working upwards from current directory
285
- def find_project_settings
286
- found = nil
287
- Pathname.new(Dir.pwd).ascend do |v|
288
- if found.nil?
289
- settings_path = v + SETTINGS_DIR
290
- # set found if we found a .translatomatic directory
291
- found = v + SETTINGS_PATH if settings_path.directory?
292
- end
293
- end
294
- # if found path is the same as the user settings path, don't use it
295
- found && found.to_s == @user_settings_path ? nil : found
296
- end
297
-
298
- def default_context
299
- # use project context if we have project configuration
300
- @project_settings_path ? :project : :user
301
- end
1
+ module Translatomatic
2
+ # Configuration classes
3
+ module Config; end
302
4
  end
5
+
6
+ require 'translatomatic/config/options'
7
+ require 'translatomatic/config/location_settings'
8
+ require 'translatomatic/config/selector'
9
+ require 'translatomatic/config/settings'
10
+ require 'translatomatic/config/files'
11
+ require 'translatomatic/config/display'
@@ -0,0 +1,78 @@
1
+ module Translatomatic
2
+ module Config
3
+ # Methods for displaying configuration
4
+ class Display
5
+ attr_reader :options
6
+
7
+ def initialize(opts = {})
8
+ @options = opts
9
+ @config_params = opts[:config_params]
10
+ raise t('config.one_at_a_time') if opts[:user] && opts[:project]
11
+ end
12
+
13
+ # @return [Array<Array<String>>] Configuration table
14
+ def config_table_body
15
+ columns = options[:columns] || []
16
+ rows = config_table_rows(columns)
17
+ if rows.present?
18
+ headings = columns.collect { |i| CONFIG_HEADING_MAP[i] }
19
+ rows = add_table_heading(rows, headings)
20
+ end
21
+ rows
22
+ end
23
+
24
+ private
25
+
26
+ include Translatomatic::Util
27
+
28
+ CONFIG_HEADING_MAP = {
29
+ key: t('config.heading.name'),
30
+ type: t('config.heading.type'),
31
+ value: t('config.heading.value'),
32
+ desc: t('config.heading.desc')
33
+ }.freeze
34
+
35
+ CONFIG_VALUE_MAP = {
36
+ key: :name,
37
+ type: :type_name,
38
+ desc: :description
39
+ }.freeze
40
+
41
+ def display_option?(option)
42
+ key = option.name.to_s
43
+ have_conf = Translatomatic.config.include?(key, @options)
44
+ return false if option.command_line_only
45
+ return false if options[:skip_blanks] && !have_conf
46
+ true
47
+ end
48
+
49
+ def add_table_heading(rows, headings)
50
+ underscores = headings.collect { |i| i.gsub(/\w/, '=') }
51
+ [headings, underscores] + rows
52
+ end
53
+
54
+ def config_table_rows(columns)
55
+ opts = Options.options.values.select { |i| display_option?(i) }
56
+ rows = opts.collect { |i| option_to_table_row(i, columns) }
57
+ rows.sort_by { |i| i[0] }
58
+ rows
59
+ end
60
+
61
+ def option_to_table_row(option, columns)
62
+ columns.collect { |i| config_table_column_value(option, i) }
63
+ end
64
+
65
+ def option_value(option)
66
+ value = Translatomatic.config.get(option.name, @options)
67
+ value.nil? ? '-' : value
68
+ end
69
+
70
+ def config_table_column_value(option, column)
71
+ return option_value(option) if column == :value
72
+ value_method = CONFIG_VALUE_MAP[column]
73
+ return option.send(value_method).to_s if value_method
74
+ raise "unhandled column type: #{column}"
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,60 @@
1
+ module Translatomatic
2
+ module Config
3
+ # Configuration file operations.
4
+ # Configuration settings may be specified in the following locations:
5
+ #
6
+ # - environment variables
7
+ # - user configuration file $HOME/.translatomatic/config.yml
8
+ # - project configuration file $PROJECT/.translatomatic/config.yml
9
+ # - command line options
10
+ #
11
+ # Settings are read in the order given above, with last setting found
12
+ # taking precedence over values read earlier.
13
+ class Files
14
+ class << self
15
+ # Save location settings to file
16
+ # @param settings [LocationSettings] Location settings object
17
+ # @return [void]
18
+ def save(settings)
19
+ return unless settings && settings.path
20
+ config_path = File.join(settings.path, CONFIG_PATH)
21
+ FileUtils.mkdir_p(File.dirname(config_path))
22
+ File.write(config_path, settings.to_yaml)
23
+ end
24
+
25
+ # load configuration from the yaml file at path.
26
+ # @param path to config file
27
+ # @return [LocationSettings] Location settings object
28
+ def load(path, options = {})
29
+ return nil unless path
30
+ config_path = File.join(path, CONFIG_PATH)
31
+ options = options.merge(path: path)
32
+ return new_settings({}, options) unless File.exist?(config_path)
33
+ config = YAML.load_file(config_path) || {}
34
+ new_settings(config, options)
35
+ end
36
+
37
+ # find a project directory working upwards from current directory.
38
+ # stop at user path.
39
+ def find_project(user_path)
40
+ user_path = user_path.to_s
41
+ Pathname.new(Dir.pwd).ascend do |v|
42
+ return nil if v.to_s == user_path || user_path.start_with?(v.to_s)
43
+ config_path = v + CONFIG_DIR
44
+ return v if config_path.directory?
45
+ end
46
+ nil
47
+ end
48
+
49
+ private
50
+
51
+ CONFIG_DIR = '.translatomatic'.freeze
52
+ CONFIG_PATH = File.join(CONFIG_DIR, 'config.yml').freeze
53
+
54
+ def new_settings(config, options = {})
55
+ LocationSettings.new(config.deep_symbolize_keys, options)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end