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.
- 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
@@ -1,268 +1,36 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
# language to another.
|
4
|
-
class Translatomatic::Converter
|
5
|
-
|
6
|
-
# @return [Array<Translatomatic::Model::Text>] A list of translations saved to the database
|
7
|
-
attr_reader :db_translations
|
8
|
-
|
9
|
-
# Create a converter to translate files
|
10
|
-
#
|
11
|
-
# @param options [Hash<Symbol,Object>] converter and/or translator options.
|
12
|
-
def initialize(options = {})
|
13
|
-
@dry_run = options[:dry_run]
|
14
|
-
@listener = options[:listener]
|
15
|
-
@translators = Translatomatic::Translator.resolve(options[:translator], options)
|
16
|
-
raise t("converter.translator_required") if @translators.empty?
|
17
|
-
@translators.each { |i| i.listener = @listener } if @listener
|
18
|
-
|
19
|
-
# use database by default if we're connected to a database
|
20
|
-
use_db = options.include?(:use_database) ? options[:use_database] : true
|
21
|
-
@use_db = use_db && ActiveRecord::Base.connected?
|
22
|
-
log.debug(t("converter.database_disabled")) unless @use_db
|
1
|
+
# Converts files from one format to another
|
2
|
+
class Translatomatic::Converter
|
23
3
|
|
24
|
-
|
25
|
-
@
|
4
|
+
def initialize(options = {})
|
5
|
+
@options = options
|
26
6
|
end
|
27
|
-
|
28
|
-
# @return [Translatomatic::ConverterStats] Translation statistics
|
29
|
-
def stats
|
30
|
-
@stats ||= Translatomatic::ConverterStats.new(@translations)
|
31
|
-
end
|
32
|
-
|
33
|
-
# Translate properties of source_file to the target locale.
|
34
|
-
# Does not write changes to disk.
|
35
|
-
#
|
36
|
-
# @param file [String, Translatomatic::ResourceFile] File to translate
|
37
|
-
# @param to_locale [String] The target locale, e.g. "fr"
|
38
|
-
# @return [Translatomatic::ResourceFile] The translated resource file
|
39
|
-
def translate(file, to_locale)
|
40
|
-
file = resource_file(file)
|
41
|
-
to_locale = parse_locale(to_locale)
|
42
7
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
# Translates a resource file and writes results to a target resource file.
|
64
|
-
# The path of the target resource file is automatically determined.
|
65
|
-
#
|
66
|
-
# @param source [Translatomatic::ResourceFile] The source
|
67
|
-
# @param to_locale [String] The target locale, e.g. "fr"
|
68
|
-
# @return [Translatomatic::ResourceFile] The translated resource file
|
69
|
-
def translate_to_file(source, to_locale)
|
70
|
-
# Automatically determines the target filename based on target locale.
|
71
|
-
source = resource_file(source)
|
72
|
-
target = Translatomatic::ResourceFile.load(source.path)
|
73
|
-
target.path = source.locale_path(to_locale)
|
74
|
-
|
75
|
-
log.info(t("converter.translating", source: source,
|
76
|
-
source_locale: source.locale, target: target, target_locale: to_locale))
|
77
|
-
translate(target, to_locale)
|
78
|
-
unless @dry_run
|
79
|
-
target.path.parent.mkpath
|
80
|
-
target.save
|
81
|
-
end
|
82
|
-
target
|
83
|
-
end
|
84
|
-
|
85
|
-
private
|
86
|
-
|
87
|
-
include Translatomatic::Util
|
88
|
-
include Translatomatic::DefineOptions
|
89
|
-
|
90
|
-
define_options(
|
91
|
-
{ name: :dry_run, type: :boolean, aliases: "-n",
|
92
|
-
desc: t("converter.dry_run")
|
93
|
-
},
|
94
|
-
{ name: :use_database, type: :boolean, default: true,
|
95
|
-
desc: t("converter.use_database")
|
96
|
-
}
|
97
|
-
)
|
98
|
-
|
99
|
-
def each_translator(result)
|
100
|
-
@translators.each do |translator|
|
101
|
-
break if result.untranslated.empty?
|
102
|
-
@current_translator = translator
|
103
|
-
yield
|
104
|
-
end
|
8
|
+
# Convert a resource file from one format to another.
|
9
|
+
# @param source [String] Path to source file. File must exist
|
10
|
+
# @param target [String] Path to target file. File will be created
|
11
|
+
# or overwritten if it already exists.
|
12
|
+
# @return [Translatomatic::ResourceFile] The converted file.
|
13
|
+
def convert(source, target)
|
14
|
+
source_path = Pathname.new(source.to_s)
|
15
|
+
target_path = Pathname.new(target.to_s)
|
16
|
+
raise t("file.not_found", file: source.to_s) unless source_path.file?
|
17
|
+
raise t("file.directory", file: target.to_s) if target_path.directory?
|
18
|
+
|
19
|
+
source_file = load_file(source_path)
|
20
|
+
target_file = load_file(target_path)
|
21
|
+
|
22
|
+
# copy properties from source file to target
|
23
|
+
target_file.properties = source_file.properties
|
24
|
+
target_file.save(target_path, @options)
|
25
|
+
target_file
|
105
26
|
end
|
106
27
|
|
107
|
-
|
108
|
-
# If variable names cannot be restored, sets the translation result to nil.
|
109
|
-
# @param result [Translatomatic::TranslationResult] translation result
|
110
|
-
# @param translation [Translatomatic::Translation] translation
|
111
|
-
# @return [void]
|
112
|
-
def restore_variables(result, translation)
|
113
|
-
file = result.file
|
114
|
-
return unless file.supports_variable_interpolation?
|
115
|
-
|
116
|
-
# find variables in the original string
|
117
|
-
variables = string_variables(translation.original, file.locale, file)
|
118
|
-
# find variables in the translated string
|
119
|
-
translated_variables = string_variables(translation.result, result.to_locale, file)
|
120
|
-
|
121
|
-
if variables.length == translated_variables.length
|
122
|
-
# we can restore variables. sort by largest offset first.
|
123
|
-
# not using translation() method as that adds to @translations hash.
|
124
|
-
conversions = variables.zip(translated_variables).collect {
|
125
|
-
|v1, v2| Translatomatic::Translation.new(v1, v2)
|
126
|
-
}
|
127
|
-
conversions.sort_by! { |t| -t.original.offset }
|
128
|
-
conversions.each do |conversion|
|
129
|
-
v1 = conversion.original
|
130
|
-
v2 = conversion.result
|
131
|
-
translation.result[v2.offset, v2.length] = v1.value
|
132
|
-
end
|
133
|
-
else
|
134
|
-
# unable to restore interpolated variable names
|
135
|
-
log.debug("#{@current_translator.name}: unable to restore variables: #{translation.result}")
|
136
|
-
translation.result = nil # mark result as invalid
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
def string_variables(value, locale, file)
|
141
|
-
string(value, locale).substrings(file.variable_regex)
|
142
|
-
end
|
143
|
-
|
144
|
-
def resource_file(path)
|
145
|
-
if path.kind_of?(Translatomatic::ResourceFile::Base)
|
146
|
-
path
|
147
|
-
else
|
148
|
-
file = Translatomatic::ResourceFile.load(path)
|
149
|
-
raise t("converter.file_unsupported", file: path) unless file
|
150
|
-
file
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
# update result with translations from the database.
|
155
|
-
def translate_properties_with_db(result)
|
156
|
-
db_texts = []
|
157
|
-
unless database_disabled?
|
158
|
-
translations = []
|
159
|
-
untranslated = hashify(result.untranslated)
|
160
|
-
db_texts = find_database_translations(result, result.untranslated.to_a)
|
161
|
-
db_texts.each do |db_text|
|
162
|
-
from_text = db_text.from_text.value
|
163
|
-
if untranslated[from_text]
|
164
|
-
translation = translation(untranslated[from_text], db_text.value, true)
|
165
|
-
restore_variables(result, translation)
|
166
|
-
translations << translation
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
result.update_strings(translations)
|
171
|
-
@listener.translated_texts(db_texts) if @listener
|
172
|
-
end
|
173
|
-
db_texts
|
174
|
-
end
|
175
|
-
|
176
|
-
# update result with translations from the translator.
|
177
|
-
def translate_properties_with_translator(result)
|
178
|
-
untranslated = result.untranslated.to_a.select { |i| translatable?(i) }
|
179
|
-
translated = []
|
180
|
-
if !untranslated.empty? && !@dry_run
|
181
|
-
untranslated_strings = untranslated.collect { |i| i.to_s }
|
182
|
-
log.debug("translating: #{untranslated_strings}")
|
183
|
-
translated = @current_translator.translate(untranslated_strings,
|
184
|
-
result.from_locale, result.to_locale
|
185
|
-
)
|
28
|
+
private
|
186
29
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
restore_variables(result, translation)
|
192
|
-
translations << translation
|
193
|
-
end
|
194
|
-
|
195
|
-
result.update_strings(translations)
|
196
|
-
unless database_disabled?
|
197
|
-
save_database_translations(result, translations)
|
198
|
-
end
|
199
|
-
end
|
200
|
-
translated
|
30
|
+
def load_file(path)
|
31
|
+
file = Translatomatic::ResourceFile.load(path)
|
32
|
+
raise t("file.unsupported", file: path) unless file
|
33
|
+
file
|
201
34
|
end
|
202
35
|
|
203
|
-
|
204
|
-
translator = @current_translator.name
|
205
|
-
t = Translatomatic::Translation.new(from, to, translator, from_database)
|
206
|
-
@translations[from] = t
|
207
|
-
t
|
208
|
-
end
|
209
|
-
|
210
|
-
def database_disabled?
|
211
|
-
!@use_db
|
212
|
-
end
|
213
|
-
|
214
|
-
def parse_locale(locale)
|
215
|
-
Translatomatic::Locale.parse(locale)
|
216
|
-
end
|
217
|
-
|
218
|
-
def translatable?(string)
|
219
|
-
# don't translate numbers
|
220
|
-
!string.empty? && !string.match(/^[\d,]+$/)
|
221
|
-
end
|
222
|
-
|
223
|
-
def save_database_translations(result, translations)
|
224
|
-
ActiveRecord::Base.transaction do
|
225
|
-
from = db_locale(result.from_locale)
|
226
|
-
to = db_locale(result.to_locale)
|
227
|
-
translations.each do |translation|
|
228
|
-
next if translation.result.nil? # skip invalid translations
|
229
|
-
save_database_translation(from, to, translation)
|
230
|
-
end
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
def save_database_translation(from_locale, to_locale, translation)
|
235
|
-
original_text = Translatomatic::Model::Text.find_or_create_by!(
|
236
|
-
locale: from_locale,
|
237
|
-
value: translation.original.to_s
|
238
|
-
)
|
239
|
-
|
240
|
-
text = Translatomatic::Model::Text.find_or_create_by!(
|
241
|
-
locale: to_locale,
|
242
|
-
value: translation.result.to_s,
|
243
|
-
from_text: original_text,
|
244
|
-
translator: @current_translator.name
|
245
|
-
)
|
246
|
-
@db_translations += [original_text, text]
|
247
|
-
text
|
248
|
-
end
|
249
|
-
|
250
|
-
def find_database_translations(result, untranslated)
|
251
|
-
from = db_locale(result.from_locale)
|
252
|
-
to = db_locale(result.to_locale)
|
253
|
-
|
254
|
-
Translatomatic::Model::Text.where({
|
255
|
-
locale: to,
|
256
|
-
translator: @current_translator.name,
|
257
|
-
from_texts_texts: {
|
258
|
-
locale_id: from,
|
259
|
-
# convert untranslated set to strings
|
260
|
-
value: untranslated.collect { |i| i.to_s }
|
261
|
-
}
|
262
|
-
}).joins(:from_text)
|
263
|
-
end
|
264
|
-
|
265
|
-
def db_locale(locale)
|
266
|
-
Translatomatic::Model::Locale.from_tag(locale)
|
267
|
-
end
|
268
|
-
end
|
36
|
+
end
|
@@ -1,143 +1,143 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
|
3
|
-
# Database functions
|
4
|
-
class Translatomatic::Database
|
5
|
-
|
6
|
-
class << self
|
7
|
-
# @param options [Hash<Symbol,Object>] Database options
|
8
|
-
# @return [boolean] True if we can connect to the database
|
9
|
-
def enabled?(options = {})
|
10
|
-
new(options).connect
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def initialize(options = {})
|
15
|
-
@env = options[:database_env] || DEFAULT_ENV
|
16
|
-
@db_config = database_config(@env, options)
|
17
|
-
@env_config = @db_config
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
# Database functions
|
4
|
+
class Translatomatic::Database
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# @param options [Hash<Symbol,Object>] Database options
|
8
|
+
# @return [boolean] True if we can connect to the database
|
9
|
+
def enabled?(options = {})
|
10
|
+
new(options).connect
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(options = {})
|
15
|
+
@env = options[:database_env] || DEFAULT_ENV
|
16
|
+
@db_config = database_config(@env, options)
|
17
|
+
@env_config = @db_config
|
18
18
|
raise t("database.no_environment",
|
19
|
-
env: @env, file: db_config_path) unless @env_config[@env]
|
20
|
-
@env_config = @env_config[@env] || {}
|
21
|
-
|
22
|
-
ActiveRecord::Base.configurations = @db_config
|
23
|
-
ActiveRecord::Tasks::DatabaseTasks.env = @env
|
24
|
-
ActiveRecord::Tasks::DatabaseTasks.db_dir = DB_PATH
|
25
|
-
ActiveRecord::Tasks::DatabaseTasks.root = DB_PATH
|
26
|
-
ActiveRecord::Tasks::DatabaseTasks.database_configuration = @db_config
|
27
|
-
create unless exists?
|
28
|
-
migrate
|
29
|
-
end
|
30
|
-
|
31
|
-
# Connect to the database
|
32
|
-
# @return [boolean] True if the connection was established
|
33
|
-
def connect
|
34
|
-
begin
|
35
|
-
ActiveRecord::Base.establish_connection(@env_config)
|
36
|
-
true
|
37
|
-
rescue LoadError
|
38
|
-
false
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
# Disconnect from the database
|
43
|
-
# @return [void]
|
44
|
-
def disconnect
|
45
|
-
ActiveRecord::Base.remove_connection
|
46
|
-
end
|
47
|
-
|
48
|
-
# Test if the database exists
|
49
|
-
# @return [Boolean] true if the database exists
|
50
|
-
def exists?
|
51
|
-
begin
|
52
|
-
return true if sqlite_database_exists?
|
53
|
-
return false unless connect
|
54
|
-
ActiveRecord::Base.connection.tables
|
55
|
-
rescue
|
56
|
-
return false
|
57
|
-
end
|
58
|
-
true
|
59
|
-
end
|
60
|
-
|
61
|
-
# Run outstanding migrations against the database
|
62
|
-
# @return [void]
|
63
|
-
def migrate
|
64
|
-
return false unless connect
|
65
|
-
ActiveRecord::Migrator.migrate(MIGRATIONS_PATH)
|
66
|
-
ActiveRecord::Base.clear_cache!
|
19
|
+
env: @env, file: db_config_path) unless @env_config[@env]
|
20
|
+
@env_config = @env_config[@env] || {}
|
21
|
+
|
22
|
+
ActiveRecord::Base.configurations = @db_config
|
23
|
+
ActiveRecord::Tasks::DatabaseTasks.env = @env
|
24
|
+
ActiveRecord::Tasks::DatabaseTasks.db_dir = DB_PATH
|
25
|
+
ActiveRecord::Tasks::DatabaseTasks.root = DB_PATH
|
26
|
+
ActiveRecord::Tasks::DatabaseTasks.database_configuration = @db_config
|
27
|
+
create unless exists?
|
28
|
+
migrate
|
29
|
+
end
|
30
|
+
|
31
|
+
# Connect to the database
|
32
|
+
# @return [boolean] True if the connection was established
|
33
|
+
def connect
|
34
|
+
begin
|
35
|
+
ActiveRecord::Base.establish_connection(@env_config)
|
36
|
+
true
|
37
|
+
rescue LoadError
|
38
|
+
false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Disconnect from the database
|
43
|
+
# @return [void]
|
44
|
+
def disconnect
|
45
|
+
ActiveRecord::Base.remove_connection
|
46
|
+
end
|
47
|
+
|
48
|
+
# Test if the database exists
|
49
|
+
# @return [Boolean] true if the database exists
|
50
|
+
def exists?
|
51
|
+
begin
|
52
|
+
return true if sqlite_database_exists?
|
53
|
+
return false unless connect
|
54
|
+
ActiveRecord::Base.connection.tables
|
55
|
+
rescue
|
56
|
+
return false
|
57
|
+
end
|
58
|
+
true
|
59
|
+
end
|
60
|
+
|
61
|
+
# Run outstanding migrations against the database
|
62
|
+
# @return [void]
|
63
|
+
def migrate
|
64
|
+
return false unless connect
|
65
|
+
ActiveRecord::Migrator.migrate(MIGRATIONS_PATH)
|
66
|
+
ActiveRecord::Base.clear_cache!
|
67
67
|
log.debug t("database.migrated")
|
68
|
-
end
|
69
|
-
|
70
|
-
# Create the database
|
71
|
-
# @return [boolean] True if the database was created
|
72
|
-
def create
|
73
|
-
begin
|
74
|
-
ActiveRecord::Tasks::DatabaseTasks.create(@env_config)
|
75
|
-
log.debug t("database.created")
|
76
|
-
true
|
68
|
+
end
|
69
|
+
|
70
|
+
# Create the database
|
71
|
+
# @return [boolean] True if the database was created
|
72
|
+
def create
|
73
|
+
begin
|
74
|
+
ActiveRecord::Tasks::DatabaseTasks.create(@env_config)
|
75
|
+
log.debug t("database.created")
|
76
|
+
true
|
77
77
|
rescue LoadError => e
|
78
78
|
log.debug t("database.could_not_create")
|
79
|
-
log.error e.message
|
80
|
-
false
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
# Drop the database
|
85
|
-
# @return [void]
|
86
|
-
def drop
|
87
|
-
disconnect
|
88
|
-
ActiveRecord::Tasks::DatabaseTasks.drop(@env_config)
|
89
|
-
log.debug t("database.deleted")
|
90
|
-
end
|
91
|
-
|
79
|
+
log.error e.message
|
80
|
+
false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Drop the database
|
85
|
+
# @return [void]
|
86
|
+
def drop
|
87
|
+
disconnect
|
88
|
+
ActiveRecord::Tasks::DatabaseTasks.drop(@env_config)
|
89
|
+
log.debug t("database.deleted")
|
90
|
+
end
|
91
|
+
|
92
92
|
private
|
93
93
|
|
94
94
|
include Translatomatic::Util
|
95
95
|
include Translatomatic::DefineOptions
|
96
|
-
|
97
|
-
def sqlite_database_exists?
|
98
|
-
@env_config['adapter'] == 'sqlite3' && File.exist?(@env_config['database'])
|
99
|
-
end
|
100
|
-
|
101
|
-
def self.join_path(*parts)
|
102
|
-
File.realpath(File.join(*parts))
|
103
|
-
end
|
104
|
-
|
105
|
-
DB_PATH = join_path(File.dirname(__FILE__), "..", "..", "db")
|
106
|
-
INTERNAL_CONFIG = File.join(DB_PATH, "database.yml")
|
107
|
-
CUSTOM_CONFIG = File.join(Dir.home, ".translatomatic", "database.yml")
|
108
|
-
DEFAULT_CONFIG = File.exist?(CUSTOM_CONFIG) ? CUSTOM_CONFIG : INTERNAL_CONFIG
|
109
|
-
MIGRATIONS_PATH = File.join(DB_PATH, "migrate")
|
110
|
-
GEM_ROOT = join_path(File.dirname(__FILE__), "..", "..")
|
111
|
-
DEFAULT_ENV = "production"
|
112
|
-
|
113
|
-
define_options(
|
114
|
-
{ name: :database_config, desc: t("database.config_file"),
|
115
|
-
default: DEFAULT_CONFIG },
|
96
|
+
|
97
|
+
def sqlite_database_exists?
|
98
|
+
@env_config['adapter'] == 'sqlite3' && File.exist?(@env_config['database'])
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.join_path(*parts)
|
102
|
+
File.realpath(File.join(*parts))
|
103
|
+
end
|
104
|
+
|
105
|
+
DB_PATH = join_path(File.dirname(__FILE__), "..", "..", "db")
|
106
|
+
INTERNAL_CONFIG = File.join(DB_PATH, "database.yml")
|
107
|
+
CUSTOM_CONFIG = File.join(Dir.home, ".translatomatic", "database.yml")
|
108
|
+
DEFAULT_CONFIG = File.exist?(CUSTOM_CONFIG) ? CUSTOM_CONFIG : INTERNAL_CONFIG
|
109
|
+
MIGRATIONS_PATH = File.join(DB_PATH, "migrate")
|
110
|
+
GEM_ROOT = join_path(File.dirname(__FILE__), "..", "..")
|
111
|
+
DEFAULT_ENV = "production"
|
112
|
+
|
113
|
+
define_options(
|
114
|
+
{ name: :database_config, desc: t("database.config_file"),
|
115
|
+
default: DEFAULT_CONFIG },
|
116
116
|
{ name: :database_env, desc: t("database.env"),
|
117
|
-
default: DEFAULT_ENV })
|
118
|
-
|
119
|
-
# return path to database config
|
120
|
-
def database_config_path(options)
|
121
|
-
if options[:database_env] == "test"
|
122
|
-
INTERNAL_CONFIG # rspec
|
123
|
-
elsif options[:database_config]
|
124
|
-
return options[:database_config]
|
125
|
-
else
|
126
|
-
DEFAULT_CONFIG
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
# return database config as a hash
|
131
|
-
def database_config(env, options)
|
132
|
-
if options[:database_config].kind_of?(Hash)
|
133
|
-
return { env => options[:database_config] }
|
134
|
-
end
|
135
|
-
|
136
|
-
db_config_path = database_config_path(options)
|
137
|
-
dbconfig = File.read(db_config_path)
|
138
|
-
dbconfig.gsub!(/\$HOME/, Dir.home)
|
139
|
-
dbconfig.gsub!(/\$GEM_ROOT/, GEM_ROOT)
|
140
|
-
YAML::load(dbconfig) || {}
|
141
|
-
end
|
142
|
-
|
143
|
-
end
|
117
|
+
default: DEFAULT_ENV })
|
118
|
+
|
119
|
+
# return path to database config
|
120
|
+
def database_config_path(options)
|
121
|
+
if options[:database_env] == "test"
|
122
|
+
INTERNAL_CONFIG # rspec
|
123
|
+
elsif options[:database_config]
|
124
|
+
return options[:database_config]
|
125
|
+
else
|
126
|
+
DEFAULT_CONFIG
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# return database config as a hash
|
131
|
+
def database_config(env, options)
|
132
|
+
if options[:database_config].kind_of?(Hash)
|
133
|
+
return { env => options[:database_config] }
|
134
|
+
end
|
135
|
+
|
136
|
+
db_config_path = database_config_path(options)
|
137
|
+
dbconfig = File.read(db_config_path)
|
138
|
+
dbconfig.gsub!(/\$HOME/, Dir.home)
|
139
|
+
dbconfig.gsub!(/\$GEM_ROOT/, GEM_ROOT)
|
140
|
+
YAML::load(dbconfig) || {}
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Translatomatic
|
2
|
+
# @!visibility private
|
3
|
+
module DefineOptions
|
4
|
+
|
5
|
+
# @!visibility private
|
6
|
+
module ClassMethods
|
7
|
+
attr_reader :options
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def define_options(*options)
|
12
|
+
@options = options.collect { |i| Translatomatic::Option.new(i) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def self.included(klass)
|
19
|
+
klass.extend(ClassMethods)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
File without changes
|
@@ -1,16 +1,16 @@
|
|
1
|
-
module Translatomatic::Extractor
|
2
|
-
# Base class for string extraction functionality
|
3
|
-
class Base
|
4
|
-
|
5
|
-
def initialize(path)
|
6
|
-
@path = path.kind_of?(Pathname) ? path : Pathname.new(path)
|
7
|
-
@contents = @path.read
|
8
|
-
end
|
9
|
-
|
10
|
-
# @return [Array<String>] All strings found
|
11
|
-
def extract
|
12
|
-
@contents.scan(/\"(.*?[^\\])"|'(.*?[^\\])'/).flatten.compact
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
end
|
1
|
+
module Translatomatic::Extractor
|
2
|
+
# Base class for string extraction functionality
|
3
|
+
class Base
|
4
|
+
|
5
|
+
def initialize(path)
|
6
|
+
@path = path.kind_of?(Pathname) ? path : Pathname.new(path)
|
7
|
+
@contents = @path.read
|
8
|
+
end
|
9
|
+
|
10
|
+
# @return [Array<String>] All strings found
|
11
|
+
def extract
|
12
|
+
@contents.scan(/\"(.*?[^\\])"|'(.*?[^\\])'/).flatten.compact
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
module Translatomatic::Extractor
|
2
|
-
# Class to extract strings from ruby code
|
3
|
-
class Ruby < Base
|
4
|
-
|
5
|
-
end # class
|
6
|
-
end # module
|
1
|
+
module Translatomatic::Extractor
|
2
|
+
# Class to extract strings from ruby code
|
3
|
+
class Ruby < Base
|
4
|
+
|
5
|
+
end # class
|
6
|
+
end # module
|
@@ -1,5 +1,5 @@
|
|
1
|
-
# Classes to extract strings from files
|
2
|
-
module Translatomatic::Extractor; end
|
3
|
-
|
4
|
-
require 'translatomatic/extractor/base'
|
5
|
-
require 'translatomatic/extractor/ruby'
|
1
|
+
# Classes to extract strings from files
|
2
|
+
module Translatomatic::Extractor; end
|
3
|
+
|
4
|
+
require 'translatomatic/extractor/base'
|
5
|
+
require 'translatomatic/extractor/ruby'
|