translatomatic 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +19 -0
  3. data/.gitignore +0 -0
  4. data/.travis.yml +7 -7
  5. data/.yardopts +9 -0
  6. data/Gemfile +4 -4
  7. data/Guardfile +0 -0
  8. data/README.de.md +61 -16
  9. data/README.es.md +60 -15
  10. data/README.fr.md +61 -16
  11. data/README.it.md +60 -15
  12. data/README.ja.md +59 -14
  13. data/README.ko.md +137 -0
  14. data/README.md +58 -13
  15. data/README.ms.md +137 -0
  16. data/README.pt.md +137 -0
  17. data/README.ru.md +137 -0
  18. data/README.sv.md +137 -0
  19. data/README.zh.md +137 -0
  20. data/bin/setup +8 -8
  21. data/bin/translatomatic +6 -6
  22. data/bin/travis +1 -1
  23. data/config/locales/translatomatic/de.yml +104 -0
  24. data/config/locales/translatomatic/en.yml +101 -0
  25. data/config/locales/translatomatic/es.yml +105 -0
  26. data/config/locales/translatomatic/fr.yml +105 -0
  27. data/config/locales/translatomatic/it.yml +103 -0
  28. data/config/locales/translatomatic/ja.yml +102 -0
  29. data/config/locales/translatomatic/ko.yml +101 -0
  30. data/config/locales/translatomatic/ms.yml +103 -0
  31. data/config/locales/translatomatic/pt.yml +105 -0
  32. data/config/locales/translatomatic/ru.yml +103 -0
  33. data/config/locales/translatomatic/sv.yml +103 -0
  34. data/config/locales/translatomatic/zh.yml +102 -0
  35. data/db/migrate/201712170000_initial.rb +2 -1
  36. data/lib/translatomatic/cli/base.rb +73 -0
  37. data/lib/translatomatic/cli/common_options.rb +14 -0
  38. data/lib/translatomatic/cli/config.rb +81 -0
  39. data/lib/translatomatic/cli/database.rb +29 -0
  40. data/lib/translatomatic/cli/main.rb +112 -0
  41. data/lib/translatomatic/cli/translate.rb +146 -0
  42. data/lib/translatomatic/cli.rb +8 -216
  43. data/lib/translatomatic/config.rb +143 -0
  44. data/lib/translatomatic/converter.rb +196 -149
  45. data/lib/translatomatic/converter_stats.rb +18 -14
  46. data/lib/translatomatic/database.rb +35 -37
  47. data/lib/translatomatic/escaped_unicode.rb +1 -1
  48. data/lib/translatomatic/extractor/base.rb +2 -0
  49. data/lib/translatomatic/extractor/ruby.rb +1 -0
  50. data/lib/translatomatic/extractor.rb +1 -0
  51. data/lib/translatomatic/http_request.rb +43 -14
  52. data/lib/translatomatic/locale.rb +28 -4
  53. data/lib/translatomatic/logger.rb +21 -13
  54. data/lib/translatomatic/model/locale.rb +5 -1
  55. data/lib/translatomatic/model/text.rb +2 -0
  56. data/lib/translatomatic/model.rb +1 -0
  57. data/lib/translatomatic/option.rb +75 -10
  58. data/lib/translatomatic/progress_updater.rb +7 -3
  59. data/lib/translatomatic/resource_file/base.rb +41 -18
  60. data/lib/translatomatic/resource_file/html.rb +11 -14
  61. data/lib/translatomatic/resource_file/markdown.rb +13 -12
  62. data/lib/translatomatic/resource_file/plist.rb +3 -14
  63. data/lib/translatomatic/resource_file/properties.rb +21 -3
  64. data/lib/translatomatic/resource_file/resw.rb +1 -0
  65. data/lib/translatomatic/resource_file/text.rb +1 -0
  66. data/lib/translatomatic/resource_file/xcode_strings.rb +23 -14
  67. data/lib/translatomatic/resource_file/xml.rb +34 -22
  68. data/lib/translatomatic/resource_file/yaml.rb +39 -5
  69. data/lib/translatomatic/resource_file.rb +7 -5
  70. data/lib/translatomatic/string.rb +40 -12
  71. data/lib/translatomatic/tmx/document.rb +11 -12
  72. data/lib/translatomatic/tmx/translation_unit.rb +5 -1
  73. data/lib/translatomatic/tmx.rb +2 -0
  74. data/lib/translatomatic/translation.rb +30 -0
  75. data/lib/translatomatic/translation_result.rb +45 -45
  76. data/lib/translatomatic/translator/base.rb +128 -83
  77. data/lib/translatomatic/translator/frengly.rb +62 -57
  78. data/lib/translatomatic/translator/google.rb +35 -31
  79. data/lib/translatomatic/translator/microsoft.rb +41 -33
  80. data/lib/translatomatic/translator/my_memory.rb +68 -64
  81. data/lib/translatomatic/translator/yandex.rb +56 -39
  82. data/lib/translatomatic/translator.rb +99 -63
  83. data/lib/translatomatic/util.rb +31 -1
  84. data/lib/translatomatic/version.rb +4 -1
  85. data/lib/translatomatic.rb +17 -0
  86. data/translatomatic.gemspec +5 -4
  87. metadata +56 -16
