lepix
lepix

Reputation: 4990

Translatable content and HTML tags

I use Twig and I want to make the following content translatable :

{% trans %}
You have actually <span class='messageNumber'>{{messageNumber}} message(s)</span> in your mailbox. 
{% endtrans %}

But when this translatable content will be parsed by POEdit and sent to translators, they will see the <span> tags and attributes. What can I do to avoid this ?

I thought about doing this way :

{% messageNumberFormatted = "<span class='messageNumber'>"~messageNumber~"message(s)</span>" %}

{% trans %}
You have actually {{messageNumberFormatted}} in your mailbox.
{% endtrans %}

But isn't it a bit heavy or even bad practice for the translators ? In that case, they can't even see the word "message".

Upvotes: 1

Views: 539

Answers (2)

greg0ire
greg0ire

Reputation: 23255

First, you should use transchoice with explicit interval pluralization, like this :

{% transchoice message_count %}
   {0}You have {{no messages}} yet|{1}You have {{one message}}|]1,+Inf]You have {{%count% messages}}.
{% endtranschoice %}

Then maybe you could use replace to replace {{ with the opening tag, and }} with the closing tag. I don't know whether you can directly chain like this

{% transchoice message_count | replace('...') %}

Or if you must store in a variable by using set first.

Upvotes: 2

Reuven
Reuven

Reputation: 3336

You can use the trans twig filer with keys representing your sentences.

{{ you.have.actually|trans }} <span class='messageNumber'> {{ messageNumber message|trans }} </span> {{ in.your.mailbox|trans }}

Upvotes: 1

Related Questions