Skip to content

Commit

Permalink
Don't double-load locale files
Browse files Browse the repository at this point in the history
When we use `require`, Ruby makes sure that we only load the file once,
but since here we're explictly pushing things onto the load path we need
to do that check ourself.

Found using bin/rspec --warnings, which showed the following:

    /Users/dgmstuart/dev/swing-out-london/config/locales/en.rb:3: warning: method redefined; discarding old ordinal_day
    /Users/dgmstuart/dev/swing-out-london/config/locales/en.rb:3: warning: previous definition of ordinal_day was here
    /Users/dgmstuart/dev/swing-out-london/config/locales/en.rb:7: warning: method redefined; discarding old human_date
    /Users/dgmstuart/dev/swing-out-london/config/locales/en.rb:7: warning: previous definition of human_date was here

...since these methods are defined in the locale file which is being
loaded here.
  • Loading branch information
dgmstuart committed Sep 1, 2024
1 parent 570af78 commit 4cf6f0a
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions spec/support/time_formats_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
require "active_support/core_ext/time/conversions"
require "active_support/core_ext/integer/inflections"

# Allow reference to translations in isolated specs:
%w[en.yml en.rb].each do |locale_file|
I18n.load_path.push(
File.expand_path(locale_file, File.expand_path("../../config/locales", __dir__))
)
locale_files = %w[en.yml en.rb].map do |locale_file|
File.expand_path(locale_file, File.expand_path("../../config/locales", __dir__))
end

# Allow reference to translations in isolated specs,
# but only load the files if they're not already in the load path
locale_files.each do |file|
I18n.load_path.push(file) unless I18n.load_path.include?(file)
end

0 comments on commit 4cf6f0a

Please sign in to comment.