-
Sean Doyle authored
When translating a `<label>` element's contents, it is difficult (or "possible", yet undocumented) to make the translation text available to a block scope. For instance, when rendering a `rich_text_area`, passing the `aria-label` attribute might be important. Prior to this commit, doing so would require a double lookup of the translation key: ```erb <%# one time here, implicitly %> <%= form.label(:content) do %> <%= form.rich_text_area( :content, # one time here, explicitly "aria-label" => translate("helpers.label.post.content"), ) %> <% end %> ``` The current implementation of the `#label` helper method already yields an instance of `ActionView::Helpers::Tags::Label::LabelBuilder`, but that class is undocumented. Instance of that class respond to `#translation` calls, which will return the translated text content. By aliasing `#translation` to `#to_s`, we're able to expose that value without the burden of exposing an additional class to the public API. Instead, view-level interpolation (either `<%= %>`, `#{ }`, or direct calls to [`capture`][capture] will coerce the value to a String, and implicitly invoke `#translation`. The new view code might look something like this: ```erb <%= form.label(:content) do |label| %> <%= form.rich_text_area(:content, "aria-label" => label) %> <% end %> ``` Callers of the helper are still free to omit the block parameter. [capture]: https://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-capture
Sean Doyle authoredWhen translating a `<label>` element's contents, it is difficult (or "possible", yet undocumented) to make the translation text available to a block scope. For instance, when rendering a `rich_text_area`, passing the `aria-label` attribute might be important. Prior to this commit, doing so would require a double lookup of the translation key: ```erb <%# one time here, implicitly %> <%= form.label(:content) do %> <%= form.rich_text_area( :content, # one time here, explicitly "aria-label" => translate("helpers.label.post.content"), ) %> <% end %> ``` The current implementation of the `#label` helper method already yields an instance of `ActionView::Helpers::Tags::Label::LabelBuilder`, but that class is undocumented. Instance of that class respond to `#translation` calls, which will return the translated text content. By aliasing `#translation` to `#to_s`, we're able to expose that value without the burden of exposing an additional class to the public API. Instead, view-level interpolation (either `<%= %>`, `#{ }`, or direct calls to [`capture`][capture] will coerce the value to a String, and implicitly invoke `#translation`. The new view code might look something like this: ```erb <%= form.label(:content) do |label| %> <%= form.rich_text_area(:content, "aria-label" => label) %> <% end %> ``` Callers of the helper are still free to omit the block parameter. [capture]: https://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-capture
Loading