Faker: Faker incorrectly assumes all I18n backends respond to `#initialized?`

Created on 29 Nov 2017  路  4Comments  路  Source: faker-ruby/faker

Faker calls I18n.reload! if I18n.backend.initialized? to reload Faker translations. However, only I18n::Backend::Simple implements #initialized?, causing an error if I18n has been configured to use another backend (or backends through I18n::Backend::Chain).

Example stack trace:

NoMethodError: undefined method `initialized?' for #<I18n::Backend::Chain:0x00005624e0e978d8>
  /home/huyderman/.gem/ruby/2.4.0/gems/faker-1.8.4/lib/faker.rb:16:in `<top (required)>'
  (...)

In my app, I use I18n::Backend::Chain to chain multiple backends, so to get Faker to work I monkey patched it like this:

class I18n::Backend::Chain
  def initialized?
    backends.any? do |backend|
      backend.respond_to?(:initialized?) ? backend.initialized? : false
    end
  end
end

Needless to say, it would be nice to not have to monkey patch to use Faker.

BuFixes

All 4 comments

Thanks for raising the issue! Feel free to submit a PR. :)

Maybe I'm looking at this too simply. It seems that changing faker.rb:16 to

I18n.reload! if (I18n.backend.respond_to?(:initialized?) && I18n.backend.initialized?)

would have the same effect as the monkey patch. If so, I'm happy to submit a PR to that effect. The tests pass locally with that change in place. I ask because I'm not familiar with I18n, and don't want to introduce side effects.

That seems reasonable to me. It would be awesome if there was a test case that could reproduce the problem.

I spent some time trying to write a unit test for this, but was unable to find a way to isolate the backend change to a single test.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ggrillone picture ggrillone  路  6Comments

crispinheneise picture crispinheneise  路  6Comments

supiash1 picture supiash1  路  5Comments

SylarRuby picture SylarRuby  路  3Comments

tejasbubane picture tejasbubane  路  5Comments