Skip to content
This repository was archived by the owner on Dec 28, 2023. It is now read-only.

Commit 217be44

Browse files
Merge pull request ruby-i18n#244 from semaperepelitsa/rescue_nil
locale= and default_locale now raise when setting a truthy value that cannot be converted with #to_sym. Conflicts: test/i18n_test.rb
2 parents b87f163 + a6ea927 commit 217be44

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/i18n/config.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def locale
99
# Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
1010
def locale=(locale)
1111
I18n.enforce_available_locales!(locale)
12-
@locale = locale.to_sym rescue nil
12+
@locale = locale && locale.to_sym
1313
end
1414

1515
# Returns the current backend. Defaults to +Backend::Simple+.
@@ -30,7 +30,7 @@ def default_locale
3030
# Sets the current default locale. Used to set a custom default locale.
3131
def default_locale=(locale)
3232
I18n.enforce_available_locales!(locale)
33-
@@default_locale = locale.to_sym rescue nil
33+
@@default_locale = locale && locale.to_sym
3434
end
3535

3636
# Returns an array of locales for which translations are available.

test/i18n_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def setup
3838
end
3939
end
4040

41+
test "default_locale= doesn't ignore junk" do
42+
assert_raise(NoMethodError) { I18n.default_locale = Class }
43+
end
44+
4145
test "raises an I18n::InvalidLocale exception when setting an unavailable default locale" do
4246
begin
4347
I18n.config.enforce_available_locales = true
@@ -58,6 +62,10 @@ def setup
5862
I18n.locale = :en
5963
end
6064

65+
test "locale= doesn't ignore junk" do
66+
assert_raise(NoMethodError) { I18n.locale = Class }
67+
end
68+
6169
test "raises an I18n::InvalidLocale exception when setting an unavailable locale" do
6270
begin
6371
I18n.config.enforce_available_locales = true

0 commit comments

Comments
 (0)