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:
Julian Herrero
2019-02-07 10:54:28 +01:00
parent 4a12425987
commit 366505f886
3 changed files with 22 additions and 3 deletions

View File

@@ -10,7 +10,7 @@ module I18n
return entry unless entry.is_a?(Hash) && count
key = pluralization_key(entry, count)
return count unless entry.has_key?(key)
return "#{count}" unless entry.has_key?(key)
entry[key]
end

View File

@@ -16,5 +16,6 @@
# end
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.plural(/^(\d+)$/i, '\1')
inflect.irregular 'organización', 'organizaciones'
end

View File

@@ -56,9 +56,27 @@ describe 'I18n' do
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: 1)).to eq(1)
expect(I18n.t("test_plural", count: 2)).to eq(2)
expect(I18n.t("test_plural", count: 1)).to eq("1")
expect(I18n.t("test_plural", count: 2)).to eq("2")
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