Reputation: 3940
When I call function $translator->trans($key)
, the doc states that the first parameter is the id. To me it seems that the id should relate to the id attribute inside the .xlf file. In reality, it relates to the source element of the .xlf file.
Method call inside controller:
$translator->trans('KUNGFU.CODER')
messages.en.xlf (EN) which does NOT work with the method call above:
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="nl" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="KUNGFU.CODER">
<source>De code fu is sterk bij deze</source> // dutch source message
<target>The code fu is strong inside this one</target>
</trans-unit>
</body>
</file>
</xliff>
messages.en.xlf (EN) which does work with the method call above:
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="nl" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="KUNGFU.CODER">
<source>KUNGFU.CODER</source> // id
<target>The code fu is strong inside this one</target>
</trans-unit>
</body>
</file>
</xliff>
I could of course go ahead and use the second option everywhere but it seems wrong. What am I missing here?
Upvotes: 1
Views: 1465
Reputation: 122
@progonkpa Unfortunately is your example wrong. If you want to have more translation units in the translation file, than you have to write an id even if you do not use it. If you let the id property empty and you have more than one trans-unit, which is 99,99% the case, than you will get an error.
The reason is in the answer of "dbrumann"
And here another example with two and more translation in the same translation file:
<trans-unit id="home" resname="home">...</trans-uni>
<trans-unit id="startseite" resname="startseite">...</trans-unit>
By the way, I have noticed that sometimes if translation doesn't work just clear the cache and try it again...
php bin/console cache:clear
Upvotes: 0
Reputation: 792
Fair warning going with option 2 can make using translations editors tricky. Because when you go to translate it the source will show the id instead of the words to be translated.
An alternative that I use is option 1, but instead of putting the id in the id field put it as a resname="". Symfony will use this resname and the translation file will make a little more sense because most programs people expect the source to be words you want.
If you already have a large xlif file without resnames I add one resname to the first item then use "Easy XML Editor" in table view to copy all the id's to the resname column, takes a minute or so to do.
My two cents.
Matt
Upvotes: 1
Reputation: 17166
You are right with your second option. The translation key uses the source, not the id-attribute.
The id is required by the XLIFF specification:
The required id attribute is used to uniquely identify the within all and elements within the same . [...] The id attribute is used in many elements as a reference to the original corresponding code data or format for the given element. The value of the id element is determined by the tool creating the XLIFF document.
When using another format like YAML you will not have this attribute and therefore it wouldn't make much sense if Symfony were to use it as translation key.
edit: As a side note, if you dump the translations as xliff file using the Symfony command it will populate the id
-attribute with an md5-hash of your source string instead of the translation key.
Upvotes: 3