Class: Translatomatic::CLI::Translate

Inherits:
Base
  • Object
show all
Defined in:
lib/translatomatic/cli/translate.rb

Overview

Translation functions for the command line interface

Instance Method Summary collapse

Instance Method Details

#file(file = nil, *locales) ⇒ void

This method returns an undefined value.

Translate files to target locales

Parameters:

  • file (String) (defaults to: nil)

    Resource file to translate, can also be set with the –source-files option.

  • locales (Array<String>)

    List of target locales, can also be set with the –target-locales option



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/translatomatic/cli/translate.rb', line 65

def file(file = nil, *locales)
  run do
    # set up database and progress updater
    Translatomatic::Database.new(conf.all)
    stats = Translation::Stats.new

    # translate the files
    source_files = load_source_files(file)
    listener = progress_updater(source_files, locales)
    source_files.each do |source|
      stats += translate_file(source, locales, listener)
    end
    log.info stats
    finish_log
  end
end

#string(text, *locales) ⇒ void

This method returns an undefined value.

Translate a string to target locales

Parameters:

  • text (String)

    String to translate

  • locales (Array<String>)

    List of target locales, can also be set with the –target-locales option



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/translatomatic/cli/translate.rb', line 28

def string(text, *locales)
  run do
    all_conf = conf.all
    source_locale = conf.get(:source_locale) || Locale.default.to_s
    target_locales = determine_target_locales(locales)
    providers = Provider.resolve(conf.get(:provider), all_conf)

    template = '(%<locale>s) %<text>s'
    puts format(template, locale: source_locale, text: text)
    providers.each do |provider|
      puts t('cli.translate.using_provider', name: provider.name)
      target_locales.each do |l|
        translations = provider.translate([text], source_locale, l)
        translations.each do |translation|
          puts format('  -> ' + template, locale: l, text: translation)
        end
      end
      puts
    end

    finish_log
  end
end