Skip to content

Commit 3d26fbc

Browse files
committed
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
1 parent 10ae8dc commit 3d26fbc

File tree

4 files changed

+12
-23
lines changed

4 files changed

+12
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

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)
13+
- Fixed `I18n.load_path` injection ([#546](https://github.com/ice-cube-ruby/ice_cube/pull/546)) by [@glaszig](https://github.com/glaszig)
1314

1415
### Fixed
1516
- 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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
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"), __FILE__)
5+
6+
I18n = begin
7+
require "i18n"
8+
::I18n.load_path += Dir[File.join(LOCALES_PATH, "*.yml")]
9+
::I18n
10+
rescue LoadError
11+
NullI18n
2512
end
2613
end

spec/examples/to_s_en_spec.rb

Lines changed: 1 addition & 1 deletion
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)