Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ def translate(*args, **opts)
# in en either, then it will raise again.
disable_enforce_available_locales do
I18n.translate(*args, **opts)
rescue I18n::MissingTranslationData
# I18n discards translations for locales that are not in
# I18n.available_locales when it initializes. If the :en
# translations were discarded, load them and try once more.
raise if I18n.exists?('faker', :en)

en_files = Dir[::File.join(__dir__, 'locales', 'en.yml'), ::File.join(__dir__, 'locales', 'en', '**/*.yml')]
I18n.backend.load_translations(en_files)
I18n.translate(*args, **opts)
end
end

Expand Down
13 changes: 13 additions & 0 deletions test/test_locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ def test_translation_fallback_without_en_in_available_locales
I18n.available_locales += [:en]
end

# https://github.com/faker-ruby/faker/issues/2987
def test_translation_fallback_when_i18n_initialized_without_en_in_available_locales
old_available_locales = I18n.available_locales
I18n.available_locales = [:de]
I18n.reload! # force I18n to discard the :en translations
Faker::Config.locale = :de

assert_kind_of String, Faker::Company.catch_phrase
ensure
I18n.available_locales = old_available_locales
I18n.reload!
end

def test_with_locale_does_not_fail_without_the_locale_in_available_locales
I18n.available_locales -= [:en]

Expand Down