Skip to content

Latest commit

 

History

History
executable file
·
47 lines (30 loc) · 1.34 KB

File metadata and controls

executable file
·
47 lines (30 loc) · 1.34 KB

Ruby gem to translate a string from one language to another using Google’s public translate endpoint (https://translate.googleapis.com/translate_a/single). The integration does not require an API key.

gem install language-translator

require 'rubygems'
require 'translator'

tr = Translator.new

# translate from English to Hindi
tr.translate('Hello, World', 'hi', 'en')  #=> "नमस्ते, दुनिया"

# translate from Japanese to English
tr.translate('こんにちは世界', 'en', 'ja')  #=> "Hello world"

# let Google auto-detect the source language (to English)
tr.to_en('¡Hola, mundo!')                #=> "Hello world"

All HTTP calls are mocked via Mocha. You can run the suite without network access:

ruby -Itest test/translator_test.rb

To experiment interactively without installing the gem:

irb -I lib
irb(main):001:0> require 'translator'
=> true
irb(main):002:0> Translator.new.translate('Hello world', 'es', 'en')
=> "Hola Mundo"

Translator::SUPPORTED_LANG_CODES tracks every language code currently available through Google Translate (including legacy aliases like iw and pt-PT). Call Translator::SUPPORTED_LANG_CODES.sort if you need to inspect or display the full list.