Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix translate with nil and empty keys #400

Merged
merged 1 commit into from Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/i18n.rb
Expand Up @@ -161,7 +161,6 @@ def translate(*args)
handling = options.delete(:throw) && :throw || options.delete(:raise) && :raise # TODO deprecate :raise

enforce_available_locales!(locale)
raise I18n::ArgumentError if key.is_a?(String) && key.empty?

result = catch(:exception) do
if key.is_a?(Array)
Expand Down
3 changes: 3 additions & 0 deletions lib/i18n/backend/base.rb
Expand Up @@ -22,7 +22,10 @@ def store_translations(locale, data, options = {})
end

def translate(locale, key, options = {})
raise I18n::ArgumentError if (key.is_a?(String) || key.is_a?(Symbol)) && key.empty?
raise InvalidLocale.new(locale) unless locale
return nil if key.nil? && !options.key?(:default)

entry = lookup(locale, key, options[:scope], options) unless key.nil?

if entry.nil? && options.key?(:default)
Expand Down
26 changes: 21 additions & 5 deletions test/i18n_test.rb
Expand Up @@ -216,6 +216,22 @@ def setup
assert_raise(I18n::ArgumentError) { I18n.t("") }
end

test "translate given an empty symbol as a key raises an I18n::ArgumentError" do
assert_raise(I18n::ArgumentError) { I18n.t(:"") }
end

test "translate given an array with empty string as a key raises an I18n::ArgumentError" do
assert_raise(I18n::ArgumentError) { I18n.t(["", :foo]) }
end

test "translate given an empty array as a key returns empty array" do
assert_equal [], I18n.t([])
end

test "translate given nil returns nil" do
assert_nil I18n.t(nil)
end

test "translate given an unavailable locale rases an I18n::InvalidLocale" do
begin
I18n.config.enforce_available_locales = true
Expand Down Expand Up @@ -405,17 +421,17 @@ def call(exception, locale, key, options); key; end
I18n.config.enforce_available_locales = false
end
end

test 'I18n.reload! reloads the set of locales that are enforced' do
begin
# Clear the backend that affects the available locales and somehow can remain
# set from the last running test.
# For instance, it contains enough translations to cause a false positive with
# this test when ran with --seed=50992
I18n.backend = I18n::Backend::Simple.new

assert !I18n.available_locales.include?(:de), "Available locales should not include :de at this point"

I18n.enforce_available_locales = true

assert_raise(I18n::InvalidLocale) { I18n.default_locale = :de }
Expand All @@ -431,11 +447,11 @@ def call(exception, locale, key, options); key; end
store_translations(:en, :foo => 'Foo in :en')
store_translations(:de, :foo => 'Foo in :de')
store_translations(:pl, :foo => 'Foo in :pl')

assert I18n.available_locales.include?(:de), ":de should now be allowed"
assert I18n.available_locales.include?(:en), ":en should now be allowed"
assert I18n.available_locales.include?(:pl), ":pl should now be allowed"

assert_nothing_raised { I18n.default_locale = I18n.locale = :en }
assert_nothing_raised { I18n.default_locale = I18n.locale = :de }
assert_nothing_raised { I18n.default_locale = I18n.locale = :pl }
Expand Down