Open rocket-turtle opened 3 months ago
Thanks for reporting the issue, @rocket-turtle !
That's interesting. In your setup, I18n has available_locales
limited to [:de]
, but when faker catches the error and tries again passing opts[:locale] = :en
, then I18n won't try to load the en
locale, as it's not available.
For me, I18n's behavior makes sense, as faker is trying to force a locale that is not even available.
So maybe this line here is not working as expected: https://github.com/faker-ruby/faker/blob/97cfd1a1b0522a16b402e9d14c3248076b24c942/lib/faker.rb#L262
Changing I18n.enforce_available_locales
might not really work, or maybe it requires I18n.reload!
. I would have to double check what this is supposed to be doing.
--
For now, if the :de
locale doesn't have catch phrases available, maybe you could use a different generator instead?
We would also be open to accepting a PR that expands the de.company
locale to add catch phrases.
I just added :en
in my populate.rake
and reload all the translations.
That "fixes" the problem for my setup.
I18n.available_locales << :en
I18n.reload!
If others have the same problem and https://github.com/faker-ruby/faker/issues/2719 is not moving to a different solution for loading the translations I can work on an PR but i think it would not be nice because disable_enforce_available_locales
would have to reload the translations twice for every call if :en
is not available.
I came to the same conclusion than @rocket-turtle
I use Faker in a Rails app with FactoryBot in my RSpec code. So one way to circumvent this is to (in config/environments/test.rb
):
Be sure to enable raise_on_missing_translations, to catch any missing key elsewhere in the application.
But I'm not satisfied as the translation cache is no VERY polluted with every Faker translations from every languages...
I currently use a fork of Faker that reloads I18n on missing translation (doing it once at the first missing translation, not every time). This way only :en translations are add and are polluting I18n. But still very basic and work is needed to reduce useless :en keys storage (when those keys are present in desired locale).
Describe the bug
With my Setup I get this error when I call:
Faker::Company.catch_phrase
To Reproduce
Describe a way to reproduce your bug. To get the Faker version, run
Faker::VERSION
.Use the reproduction script below to reproduce the issue:
Additional context
Faker has a real simple translation missing fallback:
faker-3.4.2/lib/faker.rb
The
disable_enforce_available_locales
try to fix, ifenforce_available_locales
is set but:en
is not in thei18n.available_locales
.faker-3.4.2/lib/faker.rb:262
Problem
With this setup
I18n
does not store the:en
translation.i18n-1.14.5/lib/i18n/backend/simple.rb
Current workaround
Somewhere in my code I make:
I18n.reload! if I18n.backend.send(:translations).keys.exclude?(:en)
Solution?
I can not think about a good / easy solution without "polluting" the translations:
Before the reload!
I18n.backend.send(:translations).keys.count => 1
After the reload!I18n.backend.send(:translations).keys.count => 66
Maybe the solution is in https://github.com/faker-ruby/faker/issues/2719