Gettext html in translation

Does anybody have any ideas or experience with using Gettext in a template, and having html embedded within the translated text? I’m currently translating fragments, like below, but this feels wrong and makes it more difficult to translate. Embedding raw html in the translation also seems wrong, since the translator may not even know html.

  <%= gettext "Already have an account?" %>
  <a href="/login"><%= gettext "Sign in" %></a>
  <%= gettext "to continue" %>
1 Like

This would work (using interpolation and the raw function):

  <%= gettext("Already have an account? %{a_start}Sign in%{a_end} to continue", a_start: "<a href='/login'>", a_end: "</a>") |> raw() %>

or without interpolation with the html tags directly in the text:

  <%= gettext("Already have an account? <a href='/login'>Sign in</a> to continue") |> raw() %>

You interpolate the html tags like you would interpolate anything else and then simply pass everything that gettext spits out to raw function. This assumes that the gettext text is safe. Keep that in mind.


Yeah, that’s how you do it.

You can also do the following (this is an example from a production app with translations):

  <%= gettext("Already have an account? %{sign_in} to continue", sign_in: safe_to_string(link(gettext("Sign in"), to: Routes.session_path(@conn, :new)))) |> raw() %>

A translator will usually have difficulty when there’s inline HTML, unless it’s very simple. There can also be issues with the open and close tags unless your translation platform verifies that these interpolations are intact and in the right order. I would keep all HTML out of the translations.


This is exactly what I was looking for, much appreciated!

Thanks for this. Interpolation definitely seems like the better way here since it would allow for easier editing of the html tags, in case a class would need to be added or removed for example.