Class: MsTranslate::Api
- Inherits:
-
Object
- Object
- MsTranslate::Api
- 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.
Defined Under Namespace
Classes: ArgumentException, HTTPMethodNotAllowed, InvalidAppId, InvalidLanguage
Class Attribute Summary collapse
-
.appId ⇒ Object
The AppId to work with Microsoft Translator.
-
.from ⇒ Object
The language code to translate the text from.
-
.to ⇒ Object
The language code to translate the text into.
Class Method Summary collapse
-
.break_sentences(text, language) ⇒ Array
BreakSentences Method.
-
.detect(text) ⇒ String
Detect Method.
-
.detect_array(texts) ⇒ String
DetectArray Method.
-
.get_language_names(locale, v1 = false) ⇒ Array
GetLanguageNames Method.
-
.get_languages_for_speak ⇒ Object
GetLanguagesForSpeak Method.
-
.get_languages_for_translate ⇒ Array
GetLanguagesForTranslate Method.
-
.get_translations(text, max_translations) ⇒ Object
GetTranslations Method.
-
.get_translations_array(texts, max_translations) ⇒ Array
GetTranslationsArray Method.
-
.reset! ⇒ Object
reset attributes to default value.
-
.speak(text, language) ⇒ Object
Speak Method.
-
.translate(text, query = {}) ⇒ String
Translate Method.
-
.translate_array(texts) ⇒ Array
TranslateArray Method.
Class Attribute Details
.appId ⇒ Object
The AppId to work with Microsoft Translator
55 56 57 |
# File 'lib/ms_translate.rb', line 55 def appId @appId end |
.from ⇒ Object
The language code to translate the text from
58 59 60 |
# File 'lib/ms_translate.rb', line 58 def from @from end |
.to ⇒ Object
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.
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
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
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
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_speak ⇒ Object
GetLanguagesForSpeak Method
Retrieves the languages available for speech synthesis
return [Array] Tthe language codes supported for speech synthesis by the Translator Service
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_translate ⇒ Array
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
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.
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
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
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
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.
304 305 306 |
# File 'lib/ms_translate.rb', line 304 def self.translate_array(texts) raise MsTranslate::Api::HTTPMethodNotAllowed end |