Class: ChinesePermalink::Translate

Inherits:
Object
  • Object
show all
Defined in:
lib/chinese_permalink.rb

Class Method Summary collapse

Class Method Details

.t(text) ⇒ Object



68
69
70
71
72
# File 'lib/chinese_permalink.rb', line 68

def t(text)
  response = Net::HTTP.get(URI.parse(URI.encode(translate_url + text)))
  response =~ %r|<string.*?>(.*?)</string>|
  $1
end

.translate_urlObject



74
75
76
77
78
79
80
81
# File 'lib/chinese_permalink.rb', line 74

def translate_url
  @translate_url ||= begin
    config = YAML.load(File.open(File.join(Rails.root, "config/chinese_permalink.yml")))
    app_id = config['bing']['app_id']
    language = config['bing']['language']
    "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=#{app_id}&from=#{language}&to=en&text="
  end
end