• Stars
    star
    299
  • Rank 139,269 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 14 years ago
  • Updated almost 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Translate Ruby strings and arrays with Google Translate.

Gem Version Build Status Code Climate

to_lang

to_lang is a Ruby library that adds language translation methods to strings and arrays, backed by the Google Translate API.

Installation

Simply run:

gem install to_lang

Usage

To use to_lang, require the library, then call ToLang.start with your Google Translate API key. At this point you will have access to all the new translation methods, which take the form to_language, where "language" is the language you wish to translate to.

Google Translate attempts to detect the source language, but you can specify it explicitly by calling methods in the form to_target_language_from_source_language, where "target language" is the language you are translating to and "source_language" is the language you are starting with. An inverted form with equivalent functionality, from_source_language_to_target_language is also available. These methods are generated dynamically and will not appear in a calls to String.instance_methods or Array.instance_methods until they have been called once. Strings and arrays will, however, respond_to? these methods prior to their dynamic definition.

The dynamic methods are simply syntactic sugar for String#translate and Array#translate, which you can use directly as well.

to_lang also comes with a command line utility for quick translations from the shell.

String Examples

Load and initialize to_lang:

require 'to_lang'

ToLang.start('YOUR_GOOGLE_TRANSLATE_API_KEY')

Translate some text to Spanish:

"Very cool gem!".to_spanish # => "Muy fresco joya!"

A case where the source language is ambiguous:

"a pie".to_spanish # => "a pie"
"a pie".to_spanish_from_english # => "un pastel"

Or equivalently:

"a pie".from_english_to_spanish # => "un pastel"

Using String#translate directly:

"hello world".translate('es') # => "hola mundo"
"a pie".translate('es', :from => 'en') # => "un pastel"

Array Examples

Arrays can be used to translate a batch of strings in a single method call and a single HTTP request. The exact same methods shown above work for arrays as well. For example, to translate an array of strings to Spanish:

["One", "Two", "Three"].to_spanish # => ["Uno", "Dos", "Tres"]

Debugging

translate also has the advantage of allowing you to get debug output for a translation. translate accepts a :debug option with three possible values: :request, :response, and :all. :request will cause the method to return a hash of the parameters that will be sent to the Google Translate API. :response will cause the method to return the full response from the API call as a hash. :all will cause the method to return a hash which contains both the request hash and the full response.

"hello world".translate('es', :debug => :request)
# => {:key=>"my_key", :q=>"hello world", :target=>"es"}
"hello world".translate('es', :debug => :response)
# => {"data"=>{"translations"=>[{"translatedText"=>"hola mundo", "detectedSourceLanguage"=>"en"}]}}
"hello world".translate('es', :debug => :all)
# => {:request=>{:key=>"my_key", :q=>"hello world", :target=>"es"},
#    :response=>{"data"=>{"translations"=>[{"translatedText"=>"hola mundo",
#    "detectedSourceLanguage"=>"en"}]}}}

Command Line Interface

The command line utility to_lang has the following interface:

to_lang [--key API_KEY] [--from SOURCE_LANGUAGE] --to DESTINATION_LANGUAGE STRING [STRING, ...]

to_lang accepts a space separated list of strings to translate. At least one string is required, as is the --to option, which accepts a language code (e.g. "es"). to_lang will attempt to load a Google Translate API key from the GOOGLE_TRANSLATE_API_KEY environment variable. If one is not available, it must be passed in from the command line with the --key option. For complete usage instructions, invoke the utility with the --help option.

Examples:

A simple translation with the key being passed in directly from the command line:

to_lang --key YOUR_GOOGLE_TRANSLATE_API_KEY --to es "hello world"
hola mundo

With the key in an environment variable and multiple strings:

to_lang --to es "hello world" "a pie"
hola mundo
a pie

Specifying the source language:

to_lang --from en --to es "hello world" "a pie"
hola mundo
un pastel

Supported Languages

to_lang adds the following methods to strings and arrays. Each of these methods can be called with an explicit source language by appending _from_source_language or prepending from_source_language_ to the method name.

  • to_afrikaans
  • to_albanian
  • to_arabic
  • to_belarusian
  • to_bulgarian
  • to_catalan
  • to_simplified_chinese
  • to_traditional_chinese
  • to_croatian
  • to_czech
  • to_danish
  • to_dutch
  • to_english
  • to_estonian
  • to_filipino
  • to_finnish
  • to_french
  • to_galician
  • to_german
  • to_greek
  • to_haitian_creole
  • to_hebrew
  • to_hindi
  • to_hungarian
  • to_icelandic
  • to_indonesian
  • to_irish
  • to_italian
  • to_japanese
  • to_latvian
  • to_lithuanian
  • to_macedonian
  • to_malay
  • to_maltese
  • to_norwegian
  • to_persian
  • to_polish
  • to_portuguese
  • to_romanian
  • to_russian
  • to_serbian
  • to_slovak
  • to_slovenian
  • to_spanish
  • to_swahili
  • to_swedish
  • to_thai
  • to_turkish
  • to_ukrainian
  • to_vietnamese
  • to_welsh
  • to_yiddish

Documentation

API documentation can be found at rubydoc.info.

Feedback and Contributions

