Description
Upgrading from 0.9.1 to 0.9.3 when using a chained backend causes an exception in translations
I have a Rails application with the following configuration for I18n.
I18N_REDIS = Redis::Namespace.new(:i18n, :redis => Redis.new(:path => "/tmp/redis.sock"))
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::KeyValue.new(I18N_REDIS, false), I18n.backend)
The following expression fails under 0.9.3
I18n.t(:x_minutes, count: 2, scope: 'datetime.distance_in_words')
It should return "2 minutes" but instead returns "I18n::InvalidPluralizationData: translation data nil can not be used with :count => 2. key 'other' is missing."
The 0.9.3 update included the commit "Fix issue with disabled subtrees and pluralization for KeyValue backend" which updates the file lib/i18n/backend/base.rb
The error is caused by the move of the lines 43-47 to after line 36. The lines are
entry = entry.dup if entry.is_a?(String)
count = options[:count]
entry = pluralize(locale, entry, count) if count
These are moved in front of
if entry.nil?
if (options.key?(:default) && !options[:default].nil?) || !options.key?(:default)
throw(:exception, I18n::MissingTranslation.new(locale, key, options))
end
end
The problem is that with chained backends, the initial backend may not have the translation, in which case an exception is thrown and the chaining skips to the next backend to find the translation. Now the order of the statements is changed, and if the translation is not found, then entry == nil and the call to pluralize will fail in a fatal way. In the 0.9.1 code, this couldn't happen as the test for nil happened before the call to pluralize.