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

Improve error message for missing pluralization key #371

Merged
merged 1 commit into from Jul 6, 2017
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
4 changes: 2 additions & 2 deletions lib/i18n/backend/base.rb
Expand Up @@ -136,14 +136,14 @@ def resolve(locale, object, subject, options = {})
# - It will pick the :other subkey otherwise.
# - It will pick the :zero subkey in the special case where count is
# equal to 0 and there is a :zero subkey present. This behaviour is
# not stand with regards to the CLDR pluralization rules.
# not standard with regards to the CLDR pluralization rules.
# Other backends can implement more flexible or complex pluralization rules.
def pluralize(locale, entry, count)
return entry unless entry.is_a?(Hash) && count

key = :zero if count == 0 && entry.has_key?(:zero)
key ||= count == 1 ? :one : :other
raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
raise InvalidPluralizationData.new(entry, count, key) unless entry.has_key?(key)
entry[key]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/backend/pluralization.rb
Expand Up @@ -32,7 +32,7 @@ def pluralize(locale, entry, count)
pluralizer = pluralizer(locale)
if pluralizer.respond_to?(:call)
key = count == 0 && entry.has_key?(:zero) ? :zero : pluralizer.call(count)
raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
raise InvalidPluralizationData.new(entry, count, key) unless entry.has_key?(key)
entry[key]
else
super
Expand Down
8 changes: 4 additions & 4 deletions lib/i18n/exceptions.rb
Expand Up @@ -71,10 +71,10 @@ class MissingTranslationData < ArgumentError
end

class InvalidPluralizationData < ArgumentError
attr_reader :entry, :count
def initialize(entry, count)
@entry, @count = entry, count
super "translation data #{entry.inspect} can not be used with :count => #{count}"
attr_reader :entry, :count, :key
def initialize(entry, count, key)
@entry, @count, @key = entry, count, key
super "translation data #{entry.inspect} can not be used with :count => #{count}. key '#{key}' is missing."
end
end

Expand Down
15 changes: 9 additions & 6 deletions test/i18n/exceptions_test.rb
Expand Up @@ -32,16 +32,19 @@ def test_invalid_locale_stores_locale
end
end

test "InvalidPluralizationData stores entry and count" do
test "InvalidPluralizationData stores entry, count and key" do
force_invalid_pluralization_data do |exception|
assert_equal [:bar], exception.entry
assert_equal({:other => "bar"}, exception.entry)
assert_equal 1, exception.count
assert_equal :one, exception.key
end
end

test "InvalidPluralizationData message contains count and data" do
test "InvalidPluralizationData message contains count, data and missing key" do
force_invalid_pluralization_data do |exception|
assert_equal 'translation data [:bar] can not be used with :count => 1', exception.message
assert_match '1', exception.message
assert_match '{:other=>"bar"}', exception.message
assert_match 'one', exception.message
end
end

Expand Down Expand Up @@ -71,7 +74,7 @@ def test_invalid_locale_stores_locale
assert_equal 'reserved key :scope used in "%{scope}"', exception.message
end
end

test "MissingTranslationData#new can be initialized with just two arguments" do
assert I18n::MissingTranslationData.new('en', 'key')
end
Expand All @@ -92,7 +95,7 @@ def force_missing_translation_data(options = {})
end

def force_invalid_pluralization_data
store_translations('de', :foo => [:bar])
store_translations('de', :foo => { :other => 'bar' })
I18n.translate(:foo, :count => 1, :locale => :de)
rescue I18n::ArgumentError => e
block_given? ? yield(e) : raise(e)
Expand Down