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.

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

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

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

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

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

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.

2 Likes

Yeah, that’s how you do it.

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

<p>
  <%= 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() %>
</p>

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.

5 Likes

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.