| ||
![]() |
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>
i18n TranslateAn alternative is to use i18n parameters: <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 -->
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 VariablesThis 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>
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>
HTML in Translation filesDid 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 ParametersIf you do not name all the parameters, the parameters without names are assigned to numbers (starting at 0): <i18n:translate>
Job is now {0}, Default TextYou can still specify default text in the "i18n:text" tag: <i18n:translate>
No NestingYes, "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>
|