Module: BingTranslate

Defined in:
lib/bing_translate.rb

Constant Summary collapse

URI =
URI.parse('http://api.microsofttranslator.com/V2/Http.svc/Translate')
QUERY_LIMIT =
2000000
LOCALES_MAP =
{
  :cn => :'zh-CNT'
}

Class Method Summary collapse

Class Method Details

.get_apiObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bing_translate.rb', line 11

def self.get_api
  config_file = "#{Rails.root}/config/goobalize.yml"
  #config_file = File.expand_path('../bing_translate.yml', __FILE__)
  if File.exists?(config_file)
    @@bing_translate_api = YAML.load_file(config_file)['bing_app_id']
    raise GoobalizeTranslateError.new("No APP ID found in '#{config_file}'") if @@bing_translate_api.blank?
    return @@bing_translate_api
  else
    raise GoobalizeTranslateException.new("No config file found '#{config_file}'")
  end
  return nil
end

.map(locale) ⇒ Object



24
25
26
27
# File 'lib/bing_translate.rb', line 24

def self.map(locale)
  mapped = LOCALES_MAP[locale]
  mapped.nil? ? locale : mapped
end

.perform(params) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bing_translate.rb', line 29

def self.perform(params)
  @@bing_translate_api ||= get_api
  query = {}
  query[:text] = CGI::escape(params[:q].to_s[0..QUERY_LIMIT])
  query[:from] = map params[:source]
  query[:to] = map params[:target]
  query.merge!(:appId => @@bing_translate_api, :contentType => 'text/html', :category => 'general')
  data = []
  query.each_pair { |k,v| data << "#{k}=#{v}" }
  query_string = data.join('&')
  result = Net::HTTP.new(URI.host, URI.port).get("#{URI.path}?#{query_string}")
  begin
    Nokogiri.parse(result.body).xpath('//xmlns:string').first.content
  rescue
    raise GoobalizeTranslateError.new('Translation failed')
  end
end