Merge pull request #3307 from consul/return_string_in_method_pluralize

[Backport] Return a String in I18n method 'pluralize'
This commit is contained in:
Julian Nicolas Herrero
2019-02-18 15:25:40 +01:00
committed by GitHub
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 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

View File

@@ -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

View File

@@ -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