Giter Club home page Giter Club logo

easy_translate's Introduction

EasyTranslate

CI Status

This is a Ruby library for Google Translate that makes working with bulk calls, user_ips and access via API Key easy.

Unlike the official google client, easy_translate does not have a dependency on the grpc gem. This can be an advantage because grpc may be difficult to install on some systems or configurations.


Installation

$ gem install easy_translate

Or in your Gemfile:

gem 'easy_translate'

Single translation

# auto-detect
EasyTranslate.translate('Hello, world', to: :spanish) # => "Hola, mundo"
EasyTranslate.translate('Hello, world', to: 'es') # => "Hola, mundo"

# feel free to specify explicitly
EasyTranslate.translate('Hola, mundo', from: :spanish, to: :en) # => "Hello, world"

Batch translation (Yay!)

# multiple strings
EasyTranslate.translate(['Hello', 'Goodbye'], to: :spanish) # => ["¡Hola", "Despedida"]

API Keys

# make google happy - (NOTE: use these anywhere)
EasyTranslate.translate('Hello, world', to: :es, key: 'xxx')

# don't want to set the key on every call? ** Me either! **
EasyTranslate.api_key = 'xxx'

You want language detection too?

# detect language
EasyTranslate.detect "This is definitely English!" # => 'en'

Batch detection (Woohoo!)

# detect language
EasyTranslate.detect ['Hello World', '我姓譚'] # => ['en', 'zh-CN']

Need confidence in your detections?

# detect language with confidence
EasyTranslate.detect "This is definitely English!", confidence: true # => { :language => 'en', :confidence => 0.77272725 }
# detect batch languages with confidence
EasyTranslate.detect ['This is definitely English!', '我姓譚'], confidence: true # => [{ :language => 'en', :confidence => 0.77272725 }, { :language => 'zh-CN', :confidence => 1.0 }]

Explicitly select translation model (NMT, PBMT)

Google Translate now replaces Phrase Based Machine Translation (PBMT) with Neural Machine Translation (NMT) automatically where possible. If you prefer PBMT or need to compare the results, you can use the model: parameter with either nmt or base to force the model selection:

EasyTranslate.translate("El cuervo americano es un ave con plumas negras iridiscentes sobre todo su cuerpo.", from: "es", to: "en", model: "nmt")
 # => "The American Raven is a bird with iridescent black feathers over its entire body."
EasyTranslate.translate("El cuervo americano es un ave con plumas negras iridiscentes sobre todo su cuerpo.", from: "es", to: "en", model: "base")
# => "The American crow is a bird with iridescent black feathers over her body."

See https://research.googleblog.com/2016/09/a-neural-network-for-machine.html for more background

Google Translate supports HTML (default) and plain text formats

EasyTranslate.translate "Las doce en punto", format: 'text', to: :en
# => "Twelve o'clock"
EasyTranslate.translate "Las doce <b>en punto</b>", format: 'html', to: :en
# => "Twelve <b>o&#39;clock</b>"

A note on concurrency as of v0.4.0

Due to limitations with the Google Translate batch API, above a certain number of translations - this library will begin making calls concurrently.

The default concurrency is 4, but if you'd prefer to run without threads, you can set :concurrency => 1 to run the translation calls serially.


List of languages

# list from <http://translate.google.com/>
EasyTranslate::LANGUAGES # => { 'en' => 'english', ... }

List of supported languages

# List all languages (from API)
EasyTranslate.translations_available

# List all languages supported by some language
EasyTranslate.translations_available('zh-CN')

EasyTranslate in PHP

Kofel ported this library to PHP. You can find the port on GitHub


Contributors

Full contributor data at: https://github.com/seejohnrun/easy_translate/contributors


License

(The MIT License)

Copyright © 2010-2018 John Crepezzi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

easy_translate's People

Contributors

codegoalie avatar dorianmariecom avatar elim avatar foxlance avatar glebm avatar gmaliar avatar hemancuso avatar henrik avatar jqr avatar mariusandra avatar nel avatar ogawaleo avatar petergoldstein avatar prurph avatar seejohnrun avatar tdonia avatar westonganger avatar zetaben avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

easy_translate's Issues

Invalid Value (EasyTranslate::EasyTranslateException)

easy_translate (0.5.1) and i18n-tasks (1.0.12) with Google Translate API (credit card added):

$ bundle exec i18n-tasks translate-missing --from=en is
#StandWithUkraine
/home/dev/.bundle/ruby/3.1/gems/easy_translate-0.5.1/lib/easy_translate/request.rb:47:in `perform_raw': Invalid Value (EasyTranslate::EasyTranslateException)

At https://github.com/seejohnrun/easy_translate/blob/master/lib/easy_translate/request.rb#L38-L50 I get:

[1] pry(#<EasyTranslate::Translation::TranslationRequest>)> response.code
=> "200"
[2] pry(#<EasyTranslate::Translation::TranslationRequest>)> response.body
=> "{\"data\":{\"translations\":[{\"translatedText\":\"Reikningnum \xC3\xBE\xC3\xADnum hefur veri\xC3\xB0 l\xC3\xA6st vegna \xC3\xB3h\xC3\xB3flegrar misheppna\xC3\xB0ar innskr\xC3\xA1ningartilrauna.\"},{\"translatedText\":\"B\xC3\xA6ta vi\xC3\xB0 athugasemd\"},{\"translatedText\":\"B\xC3\xA6ta vi\xC3\xB0 n\xC3\xBDrri f\xC3\xA6rslu\"},{\"translatedText\":\"B\xC3\xA6ttu vi\xC3\xB0 myndum\"},{\"translatedText\":\"Aldur\"},{\"translatedText\":\"Nafnlaus\"},{\"translatedText\":\"Ertu viss\"} [...]

(ref. glebm/i18n-tasks#480)

Wrong Daily Limit

I can not do any translation, the error message telling me that I reaches the daily limit what is wrong.

EasyTranslate.translate('Hello, world', :to => :es, :key => 'AzqLJiluZtdoMUmoKnxDrfP')
EasyTranslate::EasyTranslateException: Daily Limit Exceeded
from /Library/Ruby/Gems/2.0.0/gems/easy_translate-0.4.0/lib/easy_translate/request.rb:47:in `perform_raw' 

