0
votes

I have to translate a website where the page content can be writen in English or in French.
So I made two files in my translations repository:

  • message.en.yml (to translate french to english when the locale is en_us or en_gb)
  • message.fr.yml (to translate english to french when the locale is fr)

To explain my issue I will take an exemple, the pair of word Hardware | Matériel.

When the page is viewed with fr locale Hardware need to be translated by Matériel.
So I put : Hardware: Matériel in my message.fr.yml.

When the page is viewed with en_us or en_gb locale Matériel need to be translated by Hardware.
So I put : Matériel: Hardware in my message.en.yml.

At this point the translation behaviour is:
When the page is viewed with the fr locale Hardware is correctly translated.
When the page is viewed with the en_gb locale Hardware is correctly not translated.
When the page is viewed with the en_us locale Hardware is incorrectly translated by Matériel.

To add to the fun, if I clear the Symfony cache, the behaviour change, sometimes everything get translated by Matériel, sometimes nothing does...
A week ago I only needed to translate French to English and it worked perfectly by just using the message.en.yml file, maybe there is something to do that I didn't noticed when you use several files.
Can someone explain me how to handle this two way translation?

2

2 Answers

1
votes

Use variables.

I'm using translation like this:

messages.en.yml

string.hardware: Hardware

messages.fr.yml

string.hardware: Matériel

In twig:

{{ "string.hardware"|trans }}

Using with dynamic variables:

string.hardware: %variable% Hardware

Usage in twig:

{{ "string.hardware"|trans({"%variable%": your_variable_or_string}) }}
0
votes

Okay, I understood what I was doing wrong. For this kind of situation you have to:

  • Put : Hardware: Matériel in my message.fr.yml. (if fr locale translate Hardwareby Matériel)
  • Put : Matériel: Hardware in my message.en.yml. (if en locale translate Matérielby Hardware)
  • Put : Hardware: Hardware in my message.en.yml. (if en locale don't translate Hardware)

I think all of this happend because of the translation fallback, I ask for the translation of Hardwareso symfony look for one in message.en.yml.first (because the locale is en), if it doens't find any, it looks for it in message.fr.yml, here it found Hardware: Matérielso it use it.
I still don't get why sometime, depending on the cache, the translation worked fine on en_gb locale...