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

TypeError (no implicit conversion of String into Integer) -- While using I18n.l #534

Closed
radar opened this issue Jul 14, 2020 · 2 comments · Fixed by #535
Closed

TypeError (no implicit conversion of String into Integer) -- While using I18n.l #534

radar opened this issue Jul 14, 2020 · 2 comments · Fixed by #535

Comments

@radar
Copy link
Collaborator

radar commented Jul 14, 2020

I had:

I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
I18n.enforce_available_locales = false

And things like:

I18n.l product.updated_at, :format => :long, :locale => "un-supported"

Worked great! Used default locale's :long.

Then I upgraded to said version, and I cannot get this behavior back.

I18n::Backend::Simple.send :include, I18n::Backend::Fallbacks
I18n.enforce_available_locales = false
I18n.fallbacks = [I18n.default_locale]

Gives:

Traceback (most recent call last):
       14: from ./bin/rails:4:in `<main>'
       13: from ./bin/rails:4:in `require'
       12: from /Users/sshaw/.rvm/gems/ruby-2.5.2/gems/railties-5.0.7.2/lib/rails/commands.rb:18:in `<top (required)>'
       11: from /Users/sshaw/.rvm/gems/ruby-2.5.2/gems/railties-5.0.7.2/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
       10: from /Users/sshaw/.rvm/gems/ruby-2.5.2/gems/railties-5.0.7.2/lib/rails/commands/commands_tasks.rb:78:in `console'
        9: from /Users/sshaw/.rvm/gems/ruby-2.5.2/gems/railties-5.0.7.2/lib/rails/commands/console_helper.rb:9:in `start'
        8: from /Users/sshaw/.rvm/gems/ruby-2.5.2/gems/railties-5.0.7.2/lib/rails/commands/console.rb:65:in `start'
        7: from (irb):1
        6: from /Users/sshaw/.rvm/gems/ruby-2.5.2/gems/i18n-1.1.0/lib/i18n.rb:274:in `localize'
        5: from /Users/sshaw/.rvm/gems/ruby-2.5.2/gems/i18n-1.1.0/lib/i18n/backend/base.rb:83:in `localize'
        4: from /Users/sshaw/.rvm/gems/ruby-2.5.2/gems/i18n-1.1.0/lib/i18n.rb:181:in `translate'
        3: from /Users/sshaw/.rvm/gems/ruby-2.5.2/gems/i18n-1.1.0/lib/i18n.rb:181:in `catch'
        2: from /Users/sshaw/.rvm/gems/ruby-2.5.2/gems/i18n-1.1.0/lib/i18n.rb:185:in `block in translate'
        1: from /Users/sshaw/.rvm/gems/ruby-2.5.2/gems/i18n-1.1.0/lib/i18n/backend/fallbacks.rb:45:in `translate'
TypeError (no implicit conversion of String into Integer)
I18n.enforce_available_locales = false
I18n.fallbacks = [I18n.default_locale]

Along with other seemingly correct config permutations give:

I18n::MissingTranslationData (translation missing: XX.time.formats.long)

Back to 1.0.1! 🔒

Originally posted by @sshaw in #415 (comment)

@radar
Copy link
Collaborator Author

radar commented Jul 14, 2020

I'm able to reproduce this issue using this Gemfile:

source 'https://rubygems.org'

gem 'i18n', '~> 1.8.3'

And this test script:

require 'bundler'
Bundler.setup

require 'i18n'

I18n::Backend::Simple.send :include, I18n::Backend::Fallbacks
I18n.enforce_available_locales = false
I18n.fallbacks = [I18n.default_locale]

class Product
  def updated_at
    Time.now
  end
end

product = Product.new

I18n.l product.updated_at, format: :long, locale: "un-supported"

Thank you @sshaw for the great steps to reproduce. I will start investigating this issue this morning.

@radar
Copy link
Collaborator Author

radar commented Jul 15, 2020

I believe I have a fix for this in b751041. I will let CI be the final judge of that.

What was happening:

  1. You set I18n.fallbacks = [I18n.default_locale] -- assuming this is because this is the advice we give in the post-install message. This is the right thing to do here.
  2. When localize attempts to fallback, it does so via this each block.
  3. Unfortunately by this point I18n.fallbacks is an array, not a Hash like that method is expecting. So it fails.

A workaround for this would be:

I18n.fallbacks = I18n::Locale::Fallbacks.new(I18n.default_locale)

This correctly sets fallbacks to be a Hash-ish object, not an Array.

But this is Ruby! We can be lazy and make the code do this for us. This is what I hope b751041 will fix for us.

I'm sorry for the trouble and for the delay in attending to this issue. COVID and friends have messed with my schedule more than I would have liked.

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

Successfully merging a pull request may close this issue.

1 participant