module Transfluent::TransfluentTextApi

Public Instance Methods

add_text(text_id, text, options = {}) click to toggle source

Add a single translatable text to Transfluent

Any text added to the system can be queued for translation to any language.

@param [string] text_id an unique id for the text @param [string] text the text to add @param [Hash] options optional options to create text with @option options [String] :group text group @option options [Transfluent::Language] :language text language @option options [true, false] :invalidate_translations invalidate previous translations for the text @option options [true, false] :is_draft is the text a draft

@return [true]

# File lib/transfluent/transfluent_text_api.rb, line 18
def add_text text_id, text, options = {} 
  opts = @defaults.merge({ 
    :invalidate_translations => true,
    :is_draft => false
  }).merge(options)

  group_id = opts[:group]
  language = opts[:language]
 
  

  uri = api_uri('text')
  data = {
    'text_id' => text_id,
    'group_id' => group_id,
    'language' => language.id,
    'text' => text,
    'invalidate_translations' => opts[:invalidate_translations] ? '1' : '0',
    'is_draft' => opts[:is_draft] ? '1' : '0',
    'token' => auth_token
  }

  execute_post(uri, data)
end
add_texts(texts) click to toggle source

Add multiple translatable texts to Transfluent

Any text added to the system can be queued for translation to any language.

@param [Hash] texts texts to create @option texts [Hash] :texts a hash of texts, where key is text id and value is the text to translate @option texts [String] :group text group @option texts [Transfluent::Language] :language text language @option texts [true, false] :invalidate_translations invalidate previous translations for the text @option texts [true, false] :is_draft is the text a draft

# File lib/transfluent/transfluent_text_api.rb, line 54
def add_texts texts
  opts = @defaults.merge({ 
    :invalidate_translations => true
  }).merge(texts)

  texts_to_translate = {}
  texts[:texts].each do | text_id, text |
    texts_to_translate['texts[%s]' % text_id] = text
  end

  group_id = opts[:group]
  language = opts[:language]
 
  uri = api_uri('texts')
  data = {
    'group_id' => group_id,
    'language' => language.id,
    'invalidate_translations' => opts[:invalidate_translations] ? '1' : '0',
    'token' => auth_token
  }

  response = execute_post(uri, data.merge(texts_to_translate))

end
text_orders(options = {}) click to toggle source

Query text order status

@param [Hash] options optional options @option options [String] :group text group. Using default_group if not specified @option options [Fixnum] :limit number items to query. Defaults to 100 @option options [Fixnum] :offset where to start querying. Defaults to 0

@return [Hash]

# File lib/transfluent/transfluent_text_api.rb, line 111
def text_orders options = {}
  opts = @defaults.merge({
    :limit => 100,
    :offset => 0
  }).merge options

  uri = api_uri 'texts/orders'
  query = {
    'group_id' => opts[:group],
    'limit' => opts[:limit],
    'offset' => opts[:offset],
    'token' => auth_token
  }

  response = execute_get uri, query

  response.inject([]) do |memo, v|
    v[:source_language] = Transfluent::Language.get_by_id v[:source_language]
    v[:target_language] = Transfluent::Language.get_by_id v[:target_language]
    memo.push(v)
  end
end
text_translated?(text_id, options = {}) click to toggle source

Request translation status for a text

@param [string] text_id an unique id for the text @param [Hash] options optional options @option options [String] :group text group @option options [Transfluent::Language] :language text language

@return [true, false]

# File lib/transfluent/transfluent_text_api.rb, line 87
def text_translated? text_id, options = {}
  opts = @defaults.merge options

  uri = api_uri 'text/status'
  query = {
    'text_id' => text_id,
    'group_id' => opts[:group],
    'language' => opts[:language].id,
    'token' => auth_token
  }

  response = execute_get uri, query

  response[:is_translated]
end
texts_translate(options) click to toggle source

Request a translation for submitted texts

@param [Hash] options @option options [String] group_id the text group id. Defaults to default_group @option options [Transfluent::Language] language source language of the texts. Defaults to default_language @option options [Array] target_languages array of languages to translate the texts to @option options [1, 2, 3] level translator level. 1 == native, 2 == professional, 3 == pair of translators. Defaults to 2 @option options [Fixnum] max_words maximum words to translate. Defaults to 1000. @option options [String] comment a comment for translators about these texts @option options [URI] callback_url a request will be made to this url when the translations are complete @option options [Array] texts array of text ids to translate

@return [Hash] count of words ordered to translate

# File lib/transfluent/transfluent_text_api.rb, line 147
def texts_translate options
  opts = @defaults.merge({}).merge options

  uri = api_uri 'texts/translate'
  query = {
    'group_id' => opts[:group],
    'source_language' => opts[:language].id,
    'target_languages' => '[%s]' % opts[:target_languages].map { |lang| lang.id },
    'texts' => '[%s]' % opts[:texts].map { |text_id| '{"id":"%s"}' % text_id }.join(','),
    'token' => auth_token
  }
  query['level'] = opts[:level] unless opts[:level].nil?
  query['max_words'] = opts[:max_words] unless opts[:max_words].nil?
  query['comment'] = opts[:comment] unless opts[:comment].nil?
  query['callback_url'] = opts[:callback_uri].to_s unless opts[:callback_uri].nil?

  execute_get uri, query
end