Class: MsTranslate::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ms_translate.rb

Overview

Wrapper for Microsoft Translator V2

The library is a simple API in Ruby for Microsoft Translator V2

The Microsoft Translator services can be used in web or client applications to perform language translation operations. The services support users who are not familiar with the default language of a page or application, or those desiring to communicate with people of a different language group.

Author:

  • Carlo Bertini

Defined Under Namespace

Classes: ArgumentException, HTTPMethodNotAllowed, InvalidAppId, InvalidLanguage

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.appIdObject

The AppId to work with Microsoft Translator



55
56
57
# File 'lib/ms_translate.rb', line 55

def appId
  @appId
end

.fromObject

The language code to translate the text from



58
59
60
# File 'lib/ms_translate.rb', line 58

def from
  @from
end

.toObject

The language code to translate the text into.



61
62
63
# File 'lib/ms_translate.rb', line 61

def to
  @to
end

Class Method Details

.break_sentences(text, language) ⇒ Array

BreakSentences Method

Breaks a piece of text into sentences and returns an array containing the lengths in each sentence.

Parameters:

  • appId (.appId)

    The AppID to work with Microsoft Translator

  • text (String)

    The text to split into sentences.

  • language (String)

    The language code of input text.

Returns:

  • (Array)

    An array of integers representing the lengths of the sentences. The length of the array is the number of sentences, and the values are the length of each sentence.



328
329
330
# File 'lib/ms_translate.rb', line 328

def self.break_sentences(text, language)
  wrapper( __method__.to_s.camelize, {:text => text, :language => language})['ArrayOfint']['int']
end

.detect(text) ⇒ String

Detect Method

Use the Detect Method to identify the language of a selected piece of text

Parameters:

  • appId (.appId)

    The AppID to work with Microsoft Translator

  • text (String)

    some text whose language is to be identified

Returns:

  • (String)

    two-character Language code for the given text



128
129
130
# File 'lib/ms_translate.rb', line 128

def self.detect(text)
  wrapper( __method__.to_s.camelize, { :text => text })['string']
end

.detect_array(texts) ⇒ String

DetectArray Method

Use the DetectArray Method to identify the language of an array of string at once. Performs independent detection of each individual array element and returns a result for each row of the array

Parameters:

  • appId (.appId)

    The AppID to work with Microsoft Translator

  • texts (Array)

    the text from an unknown language

Returns:

  • (String)

    two-character Language codes for each row of the input array

Raises:



149
150
151
# File 'lib/ms_translate.rb', line 149

def self.detect_array(texts)
  raise MsTranslate::Api::HTTPMethodNotAllowed
end

.get_language_names(locale, v1 = false) ⇒ Array

GetLanguageNames Method

Retrieves friendly names for the languages passed in as the parameter languageCodes, and localized using the passed locale language

Parameters:

  • appId (.appId)

    The AppID to work with Microsoft Translator

  • locale (String)

    ISO 639 two-letter lowercase culture code

Returns:

  • (Array)

    languages names supported by the Translator Service, localized into the requested language

Raises:



170
171
172
173
# File 'lib/ms_translate.rb', line 170

def self.get_language_names(locale, v1 = false)
  raise MsTranslate::Api::HTTPMethodNotAllowed
  # wrapper( __method__.to_s.camelize, { :locale => locale.to_s, :v1 => v1 })
end

.get_languages_for_speakObject

GetLanguagesForSpeak Method

Retrieves the languages available for speech synthesis

return [Array] Tthe language codes supported for speech synthesis by the Translator Service

Parameters:

  • appId (.appId)

    The AppID to work with Microsoft Translator



187
188
189
# File 'lib/ms_translate.rb', line 187

def self.get_languages_for_speak
  wrapper( __method__.to_s.camelize )['ArrayOfstring']['string']
end

.get_languages_for_translateArray

GetLanguagesForTranslate Method

Obtain a list of language codes representing languages that are supported by the Translation Service. Translate() and TranslateArray() can translate between any two of these languages

