From 869460a0050956030045257e367d7544230860c3 Mon Sep 17 00:00:00 2001 From: KinWang-2013 <94952098+KinWang-2013@users.noreply.github.com> Date: Fri, 19 Jul 2024 19:15:35 +0600 Subject: [PATCH] return error on nil key and add test --- lib/i18n.rb | 3 ++- test/i18n_test.rb | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/i18n.rb b/lib/i18n.rb index 2980f025..d63a1069 100644 --- a/lib/i18n.rb +++ b/lib/i18n.rb @@ -264,7 +264,8 @@ def interpolation_keys(key, **options) def exists?(key, _locale = nil, locale: _locale, **options) locale ||= config.locale raise Disabled.new('exists?') if locale == false - raise I18n::ArgumentError if key.is_a?(String) && key.empty? + raise I18n::ArgumentError if (key.is_a?(String) && key.empty?) || key.nil? + config.backend.exists?(locale, key, options) end diff --git a/test/i18n_test.rb b/test/i18n_test.rb index 85679d40..642920de 100644 --- a/test/i18n_test.rb +++ b/test/i18n_test.rb @@ -315,6 +315,10 @@ def setup assert_raises(I18n::ArgumentError) { I18n.interpolation_keys(["bad-argument"]) } end + test "exists? given nil raises I18n::ArgumentError" do + assert_raises(I18n::ArgumentError) { I18n.exists?(nil) } + end + test "exists? given an existing key will return true" do assert_equal true, I18n.exists?(:currency) end