Class: Translate::RTranslate
- Inherits:
-
Object
- Object
- Translate::RTranslate
- Defined in:
- lib/rtranslate/rtranslate.rb
Constant Summary collapse
- GOOGLE_TRANSLATE_URL =
Google AJAX Language REST Service URL
"http://ajax.googleapis.com/ajax/services/language/translate"
- DEFAULT_VERSION =
Default version of Google AJAX Language API
"1.0"
Instance Attribute Summary collapse
-
#default_from ⇒ Object
readonly
Returns the value of attribute default_from.
-
#default_to ⇒ Object
readonly
Returns the value of attribute default_to.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
- .batch_translate(translate_options) ⇒ Object
- .t ⇒ Object
- .translate(text, from, to) ⇒ Object
- .translate_string_to_languages(text, options) ⇒ Object
- .translate_strings(text_array, from, to) ⇒ Object
Instance Method Summary collapse
-
#batch_translate(translate_options) ⇒ Object
Translate several strings, each into a different language.
-
#initialize(version = DEFAULT_VERSION, key = nil, default_from = nil, default_to = nil) ⇒ RTranslate
constructor
A new instance of RTranslate.
-
#translate(text, options = { }) ⇒ Object
translate the string from a source language to a target language.
-
#translate_string_to_languages(text, option) ⇒ Object
Translate one string into several languages.
-
#translate_strings(text_array, options = { }) ⇒ Object
translate several strings, all from the same source language to the same target language.
Constructor Details
#initialize(version = DEFAULT_VERSION, key = nil, default_from = nil, default_to = nil) ⇒ RTranslate
Returns a new instance of RTranslate.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rtranslate/rtranslate.rb', line 41 def initialize(version = DEFAULT_VERSION, key = nil, default_from = nil, default_to = nil) @version = version @key = key @default_from = default_from @default_to = default_to if @default_from && !(Google::Lanauage.supported?(@default_from)) raise StandardError, "Unsupported source language '#{@default_from}'" end if @default_to && !(Google::Lanauage.supported?(@default_to)) raise StandardError, "Unsupported destination language '#{@default_to}'" end end |
Instance Attribute Details
#default_from ⇒ Object (readonly)
Returns the value of attribute default_from.
20 21 22 |
# File 'lib/rtranslate/rtranslate.rb', line 20 def default_from @default_from end |
#default_to ⇒ Object (readonly)
Returns the value of attribute default_to.
20 21 22 |
# File 'lib/rtranslate/rtranslate.rb', line 20 def default_to @default_to end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
19 20 21 |
# File 'lib/rtranslate/rtranslate.rb', line 19 def key @key end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
19 20 21 |
# File 'lib/rtranslate/rtranslate.rb', line 19 def version @version end |
Class Method Details
.batch_translate(translate_options) ⇒ Object
36 37 38 |
# File 'lib/rtranslate/rtranslate.rb', line 36 def batch_translate() RTranslate.new.batch_translate() end |
.t ⇒ Object
26 27 28 |
# File 'lib/rtranslate/rtranslate.rb', line 26 def translate(text, from, to) RTranslate.new.translate(text, { :from => from, :to => to }) end |
.translate(text, from, to) ⇒ Object
23 24 25 |
# File 'lib/rtranslate/rtranslate.rb', line 23 def translate(text, from, to) RTranslate.new.translate(text, { :from => from, :to => to }) end |
.translate_string_to_languages(text, options) ⇒ Object
32 33 34 |
# File 'lib/rtranslate/rtranslate.rb', line 32 def translate_string_to_languages(text, ) RTranslate.new.translate_string_to_languages(text, ) end |
.translate_strings(text_array, from, to) ⇒ Object
28 29 30 |
# File 'lib/rtranslate/rtranslate.rb', line 28 def translate_strings(text_array, from, to) RTranslate.new.translate_strings(text_array, {:from => from, :to => to}) end |
Instance Method Details
#batch_translate(translate_options) ⇒ Object
Translate several strings, each into a different language.
Examples:
batch_translate([[“China”, => “en”, :to => “zh-CN”], [“Chinese”, => “en”, :to => “zh-CN”]])
112 113 114 115 116 |
# File 'lib/rtranslate/rtranslate.rb', line 112 def batch_translate() .collect do |text, option| self.translate(text, option) end end |
#translate(text, options = { }) ⇒ Object
translate the string from a source language to a target language.
Configuration options:
-
:from
- The source language -
:to
- The target language -
:userip
- The IP address of the end-user on whose behalf the request is being made
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rtranslate/rtranslate.rb', line 62 def translate(text, = { }) from = [:from] || @default_from to = [:to] || @default_to if (from.nil? || Google::Language.supported?(from)) && Google::Language.supported?(to) from = from ? Google::Language.abbrev(from) : nil to = Google::Language.abbrev(to) langpair = "#{from}|#{to}" text.mb_chars.scan(/(.{1,300})/).flatten.inject("") do |result, st| url = "#{GOOGLE_TRANSLATE_URL}?q=#{CGI.escape(st.to_s)}&langpair=#{CGI.escape(langpair)}&v=#{@version}" url << "&key=#{@key}" if @key url << "&userip=#{[:userip]}" if [:userip] result += CGI.unescapeHTML(do_translate(url)) end else raise UnsupportedLanguagePair, "Translation from '#{from}' to '#{to}' isn't supported yet!" end end |
#translate_string_to_languages(text, option) ⇒ Object
Translate one string into several languages.
Configuration options
-
:from
- The source language -
:to
- The target language list
Example:
translate_string_to_languages(“China”, => “en”, :to => [“zh-CN”, “zh-TW”])
101 102 103 104 105 |
# File 'lib/rtranslate/rtranslate.rb', line 101 def translate_string_to_languages(text, option) option[:to].collect do |to| self.translate(text, { :from => option[:from], :to => to }) end end |
#translate_strings(text_array, options = { }) ⇒ Object
translate several strings, all from the same source language to the same target language.
Configuration options
-
:from
- The source language -
:to
- The target language
87 88 89 90 91 |
# File 'lib/rtranslate/rtranslate.rb', line 87 def translate_strings(text_array, = { }) text_array.collect do |text| self.translate(text, ) end end |