Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass options as keyword arguments to translation procs #529

Merged
merged 1 commit into from
Jun 4, 2020
Merged
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
2 changes: 1 addition & 1 deletion lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def resolve(locale, object, subject, options = EMPTY_HASH)
I18n.translate(subject, **options.merge(:locale => locale, :throw => true))
when Proc
date_or_time = options.delete(:object) || object
resolve(locale, object, subject.call(date_or_time, options))
resolve(locale, object, subject.call(date_or_time, **options))
else
subject
end
Expand Down
11 changes: 6 additions & 5 deletions lib/i18n/tests/localization/procs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,20 @@ module Procs
test "localize Time: given a format that resolves to a Proc it calls the Proc with the object" do
setup_time_proc_translations
time = ::Time.utc(2008, 3, 1, 6, 0)
assert_equal I18n::Tests::Localization::Procs.inspect_args([time, {}]), I18n.l(time, :format => :proc, :locale => :ru)
assert_equal I18n::Tests::Localization::Procs.inspect_args([time], {}), I18n.l(time, :format => :proc, :locale => :ru)
end

test "localize Time: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
setup_time_proc_translations
time = ::Time.utc(2008, 3, 1, 6, 0)
options = { :foo => 'foo' }
assert_equal I18n::Tests::Localization::Procs.inspect_args([time, options]), I18n.l(time, **options.merge(:format => :proc, :locale => :ru))
assert_equal I18n::Tests::Localization::Procs.inspect_args([time], options), I18n.l(time, **options.merge(:format => :proc, :locale => :ru))
end

protected

def self.inspect_args(args)
def self.inspect_args(args, kwargs)
args << kwargs
args = args.map do |arg|
case arg
when ::Time, ::DateTime
Expand All @@ -85,12 +86,12 @@ def setup_time_proc_translations
I18n.backend.store_translations :ru, {
:time => {
:formats => {
:proc => lambda { |*args| I18n::Tests::Localization::Procs.inspect_args(args) }
:proc => lambda { |*args, **kwargs| I18n::Tests::Localization::Procs.inspect_args(args, kwargs) }
}
},
:date => {
:formats => {
:proc => lambda { |*args| I18n::Tests::Localization::Procs.inspect_args(args) }
:proc => lambda { |*args, **kwargs| I18n::Tests::Localization::Procs.inspect_args(args, kwargs) }
},
:'day_names' => lambda { |key, options|
(options[:format] =~ /^%A/) ?
Expand Down
5 changes: 5 additions & 0 deletions lib/i18n/tests/procs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module Procs
assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(:a_lambda, :foo => 'foo')
end

test "lookup: given a translation is a proc it passes the interpolation values as keyword arguments" do
I18n.backend.store_translations(:en, :a_lambda => lambda { |key, foo:, **| I18n::Tests::Procs.filter_args(key, foo: foo) })
assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(:a_lambda, :foo => 'foo')
end

test "defaults: given a default is a Proc it calls it with the key and interpolation values" do
proc = lambda { |*args| I18n::Tests::Procs.filter_args(*args) }
assert_equal '[nil, {:foo=>"foo"}]', I18n.t(nil, :default => proc, :foo => 'foo')
Expand Down