Skip to content
A Ruby library that adds language translation methods to strings, backed by the Google Translate API. http://rubydoc.info/github/jimmycuadra/to_lang/master/frames
Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
.gitignore
Gemfile
Gemfile.lock
README.textile
Rakefile
to_lang.gemspec

README.textile

to_lang

to_lang is a Ruby gem that uses the Google Translate API to allow you to perform a translation directly on a string.

Usage

To use to_lang, require the library, then call ToLang.start with your Google Translate API key to initialize the connector and include the methods in String. Then you have access to String#translate.

translate(to, *options)
to (required): a string indicating the language to translate to, e.g. “es”
options[:from] (optional): a string indicating the language of the original string, used when automatic language detection is not accurate

Examples

Load and initialize to_lang:

require ‘to_lang’
ToLang.start(‘YOUR_GOOGLE_TRANSLATE_API_KEY’)

Translate some text to Spanish:

“Very cool gem!”.translate(‘es’)
# “Muy fresco joya!”

If the source string is of an ambiguous language, the translation might not work as expected. In this case, you can specify the source language explicitly with the :from option:

“a pie”.translate(‘es’)
# “a pie”
“a pie”.translate(‘es’, :from => ‘en’)
# “un pastel”

Roadmap

- Add tests for current code. to_lang is in its infancy so use at your own risk.
- Add automagic sugar methods for translating to specific languages, such as to_spanish and to_spanish_from_english

Feedback and Contributions

Feedback is appreciated, as are patch contributions. Feel free to fork the project and send me a pull request.

Something went wrong with that request. Please try again.