masterT/google-translate

Translate small text snippet with the free and unlimited Google Translate API

★ 2Forks 0RubyGitHub ↗Compare

README

GoogleTranslate

Translate small text snippet with the free and unlimited Google Translate API.

Reference

Requirements

  • ruby >= 2.3.0
  • gem bundler

Installation

Add this line to your application's Gemfile:

gem 'google_translate', git: '[email protected]:masterT/google-translate.git'

And then execute:

bundle

Or 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.gem

Usage

require '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"

CLI

$ 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

Development

Install dependencies:

bundle

Test 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.

Test

Execute:

bundle exec rspec

License

CC0

Contributors

masterT

Issues