Class: Translatomatic::TranslationResult

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

Overview

Stores results of a translation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, to_locale) ⇒ TranslationResult

Create a translation result

Parameters:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/translatomatic/translation_result.rb', line 25

def initialize(file, to_locale)
  @file = file
  @value_to_keys = {}
  @untranslated = Set.new
  @from_locale = file.locale
  @to_locale = to_locale

  # duplicate strings
  @properties = file.properties.transform_values { |i| i.dup }

  @properties.each do |key, value|
    # split property value into sentences
    string = string(value, from_locale)
    string.sentences.each do |sentence|
      @untranslated << sentence
      keylist = (@value_to_keys[sentence.to_s] ||= [])
      keylist << key
    end
  end
end

Instance Attribute Details

#fileTranslatomatic::ResourceFile::Base (readonly)

Returns The resource file.

Returns:



8
9
10
# File 'lib/translatomatic/translation_result.rb', line 8

def file
  @file
end

#from_localeLocale (readonly)

Returns The locale of the original strings.

Returns:

  • (Locale)

    The locale of the original strings



14
15
16
# File 'lib/translatomatic/translation_result.rb', line 14

def from_locale
  @from_locale
end

#propertiesHash<String,String> (readonly)

Returns Translation results.

Returns:



11
12
13
# File 'lib/translatomatic/translation_result.rb', line 11

def properties
  @properties
end

#to_localeLocale (readonly)

Returns The target locale.

Returns:

  • (Locale)

    The target locale



17
18
19
# File 'lib/translatomatic/translation_result.rb', line 17

def to_locale
  @to_locale
end

#untranslatedSet<String> (readonly)

Returns Untranslated strings.

Returns:

  • (Set<String>)

    Untranslated strings



20
21
22
# File 'lib/translatomatic/translation_result.rb', line 20

def untranslated
  @untranslated
end

Instance Method Details

#update_strings(translations) ⇒ void

This method returns an undefined value.

Update result with a list of translated strings.

Parameters:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/translatomatic/translation_result.rb', line 49

def update_strings(translations)
  # sort translation list by largest offset first so that we replace
  # from the end of the string to the front, so substring offsets
  # are correct in the target string.

  #translations.sort_by! do |translation|
  #  t1 = translation.original
  #  t1.respond_to?(:offset) ? -t1.offset : 0
  #end
  translations.sort_by! { |t| -t.original.offset }

  translations.each do |translation|
    update(translation.original, translation.result)
  end
end