mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
7cf52ae981
This commit extends the `ActionView::Helpers#translate` (and by way of alias, `#t`) helper methods to accept blocks. When invoked with a block, the `translate` call will yield the translated text as its first block argument, along with the resolved translation key as its second: ```erb <%= translate(".key") do |translation, resolved_key| %> <span data-i18n-key="<%= resolved_key %>"><%= translation %></span> <% end %> ``` In cases where relative translation keys are foregone in lieu of fully qualified keys, or if the caller is not interested in the resolved key, the second block argument can be omitted: ```erb <%= translate("action.template.key") do |translation| %> <p><%= translation %></p> <p><%= translation %>, but a second time</p> <% end %> ``` A benefit of yielding the translation is that it enabled template-local variable re-use. Alternatively, [`Object#tap`][tap] could be used. Prior to this commit, however, the resolution of the translation key was internal to `ActionView`, and unavailable to the caller (unless they were willing to explicitly determine the resolved key themselves). By making it available as a block parameter, it could be used to annotate the translated value in the resulting elements. [tap]: https://ruby-doc.org/core-2.7.0/Object.html#method-i-tap
3 lines
65 B
Text
3 lines
65 B
Text
<%= t('.foo') do |translation| %>
|
|
<%= translation %>
|
|
<% end %>
|