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
Returns the value of attribute key.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
- .batch_translate(translate_options, options = {}) ⇒ Object
- .t ⇒ Object
- .translate(text, from, to, options = {}) ⇒ Object
- .translate_string_to_languages(text, options) ⇒ Object
- .translate_strings(text_array, from, to, options = {}) ⇒ Object
Instance Method Summary collapse
-
#batch_translate(translate_options, 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.
-
#post_translate(text, options = { }) ⇒ Object
This one for a POST request.
-
#translate(text, options = { }) ⇒ Object
translate the string from a source language to a target language.
-
#translate_string_to_languages(text, options) ⇒ 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.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rtranslate/rtranslate.rb', line 48 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.
22 23 24 |
# File 'lib/rtranslate/rtranslate.rb', line 22 def default_from @default_from end |
#default_to ⇒ Object (readonly)
Returns the value of attribute default_to.
22 23 24 |
# File 'lib/rtranslate/rtranslate.rb', line 22 def default_to @default_to end |
#key ⇒ Object
Returns the value of attribute key.
21 22 23 |
# File 'lib/rtranslate/rtranslate.rb', line 21 def key @key end |
#version ⇒ Object
Returns the value of attribute version.
21 22 23 |
# File 'lib/rtranslate/rtranslate.rb', line 21 def version @version end |
Class Method Details
.batch_translate(translate_options, options = {}) ⇒ Object
43 44 45 |
# File 'lib/rtranslate/rtranslate.rb', line 43 def batch_translate(, = {}) RTranslate.new.batch_translate(, ) end |
.t ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/rtranslate/rtranslate.rb', line 32 def translate(text, from, to, = {}) if [:method] == :post RTranslate.new.post_translate(text, { :from => from, :to => to }) else RTranslate.new.translate(text, { :from => from, :to => to }) end end |
.translate(text, from, to, options = {}) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/rtranslate/rtranslate.rb', line 25 def translate(text, from, to, = {}) if [:method] == :post RTranslate.new.post_translate(text, { :from => from, :to => to }) else RTranslate.new.translate(text, { :from => from, :to => to }) end end |
.translate_string_to_languages(text, options) ⇒ Object
39 40 41 |
# File 'lib/rtranslate/rtranslate.rb', line 39 def translate_string_to_languages(text, ) RTranslate.new.translate_string_to_languages(text, ) end |
.translate_strings(text_array, from, to, options = {}) ⇒ Object
34 35 36 37 |
# File 'lib/rtranslate/rtranslate.rb', line 34 def translate_strings(text_array, from, to, = {}) method = [:method] || :get RTranslate.new.translate_strings(text_array, {:from => from, :to => to, :method => method}) end |
Instance Method Details
#batch_translate(translate_options, options = {}) ⇒ Object
Translate several strings, each into a different language.
Examples:
batch_translate([[“China”, => “en”, :to => “zh-CN”], [“Chinese”, => “en”, :to => “zh-CN”]])
150 151 152 153 154 155 156 157 158 |
# File 'lib/rtranslate/rtranslate.rb', line 150 def batch_translate(, = {}) .collect do |text, option| if [:method] == :post self.post_translate(text, option) else self.translate(text, option) end end end |
#post_translate(text, options = { }) ⇒ Object
This one for a POST request
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rtranslate/rtranslate.rb', line 91 def post_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}", :v => @version} [:key] = @key if @key if text && text.mb_chars text.mb_chars.scan(/(.{1,500})/).inject("") do |result, st| url = GOOGLE_TRANSLATE_URL [:q] = st result += do_post_translate(url,) end end else raise UnsupportedLanguagePair, "Translation from '#{from}' to '#{to}' isn't supported yet!" 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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rtranslate/rtranslate.rb', line 68 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}" if text && text.mb_chars text.mb_chars.scan(/(.{1,500})/).inject("") do |result, st| url = "#{GOOGLE_TRANSLATE_URL}?q=#{st}&langpair=#{langpair}&v=#{@version}" if @key url << "&key=#{@key}" end result += do_translate(url) end end else raise UnsupportedLanguagePair, "Translation from '#{from}' to '#{to}' isn't supported yet!" end end |
#translate_string_to_languages(text, options) ⇒ 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”])
135 136 137 138 139 140 141 142 143 |
# File 'lib/rtranslate/rtranslate.rb', line 135 def translate_string_to_languages(text, ) [:to].collect do |to| if [:method] == :post self.post_translate(text, { :from => [:from], :to => to }) else self.translate(text, { :from => [:from], :to => to }) end 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
117 118 119 120 121 122 123 124 125 |
# File 'lib/rtranslate/rtranslate.rb', line 117 def translate_strings(text_array, = { }) text_array.collect do |text| if [:method] == :post self.post_translate(text, ) else self.translate(text, ) end end end |