Skip to content

Commit 3d93679

Browse files
committed
Fix handling of I18n proc messages when returning hashes
1 parent 13ddb51 commit 3d93679

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

example.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require "dry-schema"
2+
require "i18n"
3+
4+
I18n.backend.store_translations(:en, dry_schema: {
5+
errors: {
6+
max_size?: ->(*, **kargs) { {text: "is over limit", max_size: kargs[:num]} }
7+
}
8+
}
9+
)
10+
11+
schema = Dry::Schema.Params do
12+
config.messages.backend = :i18n
13+
config.messages.load_paths = []
14+
15+
required(:type).value(:string, max_size?: 1)
16+
end
17+
18+
puts schema.call({type: "abc"}).errors.to_h

lib/dry/schema/message_compiler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def visit_predicate(node, opts)
140140
path: path.last, **tokens, **lookup_options(arg_vals: arg_vals, input: input)
141141
).to_h
142142

143-
template, meta = messages[predicate, options]
143+
template, meta = messages[predicate, {**tokens, **options}]
144144

145145
unless template
146146
raise MissingMessageError.new(path, messages.looked_up_paths(predicate, options))

lib/dry/schema/messages/i18n.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def initialize
3232
def get(key, options = EMPTY_HASH)
3333
return unless key
3434

35-
result = t.(key, locale: options.fetch(:locale, default_locale))
35+
options[:locale] ||= default_locale
36+
result = t.(key, **options)
3637

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

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

115-
t.(resolved_key, **opts)
116+
result = t.(resolved_key, **opts)
117+
return result unless result.is_a?(Hash)
118+
119+
result[:text]
116120
end
117121

118122
private

spec/integration/messages/i18n_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ def store_errors(**errors)
188188
expect(meta).to eql({})
189189
end
190190

191+
it "can use a proc returning hash for a message" do
192+
store_errors(
193+
predicate_proc: ->(_path, **opts) { {text: opts[:text], code: 123, name: opts[:name]} }
194+
)
195+
196+
template, meta = messages[:predicate_proc, path: :path, text: "text", name: "abc"]
197+
198+
expect(template.()).to eql("text")
199+
expect(meta).to eql({code: 123, name: "abc"})
200+
end
201+
191202
context "with meta-data" do
192203
it "finds the meta-data" do
193204
store_errors(
@@ -203,6 +214,26 @@ def store_errors(**errors)
203214
expect(meta).to eql(code: 123)
204215
end
205216
end
217+
218+
context "with meta-data and arg type" do
219+
it "returns a template for a specific rule, meta and default arg type" do
220+
store_errors(
221+
predicate_with_meta: {
222+
arg: {
223+
default: {
224+
text: "text %{arg}",
225+
code: 123
226+
}
227+
}
228+
}
229+
)
230+
231+
template, meta = messages[:predicate_with_meta, path: :pages, locale: :en]
232+
233+
expect(template.(arg: "some arg")).to eql("text some arg")
234+
expect(meta).to eq({code: 123})
235+
end
236+
end
206237
end
207238

208239
context "with dynamic locale" do

0 commit comments

Comments
 (0)