RubyFlow The Ruby and Rails community linklog

×

The Ruby and Rails community linklog

Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!

translation_diff: re-translate only the sentences that changed

Edit one word in a 3,000-character product description and most translation APIs bill you for all 3,000 characters again. translation_diff splits the text into sentences, caches each one by content hash, and sends the provider only the sentences it hasn’t seen. Repeated blocks like a return policy get translated once across your whole catalog.

It works with DeepL, Google, Azure, ModernMT, LibreTranslate and Amazon Translate, and you can switch provider per call: TranslationDiff.translate(text, from: "en", to: "de", provider: :google). Strings, arrays and nested hashes come back in the same shape, HTML markup is kept, and class="notranslate" protects a span where the provider supports it.

The cache lives in memory, in Redis, or in your own database through ActiveRecord. TranslationDiff.preview shows what a call would send and what is already cached, without calling the API. Version 1.0.0 is on RubyGems, Ruby 3.4+.

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in