Feedback is greatly appreciated. If you have any problems with to_lang, please open a new issue. Make sure you are using the latest version of the gem, or HEAD if you've cloned the Git repository directly. Please include debugging output from using the :debug option of translate, if relevant to your issue. If you'd like to fix bugs, add features, or improve the library in general, feel free to fork the project and send me a pull request with your changes.

More Repositories

1

rust-etcd

An etcd client library for Rust.
Rust
142
star
2

retry

A Rust library to retry some code until its return value satisfies a condition.
Rust
109
star
3

vagrant-librarian-chef

*UNMAINTAINED* A Vagrant plugin to install Chef cookbooks using Librarian-Chef.
Ruby
78
star
4

docker-rust

DEPRECATED. Use https://hub.docker.com/_/rust/ instead.
48
star
5

jquery-tmpl-rails

jQuery Templates for the Rails asset pipeline.
Ruby
46
star
6

shellwords

Manipulate strings according to the word parsing rules of the UNIX Bourne shell.
TypeScript
37
star
7

ghlabel

Automatically creates and deletes labels on GitHub Issues to match a template
Rust
35
star
8

bang

Text snippets on the command line.
CoffeeScript
32
star
9

guerilla_patch

Monkey patch objects using Ruby 2.0 refinements, if available.
Ruby
27
star
10

lita-digitalocean

A Lita handler for managing DigitalOcean services.
Ruby
25
star
11

rust-shellwords

Manipulate strings according to the word parsing rules of the UNIX Bourne shell.
Rust
24
star
12

structure

Add some basic structure to your client-side JavaScript.
JavaScript
24
star
13

lita-confirmation

A Lita extension to require confirmation for commands.
Ruby
21
star
14

lita-karma

A Lita handler for tracking karma points for arbitrary terms.
Ruby
21
star
15

validations

Rust library to provide validations for arbitrary types.
Rust
20
star
16

lita-google-images

A Lita handler for fetching images from Google.
Ruby
16
star
17

rust-hcl

A Rust library for working with the HashiCorp Configuration Language (HCL).
Rust
15
star
18

kube-up-artifacts

Artifacts generated by the official Kubernetes cluster creation script.
Shell
13
star
19

lita-keyword-arguments

A Lita extension to extract CLI-style arguments from messages.
Ruby
13
star
20

vagrant-sprinkle

A Sprinkle provisioner for Vagrant.
Ruby
12
star
21

lita-retro

A Lita handler to help manage topics for regular team retrospective meetings.
Ruby
12
star
22

lita-github-web-hooks

GitHub web hook integration for Lita.
Ruby
9
star
23

lita-google

A Lita handler for returning Google search results.
Ruby
7
star
24

dotfiles

Various configuration files that are stored in my home directory on macOS and Linux systems.
Lua
7
star
25

lita-travis

A Lita handler for receiving notifications from Travis CI.
Ruby
6
star
26

tam_tam

Parse, filter, and analyze logs from online chat clients.
Ruby
6
star
27

san-francisco-food-trucks

An interactive map of food trucks in San Francisco.
Ruby
5
star
28

lita-youtube

A Lita handler for displaying YouTube video information.
Ruby
5
star
29

terraform-provider-vagrant

Vagrant provider for Terraform.
Go
5
star
30

lita-key-value

A Lita handler for storing snippets of text.
Ruby
5
star
31

extractor

A JavaScript library for managing the global namespace.
CoffeeScript
5
star
32

lita-wordnik

A Lita handler for dictionary functionality backed by Wordnik.
Ruby
5
star
33

lita-coin

A Lita handler for flipping a coin.
Ruby
4
star
34

lita-urban-dictionary

A Lita handler for fetching definitions from Urban Dictionary.
Ruby
4
star
35

gong

Text snippets on the command line.
Go
3
star
36

rump

Text snippets on the command line.
Rust
3
star
37

lita-etcd

A Lita handler for interacting with etcd.
Ruby
3
star
38

pork

Text snippets on the command line.
Python
3
star
39

rust-fleet

A Rust client library for fleet.
Rust
3
star
40

import-count

A command line interface for analyzing import statements in JavaScript code.
TypeScript
3
star
41

railsapp

An application template for Rails.
Ruby
2
star
42

serde_test

Demonstration of using Serde on stable Rust without two files in Git per module.
Rust
2
star
43

docker-rust-armv7hf

A Docker image for Rust on the armv7hf architecture.
2
star
44

gitplugged.com

Community-driven promotion of GitHub repositories.
Ruby
2
star
45

pretty_chat

Pretty print blocks of text from chats
Ruby
2
star
46

lita-how-many

Teaches Lita how to count.
Ruby
2
star
47

lita-pls

k
Ruby
2
star
48

penguin

Penguin is a tool for creating presentations.
Ruby
2
star
49

lita-literally-lita

A Lita handler to make Lita act like Lita.
Ruby
1
star
50

thewarheads.com

TypeScript
1
star
51

scam

Text snippets on the command line.
Scala
1
star
52

connect_four

Connect Four written in Rust.
Rust
1
star
53

lita-high-five

A Lita handler that won't leave you hanging.
Ruby
1
star
54

xmpp

An XMPP library for Ruby.
Ruby
1
star
55

docker-rpi-lita

A Docker image for Lita on ARMv7.
1
star
56

vim-railscasts

A railscasts color theme that works in 256color terminals as well as gui
Vim Script
1
star