Class: Translate::RTranslate

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

Class Method Summary collapse

Instance Method Summary collapse

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_fromObject (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_toObject (readonly)

Returns the value of attribute default_to.



22
23
24
# File 'lib/rtranslate/rtranslate.rb', line 22

def default_to
  @default_to
end

#keyObject

Returns the value of attribute key.



21
22
23
# File 'lib/rtranslate/rtranslate.rb', line 21

def key
  @key
end

#versionObject

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(translate_options, options = {})
  RTranslate.new.batch_translate(translate_options, options)
end

.tObject



32
33
34
35
36
37
38
# File 'lib/rtranslate/rtranslate.rb', line 32

def translate(text, from, to, options = {})
  if options[: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, options = {})
  if options[: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, options)
  RTranslate.new.translate_string_to_languages(text, options)
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, options = {})
  method = options[: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(translate_options, options = {})
  translate_options.collect do |text, option|
    if options[: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, options = { })
  from = options[:from] || @default_from
  to = options[: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)
    post_options = {:langpair => "#{from}|#{to}", :v => @version}
    post_options[:key] = @key if @key
    
    if text && text.mb_chars
      text.mb_chars.scan(/(.{1,500})/).inject("") do |result, st|
        url = GOOGLE_TRANSLATE_URL
        post_options[:q] = st
        result += do_post_translate(url,post_options)
      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, options = { })
  from = options[:from] || @default_from
  to = options[: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, options)
  options[:to].collect do |to|
    if options[:method] == :post
      self.post_translate(text, { :from => options[:from], :to => to })
    else
      self.translate(text, { :from => options[: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, options = { })
  text_array.collect do |text|
    if options[:method] == :post
      self.post_translate(text, options)
    else
      self.translate(text, options)
    end
  end
end