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.



203
204
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
# File 'lib/idiom/base.rb', line 203

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.



185
186
187
# File 'lib/idiom/base.rb', line 185

def destination
  @destination
end

#languagesObject

Array of languages to translate into.



193
194
195
# File 'lib/idiom/base.rb', line 193

def languages
  @languages
end

#pass_through_varsObject

Cached array of pass-through content



201
202
203
# File 'lib/idiom/base.rb', line 201

def pass_through_vars
  @pass_through_vars
end

#sourceObject

Original filename to translate.



181
182
183
# File 'lib/idiom/base.rb', line 181

def source
  @source
end

#substitution_varsObject

Cached array of subsitution variables



197
198
199
# File 'lib/idiom/base.rb', line 197

def substitution_vars
  @substitution_vars
end

#use_dirsObject

Write the translated strings into a directory for each language?



189
190
191
# File 'lib/idiom/base.rb', line 189

def use_dirs
  @use_dirs
end

Instance Method Details

#after_translationObject



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

def after_translation
end

#all_keys(lang) ⇒ Object



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

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



249
250
# File 'lib/idiom/base.rb', line 249

def before_translation
end

#clear_all_keysObject



299
300
301
# File 'lib/idiom/base.rb', line 299

def clear_all_keys
  @all_keys = nil
end

#comment?(line) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#copy_and_translate_line(line, lang) ⇒ Object



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

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



359
360
361
362
363
364
365
366
# File 'lib/idiom/base.rb', line 359

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



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

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



368
369
370
# File 'lib/idiom/base.rb', line 368

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

#generateObject



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

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



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

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

#key_is_new?(k, lang) ⇒ Boolean

Returns:

  • (Boolean)


327
328
329
# File 'lib/idiom/base.rb', line 327

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

#new_translation_messageObject



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

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



282
283
284
# File 'lib/idiom/base.rb', line 282

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

#translate(value, lang) ⇒ Object



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

def translate(value, lang)
  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



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

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



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

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