Skip to content

Commit

Permalink
Merge pull request #482 from jeffjyang/add-i18n-exist-check-without-f…
Browse files Browse the repository at this point in the history
…allback

Add an option to disable fallbacks for the I18n.exists? check
  • Loading branch information
radar authored Jan 20, 2020
2 parents 1799e4e + 04e6f82 commit eac7a9e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ def translate!(key, options = EMPTY_HASH)
alias :t! :translate!

# Returns true if a translation exists for a given key, otherwise returns false.
def exists?(key, _locale = nil, locale: _locale)
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?
config.backend.exists?(locale, key)
config.backend.exists?(locale, key, options)
end

# Transliterates UTF-8 characters to ASCII. By default this method will
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def translate(locale, key, options = EMPTY_HASH)
entry
end

def exists?(locale, key)
def exists?(locale, key, options = EMPTY_HASH)
lookup(locale, key) != nil
end

Expand Down
4 changes: 2 additions & 2 deletions lib/i18n/backend/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def translate(locale, key, default_options = EMPTY_HASH)
throw(:exception, I18n::MissingTranslation.new(locale, key, options))
end

def exists?(locale, key)
def exists?(locale, key, options = EMPTY_HASH)
backends.any? do |backend|
backend.exists?(locale, key)
backend.exists?(locale, key, options)
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/i18n/backend/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def extract_non_symbol_default!(options)
return first_non_symbol_default
end

def exists?(locale, key)
def exists?(locale, key, options = EMPTY_HASH)
return super unless options.fetch(:fallback, true)
I18n.fallbacks[locale].each do |fallback|
begin
return true if super(fallback, key)
Expand Down
14 changes: 14 additions & 0 deletions test/backend/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ def setup
assert_equal 'FOO', I18n.t(:foo, :locale => :'de-DE')
end

test "exists? falls back from de-DE to de given a key missing from the given locale" do
assert_equal true, I18n.exists?(:foo, :locale => :'de-DE')
end

test "exists? should return false when fallback disabled given a key missing from the given locale" do
assert_equal false, I18n.exists?(:foo, :locale => :'de-DE', fallback: false)
end

test "falls back from de-DE to de when there is no translation for de-DE available when using arrays, too" do
assert_equal ['FOO', 'FOO'], I18n.t([:foo, :foo], :locale => :'de-DE')
end
Expand Down Expand Up @@ -212,4 +220,10 @@ def setup
assert_equal false, I18n.exists?(:baz, :de)
assert_equal false, I18n.exists?(:bogus, :'de-DE')
end

test "exists? should return false when fallback is disabled given a key which is missing from the given locale" do
assert_equal true, I18n.exists?(:bar, :'de-DE')
assert_equal false, I18n.exists?(:bar, :'de-DE', fallback: false)
assert_equal false, I18n.exists?(:bar, :'de-DE-XX', fallback: false)
end
end

0 comments on commit eac7a9e

Please sign in to comment.