diff --git a/lib/dry/schema/messages/i18n.rb b/lib/dry/schema/messages/i18n.rb index 49e9be54..e13a6483 100644 --- a/lib/dry/schema/messages/i18n.rb +++ b/lib/dry/schema/messages/i18n.rb @@ -32,7 +32,8 @@ def initialize def get(key, options = EMPTY_HASH) return unless key - result = t.(key, locale: options.fetch(:locale, default_locale)) + options = {locale: default_locale, **options} + result = t.(key, **options) if result.is_a?(Hash) text = result[:text] diff --git a/spec/integration/messages/i18n_spec.rb b/spec/integration/messages/i18n_spec.rb index 44194602..16a95a30 100644 --- a/spec/integration/messages/i18n_spec.rb +++ b/spec/integration/messages/i18n_spec.rb @@ -203,6 +203,42 @@ def store_errors(**errors) expect(meta).to eql(code: 123) end end + + context "with plural message" do + before do + store_errors( + apples: { + one: "single apple", + other: "%{count} apples" + } + ) + end + + it "uses correct variant" do + template, meta = messages[:apples, path: :path, count: 3] + + expect(template.()).to eql("3 apples") + expect(meta).to eql({}) + end + + context "without count" do + it "returns meta" do + template, meta = messages[:apples, path: :path] + + expect(template.()).to eql({ + one: "single apple", + other: "%{count} apples" + }) + + expect(template.(count: 1)).to eql("single apple") + + expect(meta).to eql({ + one: "single apple", + other: "%{count} apples" + }) + end + end + end end context "with dynamic locale" do