Class: Translator
- Inherits:
-
Object
- Object
- Translator
- Defined in:
- lib/translator.rb
Constant Summary collapse
- Site_Url =
'translate.googleapis.com'- Request_Uri =
"/translate_a/single"- SUPPORTED_LANG_CODES =
[ 'af', 'sq', 'am', 'ar', 'hy', 'az', 'eu', 'be', 'bn', 'bs', 'bg', 'ca', 'ceb', 'ny', 'zh-CN', 'zh-TW', 'co', 'hr', 'cs', 'da', 'nl', 'en', 'eo', 'et', 'fil', 'fi', 'fr', 'fy', 'gl', 'ka', 'de', 'el', 'gu', 'ht', 'ha', 'haw', 'iw', 'he', 'hi', 'hmn', 'hu', 'is', 'ig', 'id', 'ga', 'it', 'ja', 'jw', 'kn', 'kk', 'km', 'rw', 'ko', 'ku', 'ky', 'lo', 'la', 'lv', 'lt', 'lb', 'mk', 'mg', 'ms', 'ml', 'mt', 'mi', 'mr', 'mn', 'my', 'ne', 'no', 'or', 'ps', 'fa', 'pl', 'pt', 'pt-PT', 'pa', 'ro', 'ru', 'sm', 'gd', 'sr', 'st', 'sn', 'sd', 'si', 'sk', 'sl', 'so', 'es', 'su', 'sw', 'sv', 'tg', 'ta', 'tt', 'te', 'th', 'tr', 'tk', 'uk', 'ur', 'ug', 'uz', 'vi', 'cy', 'xh', 'yi', 'yo', 'zu' ]
Instance Method Summary collapse
-
#to_en(text) ⇒ Object
Public: Convenience helper to translate from an unknown language to English.
-
#translate(text, to, from = 'en') ⇒ Object
Public: Translate text using Google’s HTTPS endpoint.
Instance Method Details
#to_en(text) ⇒ Object
Public: Convenience helper to translate from an unknown language to English.
Equivalent to translate(text, “en”, “”) which triggers auto language detection.
55 56 57 |
# File 'lib/translator.rb', line 55 def to_en(text) translate( text, "en", "" ) end |
#translate(text, to, from = 'en') ⇒ Object
Public: Translate text using Google’s HTTPS endpoint.
text - String containing the source text. to - Language code for the target language (see SUPPORTED_LANG_CODES). from - Optional source language code. Provide an empty string to let Google auto-detect.
Returns the translated String or nil if Google returns an empty result.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/translator.rb', line 36 def translate(text, to, from='en') begin raise UnSupportedLanguage unless SUPPORTED_LANG_CODES.include?(to) raise UnSupportedLanguage unless SUPPORTED_LANG_CODES.include?(from) unless from.empty? # letting to translate from unknown language response = Net::HTTP.get_response(request_uri(text, to, from)) raise StandardError, "Translation API responded with #{response.code}" unless response.code.to_i == 200 parse_translation_response(response.body) rescue UnSupportedLanguage raise UnSupportedLanguage.new rescue => err_msg puts "#{err_msg}" end end |