Module: Transfluent::TransfluentTextApi

Included in:
Api
Defined in:
lib/transfluent/transfluent_text_api.rb

Instance Method Summary collapse

Instance Method Details

#add_text(text_id, text, options = {}) ⇒ true

Add a single translatable text to Transfluent

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

Parameters:

  • text_id (string)

    an unique id for the text

  • text (string)

    the text to add

  • options (Hash) (defaults to: {})

    optional options to create text with

Options Hash (options):

  • :group (String)

    text group

  • :language (Transfluent::Language)

    text language

  • :invalidate_translations (true, false)

    invalidate previous translations for the text

  • :is_draft (true, false)

    is the text a draft

Returns:

  • (true)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object

Add multiple translatable texts to Transfluent

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

Parameters:

  • texts (Hash)

    texts to create

Options Hash (texts):

  • :texts (Hash)

    a hash of texts, where key is text id and value is the text to translate

  • :group (String)

    text group

  • :language (Transfluent::Language)

    text language

  • :invalidate_translations (true, false)

    invalidate previous translations for the text

  • :is_draft (true, false)

    is the text a draft



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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 = {}) ⇒ Hash

Query text order status

Parameters:

  • options (Hash) (defaults to: {})

    optional options

Options Hash (options):

  • :group (String)

    text group. Using default_group if not specified

  • :limit (Fixnum)

    number items to query. Defaults to 100

  • :offset (Fixnum)

    where to start querying. Defaults to 0

Returns:

  • (Hash)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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 = {}) ⇒ true, false

Request translation status for a text

Parameters:

  • text_id (string)

    an unique id for the text

  • options (Hash) (defaults to: {})

    optional options

Options Hash (options):

Returns:

  • (true, false)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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) ⇒ Hash

Request a translation for submitted texts

Parameters:

  • options (Hash)

Options Hash (options):

  • group_id (String)

    the text group id. Defaults to default_group

  • language (Transfluent::Language)

    source language of the texts. Defaults to default_language

  • target_languages (Array)

    array of languages to translate the texts to

  • level (1, 2, 3)

    translator level. 1 == native, 2 == professional, 3 == pair of translators. Defaults to 2

  • max_words (Fixnum)

    maximum words to translate. Defaults to 1000.

  • comment (String)

    a comment for translators about these texts

  • callback_url (URI)

    a request will be made to this url when the translations are complete

  • texts (Array)

    array of text ids to translate

Returns:

  • (Hash)

    count of words ordered to translate



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# 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