Giter Site home page Giter Site logo

google_translate_diff's Introduction

GoogleTranslateDiff

Google Translate API wrapper helps to translate only changes between revisions of long texts.

Sponsored by Evil Martians

Build Status Code Climate Test Coverage

Use case

Assume your project contains a significant amount of products descriptions which:

  • Require retranslation each time user edits them.
  • Have a lot of equal parts (like return policy).
  • Change frequently.

If your user changes a single word within the long description, you will be charged for the retranslation of the whole text.

Much better approach is to try to translate every repeated structural element (sentence) in your texts array just once to save money. This gem helps to make it done.

Installation

Add this line to your application's Gemfile:

gem 'google_translate_diff'

And then execute:

$ bundle

Or install it yourself as:

$ gem install google_translate_diff

Usage

require "google_translate_diff"

# This dependencies are not included, as you might need to roll your own cache based on different store
require "redis"
require "connection_pool"
require "redis-namespace"
require "ratelimit" # Optional, if you will use

# Setup https://github.com/GoogleCloudPlatform/google-cloud-ruby/tree/master/google-cloud-translate
ENV["TRANSLATE_KEY"] = "foobarkey"

# I always use pool for redis
pool = ConnectionPool.new(size: 10, timeout: 5) { Redis.new }

# Pass any options (like app id)
GoogleTranslateDiff.api = Google::Cloud::Translate.new

GoogleTranslateDiff.cache_store =
  GoogleTranslateDiff::RedisCacheStore.new(pool, timeout: 7.days, namespace: "t")

# Optional
GoogleTranslateDiff.rate_limiter =
  GoogleTranslateDiff::RedisRateLimiter.new(
    pool, threshold: 8000, interval: 60, namespace: "t"
  )

GoogleTranslateDiff.translate("test translations", from: "en", to: "es")

How it works

  • Text nodes are extracted from HTML.
  • Every text node is split into sentences (using punkt-segmenter gem).
  • Cache is checked for the presence of each sentence (using language couple and a hash of string).
  • Missing sentences are translated via API and cached.
  • Original HTML is recombined from translations and cache data.

NOTE: :from is required param. Cache can not be checked without specifying exact language couple, that's the limitation.

Input

::translate can receive string, array or deep hash and will return the same, but translated.

GoogleTranslateDiff.translate("test", from: "en", to: "es")
GoogleTranslateDiff.translate("test", "language", from: "en", to: "es")
GoogleTranslateDiff.translate(
  { title: "test", values: { type: "frequent" } }, from: "en", to: "es"
)

See GoogleTranslateDiff::Linearizer for details.

HTML

You can pass HTML as like as plain text:

GoogleTranslateDiff.translate("<b>Black</b>", from: "en", to: "es")

Very long texts

Google API has a limitation: query can not be longer than approximately 4 KB. If your text is really that long, multiple queries will be used to translate it automatically.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/gzigzigzeo/google_translate_diff.

google_translate_diff's People

Contributors

gzigzigzeo avatar nepalez avatar

Watchers

 avatar  avatar

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.