Reputation: 1183
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:
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?
Upvotes: 0
Views: 67
Reputation: 1183
Okay, I understood what I was doing wrong. For this kind of situation you have to:
Hardware: Matériel
in my message.fr.yml. (if fr locale translate Hardware
by Matériel
)Matériel: Hardware
in my message.en.yml. (if en locale translate Matériel
by Hardware
)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 Hardware
so 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ériel
so it use it.
I still don't get why sometime, depending on the cache, the translation worked fine on en_gb locale...
Upvotes: 0
Reputation: 3870
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}) }}
Upvotes: 1