vfsoraki

vfsoraki

Gettext with html replacement

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?

Marked As Solved

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.

Also Liked

NobbZ

NobbZ

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

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

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.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement