Giter Site home page Giter Site logo

emartech / i18n-backend-http Goto Github PK

View Code? Open in Web Editor NEW

This project forked from grosser/i18n-backend-http

0.0 10.0 1.0 1.59 MB

Rails I18n Backend for Http APIs with etag-aware distributed background polling and lru-memory+[memcache] caching.

Ruby 100.00%

i18n-backend-http's Introduction

Rails I18n Backend for Http APIs with etag-aware distributed background polling and lru-memory+[memcache] caching.
Very few request, always up to date + fast.

Install

gem install i18n-backend-http

Usage

class MyBackend < I18n::Backend::Http
  def initialize(options={})
    super({
      host:  "https://api.host.com",
      cache: Rails.cache,
      http_open_timeout: 5, # default: 1
      http_read_timeout: 5, # default: 1
      http_open_retries: 3, # default: 0
      http_read_retries: 3, # default: 0
      # exception_handler:  lambda{|e| Rails.logger.error e },
      # memory_cache_size: ??, # default: 10 locales
    }.merge(options))
  end

  def parse_response(body)
    JSON.load(body).fetch("translations")
  end

  def path(locale)
    "/path/to/api/#{locale}.json"
  end
end

I18n.backend = MyBackend.new

Polling

Tries to update all used translations every 10 minutes (using ETag and :cache), can be stopped via I18n.backend.stop_polling.
If a :cache is given, all backends pick one master to do the polling, all others refresh from :cache

I18n.backend = MyBackend.new(polling_interval: 30.minutes, cache: Rails.cache)

I18n.t('some.key') == "Old value"
# change in backend + wait 30 minutes
I18n.t('some.key') == "New value"

:cache

If you pass cache: Rails.cache, translations will be loaded from cache and updated in the cache.
The cache MUST support :unless_exist MemCacheStore + LibmemcachedStore + Dalli + ActiveSupport::Cache::MemoryStore (4+) work.

Exceptions

To handle http exceptions provide e.g. exception_handler: -> (e) { puts e } (prints to stderr by default).

Limited memory cache

The backend stores the 10 least recently used locales in memory, change via memory_cache_size: 100

Fallback

If the http backend is down, it does not translate, but also does not constantly try to query -> your app is untranslated but not down.
You should either use :default for all I18n.t or use a Chain, so when http is down e.g. english is used.

I18n.backend = I18n::Backend::Chain.new(
  MyBackend.new(options),
  I18n::Backend::Simple.new
)

TODO

  • available_locales is not implemented, since we did not need it
  • reload -> all caches should be cleared

Author

Michael Grosser
[email protected]
License: MIT
Build Status

i18n-backend-http's People

Contributors

grosser avatar craig-day avatar ajinkyapatil8190 avatar zosiu avatar

Watchers

Viktor Somodi avatar  avatar András Fincza avatar James Cloos avatar  avatar Gabor Szabo avatar Ferenc Kalcso avatar  avatar  avatar  avatar

Forkers

isabella232

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.