@@ -1,171 +1,216 @@
1
+ # The converter ties together functionality from translators,
2
+ # resource files, and the database to convert files from one
3
+ # language to another.
1
4
  class Translatomatic::Converter
2
5
 
3
- class << self
4
- attr_reader :options
5
- private
6
- include Translatomatic::DefineOptions
7
- end
8
-
9
- define_options(
10
- { name: :translator, type: :string, aliases: "-t",
11
- desc: "Translator implementation to use",
12
- enum: Translatomatic::Translator.names },
13
- { name: :dry_run, type: :boolean, aliases: "-n", desc:
14
- "Print actions without performing translations or writing files" },
15
- { name: :use_database, type: :boolean, default: true, desc:
16
- "Store and retrieve translations in the database" }
17
- )
18
-
19
- # @return [Translatomatic::ConverterStats] translation statistics
20
- attr_reader :stats
21
-
22
- # @return [Array<Translatomatic::Model::Text>] A list of translations saved to the database
6
+ # @return [Array<Translatomatic::Model::Text>] A list of translations saved to the database
23
7
  attr_reader :db_translations
24
8
 
25
9
  # Create a converter to translate files
26
10
  #
27
- # @param options A hash of converter and/or translator options.
11
+ # @param options [Hash<Symbol,Object>] converter and/or translator options.
28
12
  def initialize(options = {})
29
13
  @dry_run = options[:dry_run]
30
- @translator = options[:translator]
31
- @listener = options[:listener]
32
-
33
- # use database by default if we're connected to a database
34
- use_db = options.include?(:use_database) ? options[:use_database] : true
35
- @use_db = use_db && ActiveRecord::Base.connected?
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
36
18
 
37
- log.debug("database is disabled") unless @use_db
38
- if @translator && !@translator.respond_to?(:translate)
39
- klass = Translatomatic::Translator.find(@translator)
40
- @translator = klass.new(options)
41
- end
42
- raise "translator required" unless @translator
43
- @translator.listener = @listener if @listener
44
- @from_db = 0
45
- @from_translator = 0
46
- @db_translations = []
47
- end
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
23
+
24
+ @db_translations = []
25
+ @translations = {} # map of original text to Translation
26
+ end
48
27
 
49
28
  # @return [Translatomatic::ConverterStats] Translation statistics
50
29
  def stats
51
- Translatomatic::ConverterStats.new(@from_db, @from_translator)
30
+ @stats ||= Translatomatic::ConverterStats.new(@translations)
52
31
  end
53
32
 
54
- # Translate contents of source_file to the target locale.
55
- # Automatically determines the target filename based on target locale.
33
+ # Translate properties of source_file to the target locale.
34
+ # Does not write changes to disk.
56
35
  #
57
- # @param [String, Translatomatic::ResourceFile] source_file File to translate
58
- # @param [String] to_locale The target locale, e.g. "fr"
36
+ # @param file [String, Translatomatic::ResourceFile] File to translate
37
+ # @param to_locale [String] The target locale, e.g. "fr"
59
38
  # @return [Translatomatic::ResourceFile] The translated resource file
60
- def translate(source_file, to_locale)
61
- if source_file.kind_of?(Translatomatic::ResourceFile::Base)
62
- source = source_file
63
- else
64
- source = Translatomatic::ResourceFile.load(source_file)
65
- raise "unsupported file type #{source_file}" unless source
66
- end
67
-
68
- to_locale = parse_locale(to_locale)
69
- target = Translatomatic::ResourceFile.load(source.path)
70
- target.path = source.locale_path(to_locale)
71
- target.locale = to_locale
72
- translate_to_target(source, target)
73
- end
74
-
75
- # Translates a resource file and writes results to a target resource file
76
- #
77
- # @param source [Translatomatic::ResourceFile] The source
78
- # @param target [Translatomatic::ResourceFile] The file to write
79
- # @return [Translatomatic::ResourceFile] The translated resource file
80
- def translate_to_target(source, target)
81
- # perform translation
82
- log.info("translating #{source} to #{target}")
83
- properties = translate_properties(source.properties, source.locale, target.locale)
84
- target.properties = properties
85
- unless @dry_run
86
- target.path.parent.mkpath
87
- target.save
88
- end
89
- target
39
+ def translate(file, to_locale)
40
+ file = resource_file(file)
41
+ to_locale = parse_locale(to_locale)
42
+
43
+ # do nothing if target language is the same as source language
44
+ return file if file.locale.language == to_locale.language
45
+
46
+ result = Translatomatic::TranslationResult.new(file, to_locale)
47
+
48
+ # translate using strings from the database first
49
+ each_translator(result) { translate_properties_with_db(result) }
50
+ # send remaining unknown strings to translator
51
+ each_translator(result) { translate_properties_with_translator(result) }
52
+
53
+ log.debug(t("converter.stats", from_db: stats.from_db,
54
+ from_translator: stats.from_translator,
55
+ untranslated: result.untranslated.length))
56
+ @listener.untranslated_texts(result.untranslated) if @listener
57
+
58
+ file.properties = result.properties
59
+ file.locale = to_locale
60
+ file
90
61
  end
91
-
92
- # Translate values in the hash of properties.
93
- # Uses existing translations from the database if available.
94
- #
95
- # @param [Hash] properties Text to translate
96
- # @param [String, Locale] from_locale The locale of the given properties
97
- # @param [String, Locale] to_locale The target locale for translations
98
- # @return [Hash] Translated properties
99
- def translate_properties(properties, from_locale, to_locale)
100
- from_locale = parse_locale(from_locale)
101
- to_locale = parse_locale(to_locale)
102
-
103
- # sanity check
104
- return properties if from_locale.language == to_locale.language
105
-
106
- result = Translatomatic::TranslationResult.new(properties,
107
- from_locale, to_locale)
108
62
 
109
- # translate using strings from the database first
110
- db_texts = translate_properties_with_db(result)
111
-
112
- # send remaining unknown strings to translator
113
- tr_texts = translate_properties_with_translator(result)
114
-
115
- log.debug("translations from db: %d translator: %d untranslated: %d" %
116
- [db_texts.length, tr_texts.length, result.untranslated.length])
117
- @listener.untranslated_texts(result.untranslated) if @listener
118
-
119
- result.properties
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
120
83
  end
121
84
 
122
- private
85
+ private
123
86
 
124
- include Translatomatic::Util
87
+ include Translatomatic::Util
88
+ include Translatomatic::DefineOptions
125
89
 
126
- # update result with translations from the database.
127
- # returns a list of text records from the database.
128
- def translate_properties_with_db(result)
129
- db_texts = []
130
- unless database_disabled?
131
- untranslated = result.untranslated.to_a
132
- db_texts = find_database_translations(result, untranslated)
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
+ )
133
98
 
134
- # find strings in untranslated that were matched in the database
135
- original_map = {} # map of original text to translated text from db
136
- db_texts.each do |db_text|
137
- original_map[db_text.from_text.value] = db_text
99
+ def each_translator(result)
100
+ @translators.each do |translator|
101
+ break if result.untranslated.empty?
102
+ @current_translator = translator
103
+ yield
104
+ end
105
+ end
106
+
107
+ # Attempt to restore interpolated variable names in the translation.
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
138
132
  end
139
- matched = untranslated.select { |i| original_map[i.value] }
140
- db_texts = db_texts.collect { |i| i.value }
141
- result.update_strings(matched, db_texts)
142
- @from_db += db_texts.length
143
- @listener.translated_texts(db_texts) if @listener
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
144
137
  end
145
- db_texts
146
138
  end
147
139
 
148
- # update result with translations from the translator.
149
- # returns a list of strings from the translator.
150
- def translate_properties_with_translator(result)
151
- untranslated = result.untranslated.to_a.select { |i| translatable?(i) }
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) }
152
179
  translated = []
153
- @from_translator += untranslated.length
154
180
  if !untranslated.empty? && !@dry_run
155
- translated = @translator.translate(untranslated.collect { |i| i.to_s },
156
- result.from_locale, result.to_locale)
157
- result.update_strings(untranslated, translated)
158
- unless database_disabled?
159
- save_database_translations(result, untranslated, translated)
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
+ )
186
+
187
+ # create list of translations, filtering out invalid translations
188
+ translations = []
189
+ untranslated.zip(translated).each do |from, to|
190
+ translation = translation(from, to, false)
191
+ restore_variables(result, translation)
192
+ translations << translation
160
193
  end
161
- end
162
- translated
194
+
195
+ result.update_strings(translations)
196
+ unless database_disabled?
197
+ save_database_translations(result, translations)
198
+ end
199
+ end
200
+ translated
163
201
  end
164
202
 
165
- def database_disabled?
166
- !@use_db
203
+ def translation(from, to, from_database = false)
204
+ translator = @current_translator.name
205
+ t = Translatomatic::Translation.new(from, to, translator, from_database)
206
+ @translations[from] = t
207
+ t
167
208
  end
168
209
 
210
+ def database_disabled?
211
+ !@use_db
212
+ end
213
+
169
214
  def parse_locale(locale)
170
215
  Translatomatic::Locale.parse(locale)
171
216
  end
@@ -175,42 +220,44 @@ class Translatomatic::Converter
175
220
  !string.empty? && !string.match(/^[\d,]+$/)
176
221
  end
177
222
 
178
- def save_database_translations(result, untranslated, translated)
223
+ def save_database_translations(result, translations)
179
224
  ActiveRecord::Base.transaction do
180
225
  from = db_locale(result.from_locale)
181
226
  to = db_locale(result.to_locale)
182
- untranslated.zip(translated).each do |t1, t2|
183
- save_database_translation(from, to, t1, t2)
227
+ translations.each do |translation|
228
+ next if translation.result.nil? # skip invalid translations
229
+ save_database_translation(from, to, translation)
184
230
  end
185
231
  end
186
232
  end
187
233
 
188
- def save_database_translation(from_locale, to_locale, t1, t2)
234
+ def save_database_translation(from_locale, to_locale, translation)
189
235
  original_text = Translatomatic::Model::Text.find_or_create_by!(
190
236
  locale: from_locale,
191
- value: t1.to_s
237
+ value: translation.original.to_s
192
238
  )
193
239
 
194
240
  text = Translatomatic::Model::Text.find_or_create_by!(
195
241
  locale: to_locale,
196
- value: t2.to_s,
242
+ value: translation.result.to_s,
197
243
  from_text: original_text,
198
- translator: @translator.name
199
- )
200
- @db_translations += [original_text, text]
244
+ translator: @current_translator.name
245
+ )
246
+ @db_translations += [original_text, text]
201
247
  text
202
248
  end
203
249
 
204
250
  def find_database_translations(result, untranslated)
