From af1b437893883dc6e31eacd72bedd760d299cc59 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Tue, 20 Oct 2020 02:04:55 +0900 Subject: [PATCH 1/2] Defer loading I18n YAML files until actually translating --- lib/faker.rb | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/faker.rb b/lib/faker.rb index 17a58695ed..981f9f4dc3 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -8,10 +8,19 @@ Dir.glob(File.join(mydir, 'helpers', '*.rb')).sort.each { |file| require file } -I18n.load_path += Dir[File.join(mydir, 'locales', '**/*.yml')] -I18n.reload! if I18n.backend.initialized? - module Faker + @i18n_loaded = nil + + class << self + def load_i18n + unless @i18n_loaded + I18n.load_path += ::Dir[::File.join(__dir__, 'locales', '**/*.yml')] + I18n.reload! if I18n.backend.initialized? + @i18n_loaded = true + end + end + end + class Config @locale = nil @random = nil @@ -21,6 +30,8 @@ class << self attr_writer :random def locale + Faker.load_i18n + # Because I18n.locale defaults to :en, if we don't have :en in our available_locales, errors will happen @locale || (I18n.available_locales.include?(I18n.locale) ? I18n.locale : I18n.available_locales.first) end @@ -150,6 +161,8 @@ def parse(key) # Call I18n.translate with our configured locale if no # locale is specified def translate(*args, **opts) + Faker.load_i18n + opts[:locale] ||= Faker::Config.locale opts[:raise] = true I18n.translate(*args, **opts) @@ -166,6 +179,8 @@ def translate(*args, **opts) # Executes block with given locale set. def with_locale(tmp_locale = nil) + Faker.load_i18n + current_locale = Faker::Config.own_locale Faker::Config.locale = tmp_locale From 1a303c03edd53d0c848fc72861cb522a0edc9e51 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Tue, 20 Oct 2020 16:35:59 +0900 Subject: [PATCH 2/2] Load locale files for available locales only --- lib/faker.rb | 14 +++++++++++--- test/test_locale.rb | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/faker.rb b/lib/faker.rb index 981f9f4dc3..9e81318ae0 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -13,11 +13,19 @@ module Faker class << self def load_i18n - unless @i18n_loaded + return if @i18n_loaded + + if 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')] - I18n.reload! if I18n.backend.initialized? - @i18n_loaded = true end + + I18n.reload! if I18n.backend.initialized? + @i18n_loaded = true end end diff --git a/test/test_locale.rb b/test/test_locale.rb index 3c7b362ed7..a55de7649b 100644 --- a/test/test_locale.rb +++ b/test/test_locale.rb @@ -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