Class: Translatomatic::FileTranslator

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/translatomatic/file_translator.rb

Overview

Translates resource files from one language to another.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FileTranslator

Create a new FileTranslator instance

Parameters:

  • options (Hash<Symbol,Object>) (defaults to: {})

    converter and/or provider options.



10
11
12
13
14
# File 'lib/translatomatic/file_translator.rb', line 10

def initialize(options = {})
  @dry_run = options[:dry_run]
  @in_place = options[:in_place]
  @translator = Translator.new(options)
end

Instance Attribute Details

#translatorTranslatomatic::Translator (readonly)

Returns Translator object.

Returns:



5
6
7
# File 'lib/translatomatic/file_translator.rb', line 5

def translator
  @translator
end

Instance Method Details

#translate(file, to_locale) ⇒ Translatomatic::ResourceFile

Translate properties of file to the target locale. Does not write changes to disk.

Parameters:

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/translatomatic/file_translator.rb', line 21

def translate(file, to_locale)
  file = resource_file(file)
  to_locale = build_locale(to_locale)

  # do nothing if target language is the same as source language
  return file if file.locale.language == to_locale.language

  texts = texts_from_file(file)
  unless @dry_run
    collection = @translator.translate(texts, to_locale)
    update_properties(file, to_locale, collection)
  end
  file
end

#translate_to_file(source, to_locale) ⇒ ResourceFile

Translates a resource file and writes results to a target resource file. The path of the target resource file is automagically determined.

Parameters:

  • source (ResourceFile)

    The source

  • to_locale (String)

    The target locale, e.g. “fr”

Returns:



42
43
44
45
46
47
# File 'lib/translatomatic/file_translator.rb', line 42

def translate_to_file(source, to_locale)
  target = translation_target_file(source, to_locale)
  return source unless target
  translate(target, to_locale)
  save_resource_file(target)
end