vfsoraki

vfsoraki

I’m searching for a way to have html in my translations. I’m using Gettext in Phoenix, and I’m translating ids , like the following example:

<%= gettext("score.current", score: @score) %>

Where score.current is translated in different languages, like in English: Your score is %{score} points.

Now I want the translated text to look like You score is <b>%{score}</b> points. What is a nice way of doing it. My own thoughts:

  • Explode translation into fragments: <%= gettext("score.current") %><b><%= @score %></b><%= gettext("points") %>

  • Inject score as html into translation: <%= gettext("score.current", score: make_bold(@score)) %>

Please don’t tell me that I can remove points from end of sentence, this is just an example.

Thanks in advance!

PS. This thread is seeking the same thing as me, but he is using the sentence itself as message id. I don’t event know what is best practice. Should I switch to full sentence as translation id?

First 8 of 8 Posts Switch mode

NobbZ

NobbZ

Have you tried using Phoenix.HTML.raw/1?

egze

egze

I suggest you try to keep html out of translations.

How about something like this:

<%= gettext("score.current", score_html: "<b>#{score}</b>") %>

The GetText key would be: You score is %{score_html} points.

Maybe you’ll need to combine it with HTML.raw.

vfsoraki

vfsoraki OP

What do you mean by that? Use it where?
I didn’t ask how to escape HTML in EEx. I wanted to know what is the best practice.

vfsoraki

vfsoraki OP

Thanks. That’s what I thought is a good way too. Clean.

However I will wait for other replies too, before choosing a solution.

hauleth

hauleth

BTW Gettext approach is that the text to be matched is the in English that should be shown, so in this case it should be:

<%= raw gettext("Your score is <b>%{score}</b> points", score: @score) %>
egze

egze

That’s true, this is much better than score.current. I’d still keep the html out though.

vfsoraki

vfsoraki OP

So best practice is to use full sentence in English. Good to know, since Gettext docs don’t mention this (in a way it shouldn’t).

I am still confused as to whether put html in text, or inject it to text, like

  • Score: %{score} where score is like <b>10</b>
    or
  • Score: <b>%{score}</b> where score is 10
egze

egze

score is like <b>10</b>

  1. You are more flexible to change markup as you wish.
  2. Translators are not going to be confused by HTML.
— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement