Class: Idiom::Base

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
Directories, Locales, Processing
Defined in:
lib/idiom/base.rb

Direct Known Subclasses

Yaml, Yrb

Constant Summary

Constants included from Locales

Locales::CONFIG, Locales::LOCALES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

find_and_translate_all, source_files, translate_file

Methods included from Processing

#post_process, #pre_process

Methods included from Locales

#locales, #non_us_locales

Methods included from Directories

#destination_file_or_directory, #destination_path, #ensure_destination_path_exists, #use_directories?

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/idiom/base.rb', line 205

def initialize(options={})
  options.stringify_keys!
  
  @source = File.expand_path(options["source"])
  @overwrite = options["overwrite"]
  @languages = options["languages"]
  @substitution_vars = []
  @pass_through_vars = []
  
  @base_source = @source.gsub(/_en-US/, "")
  
  # base directory of the source file
  #
  @source_dir = File.dirname(@source)
  
  # if they specify the :use_dirs option, use that
  # if not, detect whether the source path uses directories for each language
  #
  if options.has_key?("use_dirs")
    @use_dirs = options["use_dirs"]
  else
    @use_dirs = @source_dir =~ /\/en-US$/
  end
  
  if @use_dirs
    @source_dir = File.dirname(@source).gsub(/\/en-US$/, "")
  end
  @destination = options["destination"] || @source_dir
end

Instance Attribute Details

#destinationObject

Destination directory to output the translated files to.



187
188
189
# File 'lib/idiom/base.rb', line 187

def destination
  @destination
end

#languagesObject

Array of languages to translate into.



195
196
197
# File 'lib/idiom/base.rb', line 195

def languages
  @languages
end

#pass_through_varsObject

Cached array of pass-through content



203
204
205
# File 'lib/idiom/base.rb', line 203

def pass_through_vars
  @pass_through_vars
end

#sourceObject

Original filename to translate.



183
184
185
# File 'lib/idiom/base.rb', line 183

def source
  @source
end

#substitution_varsObject

Cached array of subsitution variables



199
200
201
# File 'lib/idiom/base.rb', line 199

def substitution_vars
  @substitution_vars
end

#use_dirsObject

Write the translated strings into a directory for each language?



191
192
193
# File 'lib/idiom/base.rb', line 191

def use_dirs
  @use_dirs
end

Instance Method Details

#after_translationObject



254
255
# File 'lib/idiom/base.rb', line 254

def after_translation
end

#all_keys(lang) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/idiom/base.rb', line 288

def all_keys(lang)
  unless @all_keys
    @all_keys = {}
    Dir[destination_file_or_directory(lang)].each do |path|
      if File.exists?(path)
        keys = parse(path)
        @all_keys = @all_keys.merge(keys)
      end
    end
  end
  @all_keys
end

#before_translationObject



251
252
# File 'lib/idiom/base.rb', line 251

def before_translation
end

#clear_all_keysObject



301
302
303
# File 'lib/idiom/base.rb', line 301

def clear_all_keys
  @all_keys = nil
end

#comment?(line) ⇒ Boolean

Returns:

  • (Boolean)


379
380
381
# File 'lib/idiom/base.rb', line 379

def comment?(line)
  line =~ /^[\s]*#/
end

#copy_and_translate_line(line, lang) ⇒ Object



318
319
320
321
322
323
324
325
326
327
# File 'lib/idiom/base.rb', line 318

def copy_and_translate_line(line, lang)
  line = line.split("\n").first
  if comment?(line) || line.blank?
    nil
  elsif line.strip == "en:"
    "#{lang}:"
  else
    translate_new_key(line, lang)
  end
end

#do_translate(value, code) ⇒ Object



362
363
364
365
366
367
368
369
# File 'lib/idiom/base.rb', line 362

def do_translate(value, code)
  if CONFIG["library"].to_s.downcase == "google"
    Translate.t(value, "ENGLISH", code)
    sleep(5)
  else
    MicrosoftTranslator.t(value, code)
  end
end

#each_lineObject



273
274
275
276
277
278
279
280
281
282
# File 'lib/idiom/base.rb', line 273

def each_line
  output = []
  @lines ||= File.readlines(source)
  
  @lines.each do |line|
    new_line = yield line
    output << new_line
  end
  output.compact.join("\n")
end

#format(key, value) ⇒ Object



371
372
373
# File 'lib/idiom/base.rb', line 371

def format(key, value)
  raise "Define in child"
end

#generateObject



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/idiom/base.rb', line 235

def generate
  before_translation
  # RM NOTE: Async concurrent by language?
  non_us_locales.each do |lang|
    code = LOCALES[lang]
    destination = ensure_destination_path_exists(lang)
    # RM NOTE: this breaks on multi-line strings
    new_content = each_line do |line|
      copy_and_translate_line(line, lang)
    end
    write_content(destination, new_content)
    clear_all_keys
  end
  after_translation
end

#key_and_value_from_line(line) ⇒ Object



375
376
377
# File 'lib/idiom/base.rb', line 375

def key_and_value_from_line(line)
  raise "Define in child"
end

#key_is_new?(k, lang) ⇒ Boolean

Returns:

  • (Boolean)


329
330
331
# File 'lib/idiom/base.rb', line 329

def key_is_new?(k, lang)
  k && !all_keys(lang).has_key?(k)
end

#new_translation_messageObject



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/idiom/base.rb', line 257

def new_translation_message
  now = Time.now
  
  date = now.day
  month = now.month
  year = now.year
  
  timestamp = "#{month}/#{date}/#{year}"
  output = []
  output << "# "
  output << "# Keys translated automatically on #{timestamp}."
  output << "# "
  
  output.join("\n")
end

#parse(p) ⇒ Object



284
285
286
# File 'lib/idiom/base.rb', line 284

def parse(p)
  raise "Define in child"
end

#translate(value, lang) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/idiom/base.rb', line 349

def translate(value, lang)
  value = value.gsub(/^'/, "").gsub(/'$/, "")
  return '' if value == ''
  $stdout.puts("Translating #{value} into #{lang}...")
  code = LOCALES[lang]
  value = pre_process(value, lang)

  translation = do_translate(value, code)

  value = post_process(translation, lang)
  value
end

#translate_new_key(line, lang) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/idiom/base.rb', line 333

def translate_new_key(line, lang)
  k, v = key_and_value_from_line(line)
  
  if @overwrite || key_is_new?(k, lang)
    translation = translate(v, lang)
  
    if translation == "Error: invalid result data"
      return nil
    end
    
    format(k, translation)
  else
    nil
  end        
end

#write_content(destination, content) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/idiom/base.rb', line 305

def write_content(destination, content)
  unless content.blank?
    $stdout.puts "Writing to #{destination}"
    $stdout.puts content
    $stdout.puts
    File.open(destination, "a") do |f|
      f.puts
      f.puts new_translation_message
      f.puts content
    end
  end        
end