Parameters:

  • appId (.appId)

    The AppID to work with Microsoft Translator

Returns:

  • (Array)

    The language codes supported by the Translator Services



205
206
207
# File 'lib/ms_translate.rb', line 205

def self.get_languages_for_translate
  @available_language ||=  wrapper( __method__.to_s.camelize )['ArrayOfstring']['string']
end

.get_translations(text, max_translations) ⇒ Object

GetTranslations Method

Retrieves an array of translations for a given language pair from the store and the MT engine. GetTranslations differs from Translate as it returns all available translations.

Parameters:

  • appId (.appId)

    The AppID to work with Microsoft Translator

  • from (.from)

    The language code to translate the text from

  • to (.to)

    The language code to translate the text to

  • text (String)

    The text to translate.

  • maxTranslations (Integer)

    the maximum number of translations to return

Raises:



232
233
234
# File 'lib/ms_translate.rb', line 232

def self.get_translations(text, max_translations)
  raise MsTranslate::Api::HTTPMethodNotAllowed
end

.get_translations_array(texts, max_translations) ⇒ Array

GetTranslationsArray Method

Use the GetTranslationsArray method to retrieve multiple translation candidates for multiple source texts.

Returns a GetTranslationsResponse array

Parameters:

  • appId (.appId)

    The AppID to work with Microsoft Translator

  • from (.from)

    The language code to translate the text from

  • to (.to)

    The language code to translate the text to

  • texts (Array)

    An array containing the texts for translation. All strings should be of the same language.

  • maxTranslations (Integer)

    the maximum number of translations to return

Returns:

  • (Array)

Raises:



261
262
263
# File 'lib/ms_translate.rb', line 261

def self.get_translations_array(texts, max_translations)
  raise MsTranslate::Api::HTTPMethodNotAllowed
end

.reset!Object

reset attributes to default value



335
336
337
338
339
340
# File 'lib/ms_translate.rb', line 335

def self.reset!
  @from  = FROM
  @to    = TO
  @appId = nil
  @available_language = nil
end

.speak(text, language) ⇒ Object

Speak Method

Returns a wave or mp3 stream of the passed-in text being spoken in the desired language

Parameters:

  • appId (.appId)

    The AppID to work with Microsoft Translator

  • text (String)

    A sentence or sentences of the specified language to be spoken for the wave stream

  • language (String)

    The supported language code to speak the text in. (see get_languages_for_speak)



279
280
281
# File 'lib/ms_translate.rb', line 279

def self.speak(text, language)
  wrapper( __method__.to_s.camelize, {:text => text, :language => language})
end

.translate(text, query = {}) ⇒ String

Translate Method

Translates a text string from one language to another

Parameters:

  • appId (.appId)

    The AppID to work with Microsoft Translator

  • from (.from)

    The language code to translate the text from

  • to (.to)

    The language code to translate the text to

  • text (String)

    the text to translate

Returns:

  • (String)

    the translated text



105
106
107
108
109
110
111
# File 'lib/ms_translate.rb', line 105

def self.translate(text, query = {})
  query[:from]   = @from  if query[:from].nil?
  query[:to]     = @to    if query[:to].nil?
  query[:text]   = text

  wrapper( __method__.to_s.camelize,  query)['string']
end

.translate_array(texts) ⇒ Array

TranslateArray Method

Use the TranslateArray method to retrieve translations for multiple source texts.

Parameters:

  • appId (.appId)

    The AppID to work with Microsoft Translator

  • from (.from)

    The language code to translate the text from

  • to (.to)

    The language code to translate the text to

  • texts (Array)

    An array containing the texts for translation. All strings should be of the same language.

Returns:

  • (Array)

    Returns a TranslateArrayResponse array

Raises:



304
305
306
# File 'lib/ms_translate.rb', line 304

def self.translate_array(texts)
  raise MsTranslate::Api::HTTPMethodNotAllowed
end