bridgetownrb / bridgetown

A next-generation progressive site generator & fullstack framework, powered by Ruby
https://www.bridgetownrb.com
MIT License
1.13k stars 114 forks source link

Can't use arguments in `t` helper in Ruby 3 #617

Closed nashbridges closed 2 years ago

nashbridges commented 2 years ago

Bridgetown Version: 1.1.0 Ruby Version: 3.1.0

To Reproduce

# src/_locales/en.yml

title_template: "%{title} | My site"
# src/_layouts/default.erb

<title><%= t :title_template, title: "Homepage" %></title>

Current behavior

[Bridgetown]   Exception raised: ArgumentError
[Bridgetown] wrong number of arguments (given 2, expected 0..1)
[Bridgetown]                  1: /Users/nash/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/i18n-1.12.0/lib/i18n.rb:210:in `translate'
[Bridgetown]                  2: /Users/nash/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/bridgetown-core-1.1.0/lib/bridgetown-core/helpers.rb:115:in `t'
[Bridgetown]                  3: /Users/nash/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/bridgetown-core-1.1.0/lib/bridgetown-core/ruby_template_view.rb:66:in `method_missing'

The reason is that t uses delegation to I18n.t:

https://github.com/bridgetownrb/bridgetown/blob/2b80b4f7faa9705c788a904ab9028c8fe4d5331e/bridgetown-core/lib/bridgetown-core/helpers.rb#L114-L116

Delegation is tricky to do correctly in order to support both Ruby 2.x and 3.x.

Current workaround

Use I18n.t in templates.

Project wide?

I haven't checked, but maybe there are other places in the project where delegation is used. These should be fixed as well.

nashbridges commented 2 years ago

Just noticed the recent (unreleased) PR fixed the issue.

maybe there are other places in the project where delegation is used

That is worth to check anyway.