Skip to content
  • Sean Doyle's avatar
    7cf52ae9
    Extend `ActionView::Helpers#translate` to yield · 7cf52ae9
    Sean Doyle authored
    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
    7cf52ae9
    Extend `ActionView::Helpers#translate` to yield
    Sean Doyle authored
    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
Loading