public
Created

Random values on passenger.

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#production.rb
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
# Only works with DalliStore
Rails.cache.reset if forked
end
end
 
#config/initializers/i18n.rb
 
I18n.backend = I18n::Backend::ActiveRecord.new
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Flatten)
 
#this implements cache store inside active record store - we cache only the values that AR retrives.
#v important - DO NOT USE i18n cache implementation! its broken by design
 
module I18n
module Backend
class ActiveRecord
protected
 
def lookup_with_cache(locale, key, scope = [], options = {})
 
return unless key
separator = options[:separator] || I18n.default_separator
somekey = resolve_link(locale, key)
somekey = (Array(scope) + Array(somekey)).join(separator)
ret = Rails.cache.fetch("i18n-#{[locale, somekey].join(separator)}".gsub(/\s/, "_")) do
[lookup_without_cache(locale, key, scope, options)].to_json
end
JSON.parse(ret).first unless ret.nil?
end
 
alias_method_chain :lookup, :cache
 
#since we are using JSON to parse cached data hashes will come back like so:
#>> {"a" => "a"}
#but i18n needs:
#>> {:a => 'a'}
 
def pluralize_with_symbolized_keys(locale, entry, count)
if entry.is_a?(Hash)
pluralize_without_symbolized_keys(locale, entry.symbolize_keys, count)
else
pluralize_without_symbolized_keys(locale, entry, count)
end
end
 
alias_method_chain :pluralize, :symbolized_keys
end
end
end
 
I18n::Backend::Simple.send(:include, I18n::Backend::Memoize)
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
I18n.backend = I18n::Backend::Chain.new(I18n.backend, I18n::Backend::Simple.new)

This is simple I18n.cache implementation specific for I18n AR backend. Rails.cache is used to cache only values stored in the database. This applies only if you are using i18n backend.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.