205
251
  from = db_locale(result.from_locale)
206
252
  to = db_locale(result.to_locale)
207
-
208
- # convert untranslated set to strings
253
+
209
254
  Translatomatic::Model::Text.where({
210
- locale: to,
211
- from_texts_texts: {
212
- locale_id: from,
213
- value: untranslated.collect { |i| i.to_s }
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 }
214
261
  }
215
262
  }).joins(:from_text)
216
263
  end
@@ -1,7 +1,8 @@
1
1
  # Translation statistics
2
2
  class Translatomatic::ConverterStats
3
+ include Translatomatic::Util
3
4
 
4
- # @return [Number] The total number of strings translated.
5
+ # @return [Array<Translatomatic::Translation>] A list of all translations
5
6
  attr_reader :translations
6
7
 
7
8
  # @return [Number] The number of translations that came from the database.
@@ -9,19 +10,22 @@ class Translatomatic::ConverterStats
9
10
 
10
11
  # @return [Number] The number of translations that came from the translator.
11
12
  attr_reader :from_translator
13
+
14
+ # @return [Number] The number of untranslated strings
15
+ attr_reader :untranslated
16
+
17
+ private
18
+
19
+ def initialize(translations)
20
+ @translations = translations.values
21
+ @from_db = @translations.count { |i| i.from_database && i.result }
22
+ @from_translator = @translations.count { |i| !i.from_database && i.result }
23
+ @untranslated = @translations.count { |i| i.result == nil }
24
+ end
12
25
 
13
- def initialize(from_db, from_translator)
14
- @translations = from_db + from_translator
15
- @from_db = from_db
16
- @from_translator = from_translator
17
- end
18
-
19
- def +(other)
20
- self.class.new(@from_db + other.from_db, @from_translator + other.from_translator)
21
- end
22
-
23
- def to_s
24
- "Total translations: #{@translations} " +
25
- "(#{@from_db} from database, #{@from_translator} from translator)"
26
+ def to_s
27
+ t("converter.total_translations", total: @translations.length,
28
+ from_db: @from_db, from_translator: @from_translator,
29
+ untranslated: @untranslated)
26
30
  end
27
31
  end
@@ -1,29 +1,23 @@
1
1
  require 'active_record'
2
-
2
+
3
+ # Database functions
3
4
  class Translatomatic::Database
4
5
 
5
- include Translatomatic::Util
6
-
7
6
  class << self
8
- attr_reader :options
9
-
10
- # @param [Hash<Symbol,Object>] options Database options
11
- # @return True if we can connect to the database
7
+ # @param options [Hash<Symbol,Object>] Database options
8
+ # @return [boolean] True if we can connect to the database
12
9
  def enabled?(options = {})
13
10
  new(options).connect
14
11
  end
15
-
16
- private
17
-
18
- include Translatomatic::DefineOptions
19
12
  end
20
13
 
21
14
  def initialize(options = {})
22
- @env = options[:database_env] || DEFAULT_ENV
23
- @db_config = database_config(@env, options)
15
+ @env = options[:database_env] || DEFAULT_ENV
16
+ @db_config = database_config(@env, options)
24
17
  @env_config = @db_config
25
- raise "no environment '#{@env}' in #{db_config_path}" unless @env_config[@env]
26
- @env_config = @env_config[@env] || {}
18
+ raise t("database.no_environment",
19
+ env: @env, file: db_config_path) unless @env_config[@env]
20
+ @env_config = @env_config[@env] || {}
27
21
 
28
22
  ActiveRecord::Base.configurations = @db_config
29
23
  ActiveRecord::Tasks::DatabaseTasks.env = @env
@@ -70,7 +64,7 @@ class Translatomatic::Database
70
64
  return false unless connect
71
65
  ActiveRecord::Migrator.migrate(MIGRATIONS_PATH)
72
66
  ActiveRecord::Base.clear_cache!
73
- log.debug "Database migrated."
67
+ log.debug t("database.migrated")
74
68
  end
75
69
 
76
70
  # Create the database
