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

Commit eac7a9e

Browse files
authored
Merge pull request ruby-i18n#482 from jeffjyang/add-i18n-exist-check-without-fallback
Add an option to disable fallbacks for the I18n.exists? check
2 parents 1799e4e + 04e6f82 commit eac7a9e

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

lib/i18n.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ def translate!(key, options = EMPTY_HASH)
223223
alias :t! :translate!
224224

225225
# Returns true if a translation exists for a given key, otherwise returns false.
226-
def exists?(key, _locale = nil, locale: _locale)
226+
def exists?(key, _locale = nil, locale: _locale, **options)
227227
locale ||= config.locale
228228
raise Disabled.new('exists?') if locale == false
229229
raise I18n::ArgumentError if key.is_a?(String) && key.empty?
230-
config.backend.exists?(locale, key)
230+
config.backend.exists?(locale, key, options)
231231
end
232232

233233
# Transliterates UTF-8 characters to ASCII. By default this method will

lib/i18n/backend/base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def translate(locale, key, options = EMPTY_HASH)
6464
entry
6565
end
6666

67-
def exists?(locale, key)
67+
def exists?(locale, key, options = EMPTY_HASH)
6868
lookup(locale, key) != nil
6969
end
7070

lib/i18n/backend/chain.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ def translate(locale, key, default_options = EMPTY_HASH)
7373
throw(:exception, I18n::MissingTranslation.new(locale, key, options))
7474
end
7575

76-
def exists?(locale, key)
76+
def exists?(locale, key, options = EMPTY_HASH)
7777
backends.any? do |backend|
78-
backend.exists?(locale, key)
78+
backend.exists?(locale, key, options)
7979
end
8080
end
8181

lib/i18n/backend/fallbacks.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def extract_non_symbol_default!(options)
6868
return first_non_symbol_default
6969
end
7070

71-
def exists?(locale, key)
71+
def exists?(locale, key, options = EMPTY_HASH)
72+
return super unless options.fetch(:fallback, true)
7273
I18n.fallbacks[locale].each do |fallback|
7374
begin
7475
return true if super(fallback, key)

test/backend/fallbacks_test.rb

+14
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ def setup
158158
assert_equal 'FOO', I18n.t(:foo, :locale => :'de-DE')
159159
end
160160

161+
test "exists? falls back from de-DE to de given a key missing from the given locale" do
162+
assert_equal true, I18n.exists?(:foo, :locale => :'de-DE')
163+
end
164+
165+
test "exists? should return false when fallback disabled given a key missing from the given locale" do
166+
assert_equal false, I18n.exists?(:foo, :locale => :'de-DE', fallback: false)
167+
end
168+
161169
test "falls back from de-DE to de when there is no translation for de-DE available when using arrays, too" do
162170
assert_equal ['FOO', 'FOO'], I18n.t([:foo, :foo], :locale => :'de-DE')
163171
end
@@ -212,4 +220,10 @@ def setup
212220
assert_equal false, I18n.exists?(:baz, :de)
213221
assert_equal false, I18n.exists?(:bogus, :'de-DE')
214222
end
223+
224+
test "exists? should return false when fallback is disabled given a key which is missing from the given locale" do
225+
assert_equal true, I18n.exists?(:bar, :'de-DE')
226+
assert_equal false, I18n.exists?(:bar, :'de-DE', fallback: false)
227+
assert_equal false, I18n.exists?(:bar, :'de-DE-XX', fallback: false)
228+
end
215229
end

0 commit comments

Comments
 (0)