class Translatomatic::Provider::Base
Base
class for interfaces to translation APIs @abstract
Constants
- TRANSLATION_RETRIES
Attributes
Listener for translation events
Public Class Methods
# File lib/translatomatic/provider/base.rb, line 22 def initialize(options = {}) @listener = options[:listener] end
@return [boolean] True if a string can have alternative translations
# File lib/translatomatic/provider/base.rb, line 12 def self.supports_alternative_translations? false end
@return [boolean] true if this provider supports html5
<span translate="no"></span> tags.
# File lib/translatomatic/provider/base.rb, line 18 def self.supports_no_translate_html? false end
Public Instance Methods
@return [Array<String>] A list of languages
supported by this provider.
# File lib/translatomatic/provider/base.rb, line 38 def languages [] end
@return [String] The name of this provider.
# File lib/translatomatic/provider/base.rb, line 27 def name self.class.name.demodulize end
@return [String] The name of this provider
# File lib/translatomatic/provider/base.rb, line 32 def to_s name end
Translate strings from one locale to another @param strings [Array<String,Text>] A list of text/strings to translate. @param from [String, Translatomatic::Locale] The locale of the
given strings.
@param to [String, Translatomatic::Locale] The locale to translate to. @return [Array<Translatomatic::Translation::Result>] Translations
# File lib/translatomatic/provider/base.rb, line 48 def translate(strings, from, to) @updated_listener = false @translations = [] @from = from @to = to strings = [strings] unless strings.is_a?(Array) from = build_locale(from) to = build_locale(to) if from.language == to.language strings.each { |i| add_translations(i, i) } else perform_translate(strings, from, to) end @translations end
Private Instance Methods
# File lib/translatomatic/provider/base.rb, line 103 def add_translations(original, result) # successful translation result = [result] unless result.is_a?(Array) result = convert_to_translations(original, result) @listener.update_progress(1) if @listener @translations += result end
# File lib/translatomatic/provider/base.rb, line 122 def batcher(strings, max_count:, max_length:) StringBatcher.new(strings, max_count: max_count, max_length: max_length) end
# File lib/translatomatic/provider/base.rb, line 111 def convert_to_translations(original, result) result.collect { |i| translation(original, i) }.compact end
subclasses that call perform_fetch_translations
must implement this
# File lib/translatomatic/provider/base.rb, line 76 def fetch_translations(_string, _from, _to) raise 'subclass must implement fetch_translations' end
# File lib/translatomatic/provider/base.rb, line 80 def http_client(*args) @http_client ||= Translatomatic::HTTP::Client.new(*args) end
Fetch translations for the given strings, one at a time, by opening a http connection to the given url and calling fetch_translation() on each string. Error handling and recovery is performed by this method. (subclass must implement fetch_translation if this method is used)
# File lib/translatomatic/provider/base.rb, line 89 def perform_fetch_translations(url, strings, from, to) untranslated = strings.dup http_client.start(url) do |_http| until untranslated.empty? # get next string to translate string = untranslated[0] # fetch translation fetch_translations(string, from, to) untranslated.shift end end end
all subclasses must implement this
# File lib/translatomatic/provider/base.rb, line 71 def perform_translate(_strings, _from, _to) raise 'subclass must implement perform_translate' end
# File lib/translatomatic/provider/base.rb, line 115 def translation(original, translated) return nil if translated.blank? string1 = Translatomatic::Text[original, @from] string2 = Translatomatic::Text[translated, @to] Translatomatic::Translation::Result.new(string1, string2, name) end
# File lib/translatomatic/provider/base.rb, line 126 def try_hash(hash, *keys) result = hash keys.each do |key| result ||= {} result = result.is_a?(Hash) ? result[key] : nil end result end