Re: [xsl] xslt and i18n

Subject: Re: [xsl] xslt and i18n
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 16 Feb 2009 12:20:16 -0500
At 2009-02-16 17:06 +0000, Andrew Welch wrote:
Regarding localisation support in xslt, I've always thought we could
take advantage of dynamic despatch, import precedence or template
priorities etc

Here's a simple example:

<xsl:stylesheet version="2.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema";
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:f="f"
    exclude-result-prefixes="xs f">

<xsl:param name="locale" select="'en_GB'"/>

    <xsl:variable name="lang" select="tokenize($locale, '_')[1]"/>
    <xsl:variable name="country" select="tokenize($locale, '_')[2]"/>

    <xsl:function name="f:getLocalisation" as="xs:string">
        <xsl:param name="key" as="xs:string"/>

        <xsl:variable name="lookup">
            <xsl:element name="{$country}">
                <xsl:element name="{$lang}">
                    <xsl:element name="{$key}"/>
                </xsl:element>
            </xsl:element>
        </xsl:variable>

        <xsl:apply-templates select="$lookup/*/*"/>
    </xsl:function>

    <xsl:template match="/">
            <xsl:value-of select="f:getLocalisation('page.title')"/>
    </xsl:template>

    <xsl:template match="page.title">Default Title</xsl:template>
    <xsl:template match="en/page.title">General English page
title</xsl:template>
    <xsl:template match="US/en/page.title">Specific US English page
title</xsl:template>

The second and third match above will cause a template conflict because they both have an implicit priority=".5". Your most specific match, the third one, should have a higher explicit priority.


</xsl:stylesheet>

The key ("page.title" here) and the locale is passed in as a
parameter.  The localised values are stored as templates.  The key and
locale are converted into elements, and then the apply-templates call
is made on that temporary tree... and then the rest is taken care of.

I suppose ... the localization information is in a data file indicated by an invocation parameter. You are relying on XSLT 2 or an extension.


I'm supporting fourteen languages for the United Nations Layout Key for Invoice, using 14 differently-named top-level stylesheets:

  http://www.CraneSoftwrights.com/resources/ublss/#ubl2-ss
  Bosnian, Czech, Danish, Dutch, English, Finnish, French,
  German, Icelandic, Italian, Norwegian, Portuguese, Slovak
  and Spanish

Of course everything is modular and I use the stylesheet importation to override the English base strings with whatever strings are available in the given language. Volunteers have submitted the language translations just by editing a small prototypical stylesheet fragment with only global variable declarations ... most of them don't know XSLT.

Any ideas on how to improve this?

What benefit are you looking for by which improvement could be measured?


. . . . . . . . . . . . Ken

--
Upcoming hands-on  XQuery, XSLT, UBL & code list training classes:
Brussels, BE 2009-03;  Prague, CZ 2009-03, http://www.xmlprague.cz
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread