Module: Common::GoogleTranslator
- Included in:
- WebStuff
- Defined in:
- lib/common/google_translator.rb
Class Method Summary collapse
- .standarize_translation(original, translation) ⇒ Object
- .translate(str, from_locale, to_locale) ⇒ Object
Class Method Details
.standarize_translation(original, translation) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/common/google_translator.rb', line 31 def self.standarize_translation(original, translation) translation = translation.gsub(/\n/, "").gsub(/% s/, "%s").gsub(/ /, " "). gsub(/< ?(\/?) (\w+) >/, '<\1\2>').gsub(/ ([\.,!?])( |$)/, '\1') translation_data = translation.scan(/{ ?{.*?} ?}/) original_data = original.scan(/{{.*?}}/) if translation_data.present? translation_data.each_with_index do |trans, index| translation = translation.gsub(trans, original_data[index]) end end translation end |
.translate(str, from_locale, to_locale) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/common/google_translator.rb', line 9 def self.translate(str, from_locale, to_locale) str = str.strip return "" if str.blank? params = { "client" => "t", "sl" => from_locale, "tl" => to_locale, "hl" => "en", "sc" => "2", "ie" => "UTF-8", "oe" => "UTF-8", "oc" => "1", "otf" => "2", "ssel" => "3", "tsel" => "6", "q" => str } result = agent.get("http://translate.google.com.vn/translate_a/t", params) json = JSON(result.body.match(/^\[(\[\[.*?\]\])/)[1]) json[0][0] end |