Sha256: ec46e98b5528a8a6aeaeecb6ff7d5e91b502407a9bfd0ce9e1ed0f293e92e225
Contents?: true
Size: 709 Bytes
Versions: 4
Compression:
Stored size: 709 Bytes
Contents
require 'open-uri' module I18nTranslationGeneratorModule class Translator def initialize(lang) @lang, @cache = lang, {} end def translate(word) return @cache[word] if @cache[word] begin w = CGI.escape ActiveSupport::Inflector.humanize(word) json = OpenURI.open_uri("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=#{w}&langpair=en%7C#{@lang}").read result = ActiveSupport::JSON.decode(json) result['responseStatus'] == 200 ? (@cache[word] = result['responseData']['translatedText']) : word rescue => e puts %Q[failed to translate "#{word}" into "#{@lang}" language.] word end end end end
Version data entries
4 entries across 4 versions & 1 rubygems