Skip to content

Commit

Permalink
Load locale files for available locales only
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda committed Oct 20, 2020
1 parent af1b437 commit 74d3aa9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/faker.rb
Expand Up @@ -14,7 +14,15 @@ module Faker
class << self
def load_i18n
unless @i18n_loaded
I18n.load_path += ::Dir[::File.join(__dir__, 'locales', '**/*.yml')]
if I18n.available_locales && I18n.available_locales.any?
# We expect all locale .yml files to have the locale name in its filename
I18n.load_path += ::Dir[::File.join(__dir__, 'locales', "{#{I18n.available_locales.join(',')}}.yml")]
# Or to be located in a directory with the locale name
I18n.load_path += ::Dir[::File.join(__dir__, 'locales', "{#{I18n.available_locales.join(',')}}/*.yml")]
else
I18n.load_path += ::Dir[::File.join(__dir__, 'locales', '**/*.yml')]
end

I18n.reload! if I18n.backend.initialized?
@i18n_loaded = true
end
Expand Down
15 changes: 15 additions & 0 deletions test/test_locale.rb
Expand Up @@ -78,4 +78,19 @@ def test_regex
def test_available_locales
assert I18n.locale_available?('en-GB')
end

def test_load_i18n
Faker.instance_variable_set :@i18n_loaded, nil
available_locales_was = I18n.available_locales

I18n.available_locales = %i(en ja id)
Faker.load_i18n
I18n.eager_load!

assert Faker.instance_variable_get(:@i18n_loaded)
assert_equal %i(en id ja), I18n.backend.translations.keys.sort
ensure
I18n.available_locales = available_locales_was
Faker.instance_variable_set :@i18n_loaded, nil
end
end

0 comments on commit 74d3aa9

Please sign in to comment.