HTTP timeouts and SSL options should be configurable

  1. Getting warning: peer certificate won't be verified in this SSL session when using language detection; system certificates are not automatically used (on OSX) and there is no way to add certificate file.
  2. Can not manually configure http timeouts.

Getting error to use google translate V2

When I try a translation google is coming back with an error that I should use V2 of the api. I am confused because I thought this gem used V2 of the API. I am definitely using the must recent version of the gem and it does say version 2 in the in gem source. Is the gem suppose to use V2?

New Release

Hi John, I was looking at your recents commits and the new confidence features is pretty rad. Are you planning to make a release soon by any chance?

Cheers

Threadable.rb breaks with hard-to-debug error

Using easy_translate as part of i18n-tasks, I get the following error:

ezuk@ezuk-mm-vm:~/mimi$ bundle show i18n-tasks
/home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3
ezuk@ezuk-mm-vm:~/mimi$ bundle exec i18n-tasks translate-missing
/home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/easy_translate-0.5.0/lib/easy_translate/threadable.rb:22:in `+': no implicit conversion of nil into Array (TypeError)
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/easy_translate-0.5.0/lib/easy_translate/threadable.rb:22:in `each'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/easy_translate-0.5.0/lib/easy_translate/threadable.rb:22:in `reduce'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/easy_translate-0.5.0/lib/easy_translate/threadable.rb:22:in `threaded_process'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/easy_translate-0.5.0/lib/easy_translate/translation.rb:20:in `translate'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/google_translation.rb:30:in `fetch_google_translations'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/google_translation.rb:23:in `block in google_translate_list'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/google_translation.rb:22:in `each'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/google_translation.rb:22:in `map'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/google_translation.rb:22:in `google_translate_list'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/google_translation.rb:10:in `block in google_translate_forest'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/data/tree/nodes.rb:13:in `each'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/data/tree/nodes.rb:13:in `each'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/google_translation.rb:9:in `inject'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/google_translation.rb:9:in `google_translate_forest'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/command/commands/missing.rb:29:in `translate_missing'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/command/commander.rb:38:in `run'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/command/commander.rb:19:in `safe_run'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/bin/i18n-tasks:41:in `block (3 levels) in <top (required)>'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/slop_command.rb:18:in `call'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/lib/i18n/tasks/slop_command.rb:18:in `block (3 levels) in slop_command'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/slop-3.5.0/lib/slop.rb:260:in `call'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/slop-3.5.0/lib/slop.rb:260:in `parse!'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/slop-3.5.0/lib/slop.rb:235:in `parse!'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/slop-3.5.0/lib/slop.rb:65:in `parse!'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/slop-3.5.0/lib/slop.rb:54:in `parse'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/gems/i18n-tasks-0.7.3/bin/i18n-tasks:32:in `<top (required)>'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/bin/i18n-tasks:23:in `load'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/bin/i18n-tasks:23:in `<main>'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/bin/ruby_executable_hooks:15:in `eval'
        from /home/ezuk/.rvm/gems/ruby-2.1.1@madmimi/bin/ruby_executable_hooks:15:in `<main>'

I'm not even sure where to begin troubleshooting it. :) It may be due to an issue with my yaml, but I don't know what's wrong with my source data (and Rails seems to like it well enough). Any thoughts?

try easytranslate for free

Hello, I am getting interested to try easytranslate because it seems quite easy to use. But because I am just a student I don't feel very confident in creating a billing account. Do you know if there is a way to use it moderately for small personal projects without a billing account?
Thanks

Ambiguity issue of 'zh-CN' vs 'zh' for chinese chars.

Issue ->
EasyTranslate.translations_available.include?(EasyTranslate.detect('简体中文')) => false

Reason ->
EasyTranslate.translations_available returns array with language codes. For Chinese it returns 'zh'

EasyTranslate.detect('简体中文') => 'zh-CN'

But EasyTranslate::LANGUAGES.include?('zh-CN') => true

Why like this? and what is solution?

Upgrade Google Translate API to v3

Currently this library seems to use Google Translate API v2.

Google provides a free tier of up to 500,000 characters when using API v3, but not for v2.

It would be nice if someone updated the library to use v3.

NameError uninitialized constant EasyTranslate::Translation::TranslationRequest::LANGUAGES

For some reason I'm getting this error:

NameError uninitialized constant EasyTranslate::Translation::TranslationRequest::LANGUAGES

on production and I am not sure why this is happening.

I have my app dockerised and using version easy_translate (0.5.0). Gem was installed via bundler.

Maybe someone has some idea or had this issue before and managed to fix it?

api_key seems not worked

when assign values on api_key ,there raise an no method error.
here is the rails console.
ruby-1.9.2-p0 > EasyTranslate.api_key='keywords'
NoMethodError: undefined method `api_key=' for EasyTranslate:Module
ruby-1.9.2-p0 > EasyTranslate.methods.grep /(translate|api_key)/
=> [:translate]
seems there no method called api_key
am i doing sth wrong?

Korean support

I noticed that korean is not in your list of languages, could you add that in (it's supported in the translate api). Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.