class Translatomatic::Provider::MyMemory

Interface to the MyMemory translation API @see mymemory.translated.net/doc/

Constants

GET_URL
MAIN_URL
UPLOAD_URL

Public Class Methods

new(options = {}) click to toggle source

Create a new MyMemory provider instance

Calls superclass method Translatomatic::Provider::Base::new
# File lib/translatomatic/provider/my_memory.rb, line 12
def initialize(options = {})
  super(options)
  @key = options[:mymemory_api_key] || ENV['MYMEMORY_API_KEY']
  @email = options[:mymemory_email] || ENV['MYMEMORY_EMAIL']
  @query_options = {}
  @query_options[:de] = @email if @email
  @query_options.merge!(key: @key) if @key
end

Public Instance Methods

languages() click to toggle source

(see Base#languages)

# File lib/translatomatic/provider/my_memory.rb, line 22
def languages
  Locale.language_codes
end
upload(tmx) click to toggle source

Upload a set of translations to MyMemory @param tmx [Translatomatic::TMX::Document] TMX document @return [void]

# File lib/translatomatic/provider/my_memory.rb, line 29
def upload(tmx)
  body = [
    { key: :tmx, filename: 'import.tmx',
      content: tmx.to_xml, mime_type: 'application/xml' },
    { key: :private, value: 0 }
  ]
  response = http_client.post(UPLOAD_URL, body)
  log.debug(t('provider.share_response',
              response: response.body.inspect))
end

Private Instance Methods

fetch_translations(string, from, to) click to toggle source
# File lib/translatomatic/provider/my_memory.rb, line 50
def fetch_translations(string, from, to)
  response = http_client.get(GET_URL, {
    langpair: from.to_s + '|' + to.to_s,
    q: string # multiple q strings not supported (tested 20180101)
  }.merge(@query_options))

  log.debug("#{name} response: #{response.body}")
  data = JSON.parse(response.body)
  # matches = data['matches'] # all translations
  # matches.collect { |i| match_data(i) }
  result = try_hash(data, 'responseData', 'translatedText')
  add_translations(string, result)
end
match_data(match) click to toggle source

mymemory.translated.net/doc/features.php

# File lib/translatomatic/provider/my_memory.rb, line 65
def match_data(match)
  {
    translation: match['translation'],
    quality: match['quality'],
    usage_count: match['usage-count'],
    match: match['match'], # partial matches, see features.php above
  }
end
perform_translate(strings, from, to) click to toggle source
# File lib/translatomatic/provider/my_memory.rb, line 46
def perform_translate(strings, from, to)
  perform_fetch_translations(GET_URL, strings, from, to)
end