Class: Translatomatic::Translation::Collection

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

Overview

Stores results of translations. For each original text, there may be zero or more translations from one or more providers.

Instance Method Summary collapse

Constructor Details

#initializeCollection

Create a translation collection



10
11
12
13
14
15
# File 'lib/translatomatic/translation/collection.rb', line 10

def initialize
  # by_provider[provider] = [Result, ...]
  @by_provider = {}
  # by_original[text] = [Result, ...]
  @by_original = {}
end

Instance Method Details

#+(other) ⇒ Collection

Combine this collection with another

Parameters:

Returns:



119
120
121
122
123
124
# File 'lib/translatomatic/translation/collection.rb', line 119

def +(other)
  result = self.class.new
  @by_provider.each_value { |i| result.add(i) }
  other.translations.each { |i| result.add(i) }
  result
end

#[](string) ⇒ Array<Result>

Returns All translations for the given string.

Parameters:

  • string (String, Text)

    Original string

Returns:

  • (Array<Result>)

    All translations for the given string



19
20
21
# File 'lib/translatomatic/translation/collection.rb', line 19

def [](string)
  @by_original[string.to_s]
end

#add(translations) ⇒ void

This method returns an undefined value.

Add a list of translations to the collection

Parameters:

  • translations (Array<Result>)

    Translation results



44
45
46
47
48
49
50
51
# File 'lib/translatomatic/translation/collection.rb', line 44

def add(translations)
  translations = [translations] unless translations.is_a?(Array)
  translations.each do |tr|
    next if tr.result.nil?
    add_to_list(@by_provider, tr.provider, tr)
    add_to_list(@by_original, tr.original, tr)
  end
end

#best_translations(locale) ⇒ Array<Result>

Returns Best translations for each string.

Parameters:

  • locale (Locale)

    Target locale

Returns:

  • (Array<Result>)

    Best translations for each string



106
107
108
# File 'lib/translatomatic/translation/collection.rb', line 106

def best_translations(locale)
  @by_original.keys.collect { |i| get(i, locale) }
end

#countNumber

Returns The number of translations.

Returns:

  • (Number)

    The number of translations



54
55
56
# File 'lib/translatomatic/translation/collection.rb', line 54

def count
  translations.length
end

#descriptionString

Returns String description of all translations.

Returns:

  • (String)

    String description of all translations



135
136
137
# File 'lib/translatomatic/translation/collection.rb', line 135

def description
  translations.collect(&:description).join("\n")
end

#empty?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/translatomatic/translation/collection.rb', line 23

def empty?
  @by_original.empty?
end

#from_providersCollection

Return a new collection with only the translations that came from providers (not from the database).

Returns:



97
98
99
100
101
102
# File 'lib/translatomatic/translation/collection.rb', line 97

def from_providers
  result = self.class.new
  provider_translations = translations.reject(&:from_database)
  result.add(provider_translations)
  result
end

#get(string, locale) ⇒ Result

Returns The best translation for the given string.

Parameters:

  • string (String, Text)

    Original string

  • locale (Locale)

    Target locale

Returns:

  • (Result)

    The best translation for the given string



30
31
32
33
34
35
36
37
38
39
# File 'lib/translatomatic/translation/collection.rb', line 30

def get(string, locale)
  locale = build_locale(locale)
  list = @by_original[string.to_s] || []
  list = sort_by_best_match(list)
  if string.is_a?(Translatomatic::Text) && string.context
    # string has a context
    list = sort_by_context_match(list, string.context, locale)
  end
  list.find { |i| i.result.locale == locale }
end

#present?Boolean

Returns True if there is one or more translations.

Returns:

  • (Boolean)

    True if there is one or more translations



59
60
61
# File 'lib/translatomatic/translation/collection.rb', line 59

def present?
  count > 0
end

#providersArray<String>

Returns A list of providers that translations were sourced from.

Returns:

  • (Array<String>)

    A list of providers that translations were sourced from.



112
113
114
# File 'lib/translatomatic/translation/collection.rb', line 112

def providers
  @by_provider.keys
end

#sentences(parent, locale) ⇒ Array<Result>

Get a list of the best sentence translations for the given parent string.

Parameters:

  • parent (Text)

    Parent text

  • locale (Locale)

    Target locale

Returns:

  • (Array<Result>)

    Substring translation results



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/translatomatic/translation/collection.rb', line 81

def sentences(parent, locale)
  parent.sentences.collect do |sentence|
    # get translation for sentence
    translation = get(sentence, locale)
    # create a new translation with the sentence as the original
    # string, so that we can rely on the offset value.
    if translation
      Result.new(sentence, translation.result, translation.provider,
                 from_database: translation.from_database)
    end
  end.compact
end

#translated?(string, provider) ⇒ boolean

Returns True if there is a translation for the given string.

Parameters:

  • string (String, Text)

    Original string

  • provider (String)

    Provider name

Returns:

  • (boolean)

    True if there is a translation for the given string.



129
130
131
132
# File 'lib/translatomatic/translation/collection.rb', line 129

def translated?(string, provider)
  list = @by_provider[provider.to_s] || []
  list.any? { |tr| tr.original.to_s == string.to_s }
end

#translations(provider = nil) ⇒ Array<Result>

Get translations from this collection. If provider is specified, returns only translations from the given provider, otherwise all translations are returned.

Parameters:

  • provider (String) (defaults to: nil)

    Optional name of a provider.

Returns:

  • (Array<Result>)

    Translation results



68
69
70
71
72
73
74
# File 'lib/translatomatic/translation/collection.rb', line 68

def translations(provider = nil)
  if provider.nil?
    @by_provider.values.flatten
  else
    @by_provider[provider.to_s] || []
  end
end