Module: RubyTranslate
- Defined in:
- lib/ruby_translate.rb
Overview
Uses Google Translate to detect and translate languages.
Google documentation here: code.google.com/apis/ajaxlanguage/documentation/reference.html#_intro_fonje
Class Method Summary collapse
-
.detect(text) ⇒ Object
Detect a language given a string of text.
-
.translate(text, to, from = nil) ⇒ Object
Translate a string from one language to another.
Class Method Details
.detect(text) ⇒ Object
Detect a language given a string of text.
Examples:
RubyTranslate.detect("Mein Luftkissenfahrzeug ist voller Aale")
=> "de"
RubyTranslate.detect("Safisha viatu yangu mara moja!")
=> "sw"
54 55 56 57 58 |
# File 'lib/ruby_translate.rb', line 54 def self.detect(text) json = run('detect', {:q => text}) return json['responseData']['language'] if json['responseStatus'] == 200 raise StandardError, json['responseDetails'] end |
.translate(text, to, from = nil) ⇒ Object
Translate a string from one language to another. The from parameter is optional - if you don’t specify it, detect will be used to guess.
Examples:
RubyTranslate.translate("Mein Luftkissenfahrzeug ist voller Aale", "en")
=> "My hovercraft is full of eels"
RubyTranslate.translate("Il y a un singe qui vole dans l'arbre.", "en")
=> "There is a flying monkey in the tree."
RubyTranslate.translate("They are singing in St Peter's Square", "it")
=> "Essi sono il canto, in Piazza San Pietro"
RubyTranslate.translate("Eu queria um outro pedacinho de Apfelstrudel, por favor", "sv", "pt")
=> "Jag ville ha en bit av Apfelstrudel, tack"
Google Translate is not always perfect! Hopefully you already know that!
37 38 39 40 41 42 |
# File 'lib/ruby_translate.rb', line 37 def self.translate(text, to, from=nil) from = detect(text) if from.nil? json = run('translate', {:langpair => "#{from}|#{to}", :q => text}) return json['responseData']['translatedText'] if json['responseStatus'] == 200 raise StandardError, json['responseDetails'] end |