Proper way to manage i18n strings that could have HTML in them #898
-
|
As I translate my app to use Phlex, one of the things I'm not sure how to solve for is the way I'm doing i18n. I'm using the Ruby i18n library, but this is in a Sinatra app, not rails. I'm porting a custom ERB-based view layer to Phlex. In my custom implementation, my version of I have a few i18n strings like so: contact_support: "Delete some widgets or <a href='mailto:support@example.com'>contact support</a> for a limit increase",If I can avoid HTML in i18n strings entirely, that would be great, but I'm not sure how to do that here, since you generally want to avoid dynamically building sentences since each language constructs them differently. In my custom view layer, all i18n strings are wrapped in my As I'm porting this to Phlex, I'm doing this: p do
raw(safe(t(:contact_support).to_s))
endThis works, but is error-prone and verbose. Even if Phlex were to start using What is the right way to do this?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
I believe Phlex will render the p do
t(:contact_support)
endIf there’s something else in the block, you would have to use p do
plain "Please contact "
raw t(:contact_support)
endA few options for the other bit:
Another option is to make your Phlex’ You could define def t(key)
proc { raw(safe(I18n.whatever(key))) }
endAnd then render translations like this render t(:contact_support) |
Beta Was this translation helpful? Give feedback.
I believe Phlex will render the
to_sof anything that extendsPhlex::SGML::SafeObjectimplicitly. So you can omit therawcall in your example as long as it's the only thing in the block.If there’s something else in the block, you would have to use
raw.A few options for the other bit:
tmethod to return the output ofsafe;Phlex::SGML::SafeObjectinto yourHTMLSafeStringclass; orPhlex::SGML::SafeValueinstead of yourHTMLSafeString, which is essentially the same thing. You can see the implementation here and allsafeis doing is returning …