translatomatic 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.translatomatic/config.yml +18 -0
- data/.travis.yml +33 -33
- data/Gemfile +6 -4
- data/README.de.md +53 -18
- data/README.es.md +55 -20
- data/README.fr.md +54 -19
- data/README.it.md +58 -23
- data/README.ja.md +54 -19
- data/README.ko.md +58 -23
- data/README.md +167 -141
- data/README.ms.md +51 -16
- data/README.pt.md +58 -23
- data/README.ru.md +53 -18
- data/README.sv.md +53 -18
- data/README.zh.md +53 -18
- data/bin/translatomatic +6 -6
- data/bin/travis +24 -26
- data/config/locales/translatomatic/de.yml +22 -11
- data/config/locales/translatomatic/en.yml +21 -12
- data/config/locales/translatomatic/es.yml +22 -11
- data/config/locales/translatomatic/fr.yml +22 -12
- data/config/locales/translatomatic/it.yml +22 -11
- data/config/locales/translatomatic/ja.yml +22 -11
- data/config/locales/translatomatic/ko.yml +22 -11
- data/config/locales/translatomatic/ms.yml +22 -11
- data/config/locales/translatomatic/pt.yml +22 -11
- data/config/locales/translatomatic/ru.yml +22 -11
- data/config/locales/translatomatic/sv.yml +22 -11
- data/config/locales/translatomatic/zh.yml +22 -11
- data/db/migrate/201712170000_initial.rb +25 -25
- data/lib/translatomatic/cli/base.rb +81 -73
- data/lib/translatomatic/cli/config.rb +110 -81
- data/lib/translatomatic/cli/main.rb +85 -72
- data/lib/translatomatic/cli/translate.rb +141 -106
- data/lib/translatomatic/cli.rb +8 -8
- data/lib/translatomatic/config.rb +302 -155
- data/lib/translatomatic/converter.rb +28 -260
- data/lib/translatomatic/database.rb +134 -134
- data/lib/translatomatic/define_options.rb +22 -0
- data/lib/translatomatic/escaped_unicode.rb +0 -0
- data/lib/translatomatic/extractor/base.rb +16 -16
- data/lib/translatomatic/extractor/ruby.rb +6 -6
- data/lib/translatomatic/extractor.rb +5 -5
- data/lib/translatomatic/file_translator.rb +269 -0
- data/lib/translatomatic/http_request.rb +162 -162
- data/lib/translatomatic/locale.rb +76 -76
- data/lib/translatomatic/logger.rb +23 -23
- data/lib/translatomatic/model/locale.rb +25 -25
- data/lib/translatomatic/model/text.rb +19 -19
- data/lib/translatomatic/model.rb +1 -1
- data/lib/translatomatic/option.rb +37 -41
- data/lib/translatomatic/progress_updater.rb +13 -13
- data/lib/translatomatic/resource_file/base.rb +269 -192
- data/lib/translatomatic/resource_file/csv.rb +37 -0
- data/lib/translatomatic/resource_file/html.rb +54 -47
- data/lib/translatomatic/resource_file/markdown.rb +50 -55
- data/lib/translatomatic/resource_file/plist.rb +153 -19
- data/lib/translatomatic/resource_file/po.rb +107 -0
- data/lib/translatomatic/resource_file/properties.rb +91 -90
- data/lib/translatomatic/resource_file/resw.rb +50 -30
- data/lib/translatomatic/resource_file/subtitle.rb +75 -0
- data/lib/translatomatic/resource_file/text.rb +24 -30
- data/lib/translatomatic/resource_file/xcode_strings.rb +75 -80
- data/lib/translatomatic/resource_file/xml.rb +98 -91
- data/lib/translatomatic/resource_file/yaml.rb +94 -116
- data/lib/translatomatic/resource_file.rb +87 -78
- data/lib/translatomatic/string.rb +188 -188
- data/lib/translatomatic/tmx/document.rb +99 -99
- data/lib/translatomatic/translation_result.rb +63 -63
- data/lib/translatomatic/{converter_stats.rb → translation_stats.rb} +17 -17
- data/lib/translatomatic/translator/base.rb +1 -1
- data/lib/translatomatic/translator/google.rb +2 -0
- data/lib/translatomatic/translator.rb +10 -2
- data/lib/translatomatic/util.rb +45 -45
- data/lib/translatomatic/version.rb +7 -7
- data/lib/translatomatic.rb +52 -49
- data/translatomatic.gemspec +3 -2
- metadata +25 -5
data/lib/translatomatic/cli.rb
CHANGED
@@ -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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
# @
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
# @
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
def
|
53
|
-
key = check_valid_key(key)
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
end
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
def
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
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
|