No commit activity in last 3 years
No release in over 3 years
Rails I18n Backend for Http APIs with etag-aware background polling and memory+[memcache] caching
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
>= 0
>= 0
>= 0
~> 2.5
>= 0

Runtime

>= 0
 Project Readme

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
michael@grosser.it
License: MIT
Build Status