Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.9.3 Update Breaks Chained Backends #405

Closed
esb opened this issue Feb 2, 2018 · 2 comments
Closed

0.9.3 Update Breaks Chained Backends #405

esb opened this issue Feb 2, 2018 · 2 comments

Comments

@esb
Copy link

@esb esb commented Feb 2, 2018

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.

@fatkodima
Copy link
Contributor

@fatkodima fatkodima commented Feb 4, 2018

Can confirm. I'm working on a fix.

@radar
Copy link
Collaborator

@radar radar commented Feb 7, 2018

How’s that fix coming along @fatkodima?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants