Posted 2004-06-24T15:49:00+01:00 in xslt

XSLT Template language localization

Messagecatalog and xslt_localizer is a simple system to create localized XSLT templates with fixed string labels, dynamically. This means you don't have to mix conditional code for language support in your XSLT template. You code your XSL template for one language, and then add localizer elements for all fixed text strings that need to be localized for other languages.

Download xslt_localizer.tar.bz2

You write a messagecatalog xml file with string labels for each language you wish to support. E.g.:

<messagecatalog xml:lang="nl">
    <message>
    <key>Heading</key>
    <value>Kop</value>
    </message>
    ...
</messagecatalog> 

You write a XSLT template file with instructions to find the localized string label from the messagecatalog. E.g.:

<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml"
           xmlns:mc="http://www.redslider.net/ns/messagecatalog.1.0"
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="h1">
        <mc:label>Heading</mc:label>
        <xsl:apply-templates/>
    </xsl:template>
</xsl:stylesheet> 

After running the localization script, you end up with an XSLT template for the dutch language that looks like this:

<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml"
         xmlns:mc="http://www.redslider.net/ns/messagecatalog.1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="h1">
        <xsl:text>Kop</xsl:text>
        <xsl:apply-templates/>
    </xsl:template>
</xsl:stylesheet> 

TERMINOLOGY: Localization is also known as internationalization, and both words have acronyms, namely l10n and i18n.

TODO: Allow sentence templates where parameters get mixed in, in an order that can vary for different languages. The programmer would write a string like "No, %(parrots)s cannot be bought at this %(pet shop)s!". Where parrot and pet shop will be system supplied strings. The Dutch translator might write a string like "Nee, deze %(pet shop)s verkoopt geen %(parrots)s!".

HISTORY: this was just an itch I needed to scratch after working on a bilingual part for the rijksmuseum website. Later I noticed a much older post to the XSL list that suggests the same solution: RE: internationalization / localization of XSLT output, by Chris Bayes

Another elegant solution is presented in XSLT Reflection at xml.com, by Jirka Kosek.

Changelog

Written June 2004

August 9, 2004: Add the label element for more elegant fixed string translation.