Skip to content

Commit ab36aba

Browse files
authored
Fix I18n.load_path injection (#546)
* fix I18n.load_path injection ice_cube would inject its locale files at the end of `I18n.load_path` due to its `IceCube::I18n` module being `autoload`ed and thereby overwrite any customisation the user may have made in other locale files earlier in the load path. i fix this by injecting ice cube locales at gem load time so that the user has a chance to modify locale keys later. this also eliminates a bunch of delegation having a custom I18n module; `IceCube::I18n` is just the same as ::I18n if the i18n gem is available. resolves #489, #432, #431 * use __dir__ expansion instead of __FILE__ * disable linter for I18n gymnastics
1 parent 621cef4 commit ab36aba

File tree

4 files changed

+14
-23
lines changed

4 files changed

+14
-23
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
### Changed
1212
- Removed use of `delegate` method added in [66f1d797](https://github.com/ice-cube-ruby/ice_cube/commit/66f1d797092734563bfabd2132c024c7d087f683) , reverting to previous implementation. ([#522](https://github.com/ice-cube-ruby/ice_cube/pull/522)) by [@pacso](https://github.com/pacso)
1313
- Updated supported versions of Ruby and Rails and fixed standardrb lint issues. ([#552](https://github.com/ice-cube-ruby/ice_cube/pull/552)) by [@pacso](https://github.com/pacso)
14+
- Fixed `I18n.load_path` injection ([#546](https://github.com/ice-cube-ruby/ice_cube/pull/546)) by [@glaszig](https://github.com/glaszig)
1415

1516
### Fixed
1617
- Fix for weekly interval results when requesting `occurrences_between` on a narrow range ([#487](https://github.com/seejohnrun/ice_cube/pull/487)) by [@jakebrady5](https://github.com/jakebrady5)

lib/ice_cube.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module IceCube
66

77
autoload :TimeUtil, "ice_cube/time_util"
88
autoload :FlexibleHash, "ice_cube/flexible_hash"
9-
autoload :I18n, "ice_cube/i18n"
9+
10+
require "ice_cube/i18n"
1011

1112
autoload :Rule, "ice_cube/rule"
1213
autoload :Schedule, "ice_cube/schedule"

lib/ice_cube/i18n.rb

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
require "ice_cube/null_i18n"
22

33
module IceCube
4-
module I18n
5-
LOCALES_PATH = File.expand_path(File.join("..", "..", "..", "config", "locales"), __FILE__)
6-
7-
def self.t(*args, **kwargs)
8-
backend.t(*args, **kwargs)
9-
end
10-
11-
def self.l(*args, **kwargs)
12-
backend.l(*args, **kwargs)
13-
end
14-
15-
def self.backend
16-
@backend ||= detect_backend!
17-
end
18-
19-
def self.detect_backend!
20-
::I18n.load_path += Dir[File.join(LOCALES_PATH, "*.yml")]
21-
::I18n
22-
rescue NameError
23-
NullI18n
24-
end
4+
LOCALES_PATH = File.expand_path(File.join("..", "..", "config", "locales"), __dir__)
5+
6+
# rubocop:disable Naming/ConstantName
7+
I18n = begin
8+
require "i18n"
9+
::I18n.load_path += Dir[File.join(LOCALES_PATH, "*.yml")]
10+
::I18n
11+
rescue LoadError
12+
NullI18n
2513
end
14+
# rubocop:enable Naming/ConstantName
2615
end

spec/examples/to_s_en_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
before(:each) { I18n.locale = :en }
213213

214214
it "uses I18n" do
215-
expect(IceCube::I18n.backend).to eq(I18n)
215+
expect(IceCube::I18n).to eq ::I18n
216216
end
217217

218218
it_behaves_like "to_s in English"

0 commit comments

Comments
 (0)