Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make sure the cache backend does not try to store proc objects (resul…

…ting in marshalling errors since rails 3.x)
  • Loading branch information...
commit d9d2c767474226dc7f06cd110872307d3e916c0f 1 parent b832037
Sven Fuchs authored
23 lib/i18n/backend/cache.rb
View
@@ -71,17 +71,25 @@ def translate(locale, key, options = {})
protected
def fetch(cache_key, &block)
- result = begin
- I18n.cache_store.fetch(cache_key, &block)
- rescue MissingTranslationData => exception
- I18n.cache_store.write(cache_key, exception)
- exception
- end
+ result = fetch_storing_missing_translation_exception(cache_key, &block)
raise result if result.is_a?(Exception)
result = result.dup if result.frozen? rescue result
result
end
+ def fetch_storing_missing_translation_exception(cache_key, &block)
+ fetch_ignoring_procs(cache_key, &block)
+ rescue MissingTranslationData => exception
+ I18n.cache_store.write(cache_key, exception)
+ exception
+ end
+
+ def fetch_ignoring_procs(cache_key, &block)
+ I18n.cache_store.read(cache_key) || yield.tap do |result|
+ I18n.cache_store.write(cache_key, result) unless result.is_a?(Proc)
+ end
+ end
+
def cache_key(locale, key, options)
# This assumes that only simple, native Ruby values are passed to I18n.translate.
"i18n/#{I18n.cache_namespace}/#{locale}/#{key.hash}/#{USE_INSPECT_HASH ? options.inspect.hash : options.hash}"
@@ -91,7 +99,6 @@ def cache_key(locale, key, options)
# In Ruby < 1.9 the following is true: { :foo => 1, :bar => 2 }.hash == { :foo => 2, :bar => 1 }.hash
# Therefore we must use the hash of the inspect string instead to avoid cache key colisions.
USE_INSPECT_HASH = RUBY_VERSION <= "1.9"
-
end
end
-end
+end
4 lib/i18n/exceptions.rb
View
@@ -28,7 +28,9 @@ def initialize(filename)
class MissingTranslationData < ArgumentError
attr_reader :locale, :key, :options
def initialize(locale, key, opts = nil)
- @key, @locale, @options = key, locale, opts || {}
+ @key, @locale, @options = key, locale, opts.dup || {}
+ options.each { |k, v| options[k] = v.inspect if v.is_a?(Proc) }
+
keys = I18n.normalize_keys(locale, key, options[:scope])
keys << 'no key' if keys.size < 2
super "translation missing: #{keys.join(', ')}"
2  test/api/all_features_test.rb
View
@@ -12,8 +12,8 @@
class I18nAllFeaturesApiTest < Test::Unit::TestCase
class Backend < I18n::Backend::Simple
- include I18n::Backend::Cache
include I18n::Backend::Metadata
+ include I18n::Backend::Cache
include I18n::Backend::Cascade
include I18n::Backend::Fallbacks
include I18n::Backend::Pluralization
Please sign in to comment.
Something went wrong with that request. Please try again.