Class: GoogleTranslator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#google_arrayObject

Returns the value of attribute google_array.



11
12
13
# File 'lib/google_translator.rb', line 11

def google_array
  @google_array
end

#google_stringObject

Returns the value of attribute google_string.



11
12
13
# File 'lib/google_translator.rb', line 11

def google_string
  @google_string
end

#inputObject

Returns the value of attribute input.



11
12
13
# File 'lib/google_translator.rb', line 11

def input
  @input
end

#input_languageObject

Returns the value of attribute input_language.



11
12
13
# File 'lib/google_translator.rb', line 11

def input_language
  @input_language
end

#localeObject

Returns the value of attribute locale.



12
13
14
# File 'lib/google_translator.rb', line 12

def locale
  @locale
end

#outputObject

Returns the value of attribute output.



12
13
14
# File 'lib/google_translator.rb', line 12

def output
  @output
end

#output_languageObject

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_googleObject



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

#initalizeObject



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

#queryObject



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_googleObject



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

#translationObject



41
42
43
44
# File 'lib/google_translator.rb', line 41

def translation

	google_array[0][0][0]
end

#uri_encodedObject



62
63
64
65
# File 'lib/google_translator.rb', line 62

def uri_encoded

	URI::encode( input )
end

#urlObject



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

def url
	"http://translate.google.nl/translate_a/t"
end