Translate small text snippet with the free and unlimited Google Translate API.
- ruby
>= 2.3.0 - gem
bundler
Add this line to your application's Gemfile:
gem 'google_translate', git: '[email protected]:masterT/google-translate.git'And then execute:
bundleOr install it yourself as:
git clone [email protected]:masterT/google-translate.git
cd google-translate
gem build google_translate.gemspec
gem install google_translate-0.0.1.gemrequire 'google_translate'
# Translate 'hi' from english to french.
GoogleTranslate.translate('Hi', {source: :en, target: :fr})
# Auto detect source language.
GoogleTranslate.translate('Hi', {target: :fr})
# => "salut"
# Option target is required.
GoogleTranslate.translate('Hi', {source: :en})
# ArgumentError:
# Expect options 'target'.
GoogleTranslate::VERSION
# => "0.0.1"$ google_translate -h
usage: google_translate [options] <text>
-s, --source the source language
-t, --target the target language
-v, --version
-h, --help
$ google_translate -t fr 'Hi how are you?'
Salut comment ca va?
$ google_translate -s fr -t fr 'Hi how are you?'
Hi how are you?
$ google_translate -v
0.0.1
Install dependencies:
bundleTest the CLI locally:
./bin/google_translate -t fr 'Hi how are you?'
Salut comment ca va?Test the gem locally using IRB:
$ irb -I lib -r google_translate
irb(main):001:0> GoogleTranslate.translate('Hi', {source: :en, target: :fr})
=> "salut"
irb(main):002:0> GoogleTranslate::VERSION
=> "0.0.1"
The -I option loads the lib files and the -r option loads the module google_translate.
Execute:
bundle exec rspec