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.



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

Returns the value of attribute default_to.



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

def default_to
  @default_to
end

#keyObject (readonly)

Returns the value of attribute key.



19
20
21
# File 'lib/rtranslate/rtranslate.rb', line 19

def key
  @key
end

#versionObject (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(translate_options)
  RTranslate.new.batch_translate(translate_options)
end

.tObject



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, options)
  RTranslate.new.translate_string_to_languages(text, options)
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(translate_options)
  translate_options.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, 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}"

    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=#{options[:userip]}" if options[: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, options = { })
  text_array.collect do |text|
    self.translate(text, options)
  end
end