1 # encoding: utf-8
2
3 # This module allows you to easily cache all responses from the backend - thus
4 # speeding up the I18n aspects of your application quite a bit.
5 #
6 # To enable caching you can simply include the Cache module to the Simple
7 # backend - or whatever other backend you are using:
8 #
9 # I18n::Backend::Simple.send(:include, I18n::Backend::Cache)
10 #
11 # You will also need to set a cache store implementation that you want to use:
12 #
13 # I18n.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
14 #
15 # You can use any cache implementation you want that provides the same API as
16 # ActiveSupport::Cache (only the methods #fetch and #write are being used).
17 #
18 # The cache_key implementation assumes that you only pass values to
19 # I18n.translate that return a valid key from #hash (see
20 # http://www.ruby-doc.org/core/classes/Object.html#M000337).
21 module I18n
22 class << self
23 @@cache_store = nil
24 @@cache_namespace = nil
25
26 def cache_store
27 @@cache_store
28 end
29
30 def cache_store=(store)
31 @@cache_store = store
32 end
33
34 def cache_namespace
35 @@cache_namespace
36 end
37
38 def cache_namespace=(namespace)
39 @@cache_namespace = namespace
40 end
41
42 def perform_caching?
43 !cache_store.nil?
44 end
45 end
46
47 module Backend
48 # TODO Should the cache be cleared if new translations are stored?
49 module Cache
50 def translate(*args)
51 I18n.perform_caching? ? fetch(*args) { super } : super
52 end
53
54 protected
55
56 def fetch(*args, &block)
57 result = I18n.cache_store.fetch(cache_key(*args), &block)
58 raise result if result.is_a?(Exception)
59 result = result.dup if result.frozen? rescue result
60 result
61 rescue MissingTranslationData => exception
62 I18n.cache_store.write(cache_key(*args), exception)
63 raise exception
64 end
65
66 def cache_key(*args)
67 # This assumes that only simple, native Ruby values are passed to I18n.translate.
68 # Also, in Ruby < 1.8.7 {}.hash != {}.hash
69 # (see http://paulbarry.com/articles/2009/09/14/why-rails-3-will-require-ruby-1-8-7)
70 # If args.inspect does not work for you for some reason, patches are very welcome :)
71 hash = RUBY_VERSION >= "1.8.7" ? args.hash : args.inspect
72 keys = ['i18n', I18n.cache_namespace, hash]
73 keys.compact.join('-')
74 end
75 end
76 end
77 en