Skip to content

Commit

Permalink
Merge pull request #371 from ragesoss/master
Browse files Browse the repository at this point in the history
Improve error message for missing pluralization key
  • Loading branch information
radar committed Jul 6, 2017
2 parents dd6ef4a + ed3623f commit f6ef647
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
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

0 comments on commit f6ef647

Please sign in to comment.