class GoogleTranslator

Constants

EMPTY_GOOGLE_STRING

Attributes

google_array[RW]
google_string[RW]
input[RW]
input_language[RW]
locale[RW]
output[RW]
output_language[RW]

Public Instance Methods

ask_google() click to toggle source
# 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() click to toggle source
# File lib/google_translator.rb, line 14
def initalize
        self.locale = 'nl'
end
language(input) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/google_translator.rb, line 41
def translation

        google_array[0][0][0]
end
uri_encoded() click to toggle source
# File lib/google_translator.rb, line 62
def uri_encoded

        URI::encode( input )
end
url() click to toggle source
# File lib/google_translator.rb, line 58
def url
        "http://translate.google.nl/translate_a/t"
end