@@ -78,10 +72,11 @@ class Translatomatic::Database
78
72
  def create
79
73
  begin
80
74
  ActiveRecord::Tasks::DatabaseTasks.create(@env_config)
81
- log.debug "Database created."
75
+ log.debug t("database.created")
82
76
  true
83
- rescue LoadError => e
84
- log.debug "Database could not be created: " + e.message
77
+ rescue LoadError => e
78
+ log.debug t("database.could_not_create")
79
+ log.error e.message
85
80
  false
86
81
  end
87
82
  end
@@ -91,10 +86,13 @@ class Translatomatic::Database
91
86
  def drop
92
87
  disconnect
93
88
  ActiveRecord::Tasks::DatabaseTasks.drop(@env_config)
94
- log.debug "Database deleted."
89
+ log.debug t("database.deleted")
95
90
  end
96
91
 
97
- private
92
+ private
93
+
94
+ include Translatomatic::Util
95
+ include Translatomatic::DefineOptions
98
96
 
99
97
  def sqlite_database_exists?
100
98
  @env_config['adapter'] == 'sqlite3' && File.exist?(@env_config['database'])
@@ -113,11 +111,11 @@ class Translatomatic::Database
113
111
  DEFAULT_ENV = "production"
114
112
 
115
113
  define_options(
116
- { name: :database_config, description: "Database config file",
114
+ { name: :database_config, desc: t("database.config_file"),
117
115
  default: DEFAULT_CONFIG },
118
- { name: :database_env, description: "Database environment",
116
+ { name: :database_env, desc: t("database.env"),
119
117
  default: DEFAULT_ENV })
120
-
118
+
121
119
  # return path to database config
122
120
  def database_config_path(options)
123
121
  if options[:database_env] == "test"
@@ -127,19 +125,19 @@ class Translatomatic::Database
127
125
  else
128
126
  DEFAULT_CONFIG
129
127
  end
130
- end
131
-
132
- # return database config as a hash
133
- def database_config(env, options)
134
- if options[:database_config].kind_of?(Hash)
135
- return { env => options[:database_config] }
136
- end
137
-
138
- db_config_path = database_config_path(options)
139
- dbconfig = File.read(db_config_path)
140
- dbconfig.gsub!(/\$HOME/, Dir.home)
141
- dbconfig.gsub!(/\$GEM_ROOT/, GEM_ROOT)
142
- YAML::load(dbconfig) || {}
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) || {}
143
141
  end
144
142
 
145
143
  end
@@ -41,7 +41,7 @@ module Translatomatic::EscapedUnicode
41
41
  )+ |
42
42
  [\x80-\xc1\xf5-\xff] # invalid
43
43
  )/nx) { |c|
44
- c.size == 1 and raise "Invalid utf8 byte: '#{c}'"
44
+ c.size == 1 and raise t("unicode.invalid_byte", byte: c)
45
45
  s = c.encode("utf-16be", "utf-8").unpack('H*')[0]
46
46
  s.force_encoding(::Encoding::ASCII_8BIT)
47
47
  s.gsub!(/.{4}/n, '\\\\u\&')
@@ -1,4 +1,5 @@
1
1
  module Translatomatic::Extractor
2
+ # Base class for string extraction functionality
2
3
  class Base
3
4
 
4
5
  def initialize(path)
@@ -6,6 +7,7 @@ module Translatomatic::Extractor
6
7
  @contents = @path.read
7
8
  end
8
9
 
10
+ # @return [Array<String>] All strings found
9
11
  def extract
10
12
  @contents.scan(/\"(.*?[^\\])"|'(.*?[^\\])'/).flatten.compact
11
13
  end
@@ -1,4 +1,5 @@
1
1
  module Translatomatic::Extractor
2
+ # Class to extract strings from ruby code
2
3
  class Ruby < Base
3
4
 
4
5
  end # class
@@ -1,3 +1,4 @@
1
+ # Classes to extract strings from files
1
2
  module Translatomatic::Extractor; end
2
3
 
3
4
  require 'translatomatic/extractor/base'