Return a String in I18n method 'pluralize'
If a translation was missing returning a Fixnum was raising an exception 'undefined method X for Fixnum'.
This commit is contained in:
@@ -10,7 +10,7 @@ module I18n
|
|||||||
return entry unless entry.is_a?(Hash) && count
|
return entry unless entry.is_a?(Hash) && count
|
||||||
|
|
||||||
key = pluralization_key(entry, count)
|
key = pluralization_key(entry, count)
|
||||||
return count unless entry.has_key?(key)
|
return "#{count}" unless entry.has_key?(key)
|
||||||
entry[key]
|
entry[key]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -16,5 +16,6 @@
|
|||||||
# end
|
# end
|
||||||
|
|
||||||
ActiveSupport::Inflector.inflections(:en) do |inflect|
|
ActiveSupport::Inflector.inflections(:en) do |inflect|
|
||||||
|
inflect.plural(/^(\d+)$/i, '\1')
|
||||||
inflect.irregular 'organización', 'organizaciones'
|
inflect.irregular 'organización', 'organizaciones'
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -56,9 +56,27 @@ describe 'I18n' do
|
|||||||
I18n.backend.store_translations(:en, { test_plural: keys })
|
I18n.backend.store_translations(:en, { test_plural: keys })
|
||||||
|
|
||||||
expect(I18n.t("test_plural", count: 0)).to eq("No comments")
|
expect(I18n.t("test_plural", count: 0)).to eq("No comments")
|
||||||
expect(I18n.t("test_plural", count: 1)).to eq(1)
|
expect(I18n.t("test_plural", count: 1)).to eq("1")
|
||||||
expect(I18n.t("test_plural", count: 2)).to eq(2)
|
expect(I18n.t("test_plural", count: 2)).to eq("2")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "returns a String to avoid exception 'undefined method for Fixnum'" do
|
||||||
|
keys = { zero: "No comments" }
|
||||||
|
I18n.backend.store_translations(:en, { test_plural: keys })
|
||||||
|
|
||||||
|
result = I18n.t("test_plural", count: 1)
|
||||||
|
expect(result.class).to be String
|
||||||
|
expect { result.pluralize }.not_to raise_error
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns the number not pluralized for missing translations" do
|
||||||
|
keys = { zero: "No comments" }
|
||||||
|
I18n.backend.store_translations(:en, { test_plural: keys })
|
||||||
|
|
||||||
|
expect(I18n.t("test_plural", count: 1).pluralize).to eq "1"
|
||||||
|
expect(I18n.t("test_plural", count: 2).pluralize).to eq "2"
|
||||||
|
expect(I18n.t("test_plural", count: 1).pluralize).not_to eq "1s"
|
||||||
|
expect(I18n.t("test_plural", count: 2).pluralize).not_to eq "2s"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user