From 3e1aac5b9a007836de9a4dab21cfc4352acc5514 Mon Sep 17 00:00:00 2001 From: Pascal Zumkehr Date: Wed, 1 Mar 2023 14:27:21 +0100 Subject: [PATCH 1/2] Remove unnecessary i18n reload I18n is reloaded whenever load_path are set, so an additional reload is not needed and only increases load time. --- lib/faker.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/faker.rb b/lib/faker.rb index 89eb82ae77..072c2b0082 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -8,7 +8,6 @@ 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 module Config From bfb9ee4b0d30d4258d23800eae5b3412e745450f Mon Sep 17 00:00:00 2001 From: Pascal Zumkehr Date: Wed, 29 Mar 2023 23:06:12 +0200 Subject: [PATCH 2/2] Test that faker and i18n are loaded properly --- test/test_i18n_reload.rb | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/test_i18n_reload.rb diff --git a/test/test_i18n_reload.rb b/test/test_i18n_reload.rb new file mode 100644 index 0000000000..e20c145e52 --- /dev/null +++ b/test/test_i18n_reload.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'test_helper' +require 'open3' + +class TestI18nLoad < Test::Unit::TestCase + def test_faker_i18n + # run this code in a subshell to test require faker + # and proper initialization of i18n. + code = <<-RUBY + require 'bundler/inline' + + gemfile do + source 'https://rubygems.org' + gem 'minitest' + gem 'i18n' + end + + require 'minitest/autorun' + require 'i18n' + + class TestI18nLoad < Minitest::Test + def test_faker_i18n + I18n.available_locales = [:en] + refute_predicate I18n.backend, :initialized? + I18n.translate('doesnt matter just triggering a lookup') + + assert_predicate I18n.backend, :initialized? + + assert require File.expand_path('#{File.dirname(__FILE__)}/../lib/faker') + + assert Faker::Name.name + end + end + RUBY + + cmd = %( ruby -e "#{code}" ) + output, status = Open3.capture2e(cmd) + + assert_equal 0, status, output + end +end