Class: GoogleTranslator
- Inherits:
-
Object
- Object
- GoogleTranslator
- Defined in:
- lib/google_translator.rb
Constant Summary collapse
- EMPTY_GOOGLE_STRING =
"[ [[\"#{__FILE__} error: empty or erroneous response from Google Translate\"]], [] ]"
Instance Attribute Summary collapse
-
#google_array ⇒ Object
Returns the value of attribute google_array.
-
#google_string ⇒ Object
Returns the value of attribute google_string.
-
#input ⇒ Object
Returns the value of attribute input.
-
#input_language ⇒ Object
Returns the value of attribute input_language.
-
#locale ⇒ Object
Returns the value of attribute locale.
-
#output ⇒ Object
Returns the value of attribute output.
-
#output_language ⇒ Object
Returns the value of attribute output_language.
Instance Method Summary collapse
- #ask_google ⇒ Object
- #initalize ⇒ Object
- #language(input) ⇒ Object
- #query ⇒ Object
- #query_google ⇒ Object
- #translate(input, input_language = 'auto', output_language = locale) ⇒ Object
- #translation ⇒ Object
- #uri_encoded ⇒ Object
- #url ⇒ Object
Instance Attribute Details
#google_array ⇒ Object
Returns the value of attribute google_array.
11 12 13 |
# File 'lib/google_translator.rb', line 11 def google_array @google_array end |
#google_string ⇒ Object
Returns the value of attribute google_string.
11 12 13 |
# File 'lib/google_translator.rb', line 11 def google_string @google_string end |
#input ⇒ Object
Returns the value of attribute input.
11 12 13 |
# File 'lib/google_translator.rb', line 11 def input @input end |
#input_language ⇒ Object
Returns the value of attribute input_language.
11 12 13 |
# File 'lib/google_translator.rb', line 11 def input_language @input_language end |
#locale ⇒ Object
Returns the value of attribute locale.
12 13 14 |
# File 'lib/google_translator.rb', line 12 def locale @locale end |
#output ⇒ Object
Returns the value of attribute output.
12 13 14 |
# File 'lib/google_translator.rb', line 12 def output @output end |
#output_language ⇒ Object
Returns the value of attribute output_language.
11 12 13 |
# File 'lib/google_translator.rb', line 11 def output_language @output_language end |
Instance Method Details
#ask_google ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/google_translator.rb', line 46 def ask_google query_google # Google translate returns a javascript array, which may contain empty # items, i.e. array = [1,,2] is correct javascript # fill empty places of array [1,, ,4, , ] => "[23,-1,-1,4,-1,-1]" array_string = google_string.gsub( /,\s*,/, ',-1,' ).gsub( /,\s*,/, ',-1,' ).gsub( /\[\s*,/, '[-1,' ).gsub( /,\s*\]/, ',-1]' ) self.google_array = eval( array_string ) end |
#initalize ⇒ Object
14 15 16 |
# File 'lib/google_translator.rb', line 14 def initalize self.locale = 'nl' end |
#language(input) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/google_translator.rb', line 30 def language input self.input = input self.input_language = 'auto' self.output_language = locale ask_google google_array[2] end |
#query ⇒ Object
67 68 69 |
# File 'lib/google_translator.rb', line 67 def query "?client=t&sl=#{input_language}&tl=#{output_language}&hl=en&sc=2&ie=UTF-8&oe=UTF-8&prev=btn&ssel=0&tsel=0&q=#{uri_encoded}" end |
#query_google ⇒ Object
71 72 73 74 |
# File 'lib/google_translator.rb', line 71 def query_google self.google_string = Curl.get( url + query ).body_str || EMPTY_GOOGLE_STRING end |
#translate(input, input_language = 'auto', output_language = locale) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/google_translator.rb', line 18 def translate input, input_language='auto', output_language=locale self.input = input self.input_language = input_language self.output_language = output_language ask_google # return the untranslated input when it is translated to white space only self.output = translation.match( /^\s*$/ ) ? input : StringHelpers::equalize( translation, input ) end |
#translation ⇒ Object
41 42 43 44 |
# File 'lib/google_translator.rb', line 41 def translation google_array[0][0][0] end |
#uri_encoded ⇒ Object
62 63 64 65 |
# File 'lib/google_translator.rb', line 62 def uri_encoded URI::encode( input ) end |
#url ⇒ Object
58 59 60 |
# File 'lib/google_translator.rb', line 58 def url "http://translate.google.nl/translate_a/t" end |