translatomatic 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
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,8 +1,8 @@
1
- module Translatomatic::CLI; end
2
-
3
- require 'translatomatic/cli/common_options'
4
- require 'translatomatic/cli/base'
5
- require 'translatomatic/cli/translate'
6
- require 'translatomatic/cli/database'
7
- require 'translatomatic/cli/config'
8
- require 'translatomatic/cli/main'
1
+ module Translatomatic::CLI; end
2
+
3
+ require 'translatomatic/cli/common_options'
4
+ require 'translatomatic/cli/base'
5
+ require 'translatomatic/cli/translate'
6
+ require 'translatomatic/cli/database'
7
+ require 'translatomatic/cli/config'
8
+ require 'translatomatic/cli/main'
@@ -1,155 +1,302 @@
1
- require 'singleton'
2
-
3
- # Translatomatic configuration
4
- class Translatomatic::Config
5
- include Singleton
6
- include Translatomatic::Util
7
-
8
- # @return [Logger] The logger instance
9
- attr_accessor :logger
10
-
11
- # @return [String] The default locale
12
- attr_accessor :default_locale
13
-
14
- # Change a configuration setting
15
- # @param key [String] configuration key
16
- # @param value [String] new value for the configuration
17
- # @return [String] the new value
18
- def set(key, value)
19
- key = check_valid_key(key)
20
- @settings[key] = value
21
- save
22
- end
23
-
24
- # Get a configuration setting
25
- # @param key [String] configuration key
26
- # @return [String] configuration value
27
- def get(key)
28
- key = check_valid_key(key)
29
- option = option(key)
30
- env_name = option.name.to_s.upcase
31
- if @settings.include?(key)
32
- cast(@settings[key], option.type)
33
- elsif option.use_env && ENV.include?(env_name)
34
- cast(ENV[env_name], option.type)
35
- else
36
- option.default
37
- end
38
- end
39
-
40
- # Remove a configuration setting
41
- # @param key [String] configuration key to remove
42
- # @return [void]
43
- def remove(key)
44
- key = check_valid_key(key)
45
- @settings.delete(key)
46
- save
47
- end
48
-
49
- # Test if configuration includes the given key
50
- # @param key [String] configuration key
51
- # @return [boolean] true if the configuration key is set
52
- def include?(key)
53
- key = check_valid_key(key)
54
- @settings.include?(key)
55
- end
56
-
57
- # @return [Hash<String,String>] configuration settings
58
- def settings
59
- @settings.dup
60
- end
61
-
62
- # Save configuration settings
63
- def save
64
- FileUtils.mkdir_p(File.dirname(@settings_path))
65
- File.open(@settings_path, "w") { |f| f.puts @settings.to_yaml }
66
- end
67
-
68
- # Load configuration from the config file
69
- def load
70
- @settings = YAML.load_file(@settings_path) if File.exist?(@settings_path)
71
- @settings ||= {}
72
- @settings.delete_if { |i| !valid_key?(i) }
73
- @settings
74
- end
75
-
76
- # @return [Array<Translatomatic::Option] all available options
77
- def self.options
78
- self.config_options.values
79
- end
80
-
81
- private
82
-
83
- SETTINGS_PATH = File.join(Dir.home, ".translatomatic", "config.yml")
84
-
85
- # @return [Hash<String,Translatomatic::Option>] options
86
- def self.config_options
87
- @config_options ||= begin
88
- # create mapping from option name to option object
89
- map = {}
90
- sources = [
91
- Translatomatic::CLI::CommonOptions,
92
- Translatomatic::CLI::Translate,
93
- Translatomatic::Translator.modules,
94
- Translatomatic::Database,
95
- Translatomatic::Converter
96
- ]
97
- sources.each do |source|
98
- source_options = Translatomatic::Option.options_from_object(source)
99
- source_options.each do |sopt|
100
- optname = sopt.name.to_s
101
- raise "#{optname} already defined" if map.include?(optname)
102
- map[optname] = sopt
103
- end
104
- end
105
- map
106
- end
107
- end
108
-
109
- def cast(value, type)
110
- case type
111
- when :boolean
112
- return true if ["true", "t", "yes", "on"].include?(value)
113
- return false if ["false", "f", "no", "off"].include?(value)
114
- return value ? true : false
115
- when :string
116
- return value.nil? ? value : value.to_s
117
- when :array
118
- unless value.nil?
119
- value = [value] unless value.kind_of?(Array)
120
- value = value.collect { |i| i.split(/[, ]/) }.flatten.compact
121
- end
122
- end
123
- value
124
- end
125
-
126
- def option(key)
127
- self.class.config_options[key.to_s]
128
- end
129
-
130
- def check_valid_key(key)
131
- key = key.to_s
132
- raise t("config.invalid_key", key: key) unless valid_key?(key)
133
- key
134
- end
135
-
136
- def valid_key?(key)
137
- self.class.config_options.include?(key.to_s)
138
- end
139
-
140
- # override settings path, used for testing
141
- def settings_path=(path)
142
- @settings = {}
143
- @settings_path = path
144
- load
145
- end
146
-
147
- def initialize
148
- @logger = Translatomatic::Logger.new
149
- lang = (ENV['LANG'] || '').split(/\./)[0]
150
- @default_locale = Translatomatic::Locale.parse(lang).language || "en"
151
- @settings_path = SETTINGS_PATH
152
- load
153
- end
154
-
155
- end
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
302
+ end