Solprovider Lenya Kongregate Registration for Free Flash Games and Chat with solprovider

Using Variables with i18n

Other pages explain how to create keys and several formats for using i18n. There is longer format useful for adding variables. You could use it everywhere, but it is only necessary when you want to dynamically add text to a translated message.

If the expected results are:
Hello, Paul. Your number is 19, and you are a Consultant.

Using the short version, you could have:
<i18n:text key="info-greeting">Hello</i18n:text>, <xsl:value-of select="$firstname">. <i18n:text key="info-number">Your number is</i18n:text> <xsl:value-of select="$number">, <i18n:text key="info-job">and you are a</i18n:text> <xsl:value-of select="$job">.

Now you need to add 3 lines to every translation file.
<message key="info-greeting">Hello</message>
<message key="info-number">Your number is</message>
<message key="info-jobr">and you are a</message>

i18n Translate


An alternative is to use i18n parameters:
<i18n:translate>
<i18n:text i18n:key="info"/>
<i18n:param><xsl:value-of select="$firstname"/></i18n:param>
<i18n:param><xsl:value-of select="$number"/></i18n:param>
<i18n:param><xsl:value-of select="$job"/></i18n:param>
</i18n:translate>


Then you only add one line to every translation file (but you should add a comment so the maintainer knows what variables are available.)
<!-- {0} = FirstName, {1} = EmployeeNumber, {2} = JobTitle -->
<message key="info">Hello, {0}. Your number is {1}, and you are a {2}.</message>


You need to decide which format is better depending on the skills of the language files maintainers, and the translation requirements. They do not need to understand how to use variables with the short version. But the long version allows better language integration. Some language might need to reorder the parameters to:
Greetings, Paul. Consultant is your job title as employee number 19.

The long version allows the parameters to be rearranged, and avoids the need for a fourth line if there is more text.

Naming Variables


This is not documented on the Cocoon website, but I read the i18nTrasnformer source code. You can also name your variables! This was so exciting I even tested it:
<i18n:translate>
<i18n:text i18n:key="info"/>
<i18n:param name="firstname"><xsl:value-of select="$firstname"/></i18n:param>
<i18n:param name="number"><xsl:value-of select="$number"/></i18n:param>
<i18n:param name="jobtitle"><xsl:value-of select="$job"/></i18n:param>
</i18n:translate>


Then use:
<message key="info">Hello, {firstname}. Your number is {number}, and you are a {jobtitle}.</message>

This could be very useful to allow your language maintainers to choose which variables they want:
<i18n:translate>
<i18n:text i18n:key="info"/>
<i18n:param name="firstname"><xsl:value-of select="$firstname"/></i18n:param>
<i18n:param name="lastname"><xsl:value-of select="$lastname"/></i18n:param>
<i18n:param name="number"><xsl:value-of select="$number"/></i18n:param>
<i18n:param name="jobtitle"><xsl:value-of select="$job"/></i18n:param>
</i18n:translate>

<!-- {firstname}, {number} = EmployeeNumber, {jobtitle} -->
<message key="info">Hello, {firstname}. Your number is {number}, and you are a {jobtitle}.</message>
<message key="info">Hello, {firstname}.{lastname} Your number is {number}, and you are a {jobtitle}.</message>

HTML in Translation files


Did I mention you can use HTML in the translation files?

<message key="info"><b>{lastname}, {firstname}</b><br/>Employee number: {number}<br/>Job Title: {jobtitle}.</message>

Mixing Named and Unnamed Parameters


If you do not name all the parameters, the parameters without names are assigned to numbers (starting at 0):
<i18n:translate>
<i18n:text i18n:key="info"/>
<i18n:param name="firstname"><xsl:value-of select="$firstname"/></i18n:param>
<i18n:param name="number"><xsl:value-of select="$number"/></i18n:param>
<i18n:param><xsl:value-of select="$job"/></i18n:param>
</i18n:translate>


Job is now {0},

Default Text


You can still specify default text in the "i18n:text" tag:
<i18n:translate>
<i18n:text i18n:key="info">Hello, {firstname}. Your number is {number}, and you are a {0}.</i18n:text>
<i18n:param name="firstname"><xsl:value-of select="$firstname"/></i18n:param>
<i18n:param name="number"><xsl:value-of select="$number"/></i18n:param>
<i18n:param><xsl:value-of select="$job"/></i18n:param>
</i18n:translate>

No Nesting


Yes, "job" should also be translated. You must use an XSL variable:
<xsl:variable name="job"><i18n:text><xsl:value-of select="$jobtitle"></i18n:text></xsl:variable>

You cannot nest i18n. The following is not valid:
<i18n:translate>
<i18n:text i18n:key="info"/>
<i18n:param name="firstname"><xsl:value-of select="$firstname"/></i18n:param>
<i18n:param name="number"><xsl:value-of select="$number"/></i18n:param>
<i18n:param><i18n:text><xsl:value-of select="$job"/></i18n:text></i18n:param>
</i18n:translate>


<< i18n TranslationContents >>

Contact Solprovider
Paul Ercolino