Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/dry/schema/messages/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
result = t.(key, **options)

if result.is_a?(Hash)
text = result[:text]
Expand Down Expand Up @@ -112,7 +113,10 @@ def interpolate(key, options, **data)

resolved_key = key?(text_key, opts) ? text_key : key

t.(resolved_key, **opts)
result = t.(resolved_key, **opts)
return result unless result.is_a?(Hash)

result[:text]
end

private
Expand Down
31 changes: 31 additions & 0 deletions spec/integration/messages/i18n_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ def store_errors(**errors)
expect(meta).to eql({})
end

it "can use a proc returning hash for a message" do
store_errors(
predicate_proc: ->(_path, **opts) { {text: opts[:text], code: 123, name: opts[:name]} }
)

template, meta = messages[:predicate_proc, path: :path, text: "text", name: "abc"]

expect(template.()).to eql("text")
expect(meta).to eql({code: 123, name: "abc"})
end

context "with meta-data" do
it "finds the meta-data" do
store_errors(
Expand All @@ -203,6 +214,26 @@ def store_errors(**errors)
expect(meta).to eql(code: 123)
end
end

context "with meta-data and arg type" do
it "returns a template for a specific rule, meta and default arg type" do
store_errors(
predicate_with_meta: {
arg: {
default: {
text: "text %{arg}",
code: 123
}
}
}
)

template, meta = messages[:predicate_with_meta, path: :pages, locale: :en]

expect(template.(arg: "some arg")).to eql("text some arg")
expect(meta).to eq({code: 123})
end
end
Comment on lines +218 to +236
Copy link
Author

@rrothenberger rrothenberger Aug 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I wanted to jest drop the logic around text_key in lib/dry/schema/messages/i18n.rb - but it was making this possible and there might be people relying on this behavior so I thought it was worth to add test for it.

end

context "with dynamic locale" do
Expand Down