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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/translatomatic/cli/translate.rb', line 65

def file(file = nil, *locales)
  run do
    setup_translation
    determine_target_locales(locales)

    # check source file(s) exist and they can be loaded
    source_files = parse_list(cli_option(:source_files), file)
    source_files.each do |source_file|
      path = source_path(source_file)
      raise t("file.not_found", file: path) unless File.exist?(path)
      source = Translatomatic::ResourceFile.load(path, @source_locale)
      raise t("file.unsupported", file: path) unless source
    end

    # set up database
    Translatomatic::Database.new(options)

    # set up file translatiln
    translation_count = calculate_translation_count(source_files, @target_locales)
    ft_options = options.merge(
      translator: @translators,
      listener: progress_updater(translation_count)
    )
    ft = Translatomatic::FileTranslator.new(ft_options)

    source_files.each do |path|
      # read source file
      source = Translatomatic::ResourceFile.load(path, @source_locale)

      # convert source to locale(s) and write files
      @target_locales.each do |i|
        to_locale = locale(i)
        next if to_locale.language == source.locale.language
        ft.translate_to_file(source, to_locale)
      end
    end

    log.info ft.stats
    finish_log

    share_translations(ft) if cli_option(:share)
  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



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

def string(text, *locales)
  run do
    setup_translation
    determine_target_locales(locales)

    puts "(%s) %s" % [@source_locale, text]
    @translators.each do |translator|
      puts t("cli.using_translator", name: translator.name)
      @target_locales.each do |target_locale|
        result = translator.translate([text], @source_locale, target_locale)
        puts "  -> (%s) %s" % [target_locale, result]
      end
      puts
    end

    finish_log
  end
end