faker-ruby / faker

A library for generating fake data such as names, addresses, and phone numbers.
MIT License
11.25k stars 3.18k forks source link

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

Closed huyderman closed 2 years ago

huyderman commented 6 years ago

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.

stympy commented 6 years ago

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

neontapir commented 6 years ago

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.

stympy commented 6 years ago

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

neontapir commented 6 years ago

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.

stefannibrasil commented 2 years ago

Hey, folks. In an effort to lighten our load as maintainers and be able to serve you better in the future, the faker-ruby team is working on cleaning out the cobwebs in this repo by pruning the backlog. As there are few of us, there are a lot of items that will simply never earn our attention in a reasonable time frame, and rather than giving you an empty promise, we think it makes more sense to focus on more recent issues. That means, unfortunately, that we must close this issue.

Don't take this the wrong way: our aim is not to diminish the effort people have made or dismiss problems that have been raised. If you feel that we should reopen this issue, then please let us know so that we can reprioritize it. Thanks!