Re: internationalization / localization of XSLT output

Subject: Re: internationalization / localization of XSLT output
From: Tony Graham <tgraham@xxxxxxxxxxxxxxxx>
Date: Fri, 25 Aug 2000 18:25:10 -0400 (EST)
At 24 Aug 2000 12:03 -0600, Mike Brown wrote:
 > XML has a facility for communicating language-specific information in a
 > standard way: the xml:lang attribute, which you can put on any element,
 > and it will apply to that element and all its descendants, until
 > redeclared, just like namespaces.
 > 
 > XSLT/XPath have a function for accessing the language that is in effect
 > for a given element, whether it is declared on that element on or one of
 > its ancestors: lang().

If you do use xml:lang, then you can also do:

<xsl:template name="gettext">
  <xsl:param name="string-name"/>

  <xsl:choose>
    <xsl:when test="$string-name='next page'">
      <xsl:choose>
        <xsl:when test="lang()='de'">naechste Seite</xsl:when>
	<xsl:otherwise>next page</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$string-name='Results of database query'">
      <xsl:choose>
        <xsl:when test="lang()='de'">Ergebnis der Datenbankabfrage</xsl:when>
	<!-- Sometimes you do need to select based on subfields -->
	<xsl:when test="lang()='zh'">
	  <xsl:variable name="lang"
	                select="translate(ancestor-or-self::*/@xml:lang,
			                  'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
			                  'abcdefghijklmnopqrstuvwxyz')"/>
	  <xsl:choose>
	    <xsl:when test="$lang='zh-tw'">Traditional Chinese text</xsl:when>
	    <xsl:when test="$lang='zh-cn'">Simplified Chinese text</xsl:when>
	  </xsl:choose>
	</xsl:when>
	<!-- You can use the default string as the parameter and
	     output it as the default when no other language matched -->
	<xsl:otherwise><xsl:value-of select="$string-name"/></xsl:otherwise>
      </xsl:choose>
    </xsl:when>
  </xsl:choose>
</xsl:template>

>From your template, all you need is an xsl:call-template with the
right parameter value:

<xsl:template match="/">
  <html>
    <body>
      <h1><xsl:call-template name="gettext">
            <xsl:with-param name="string-name"
	                    select="'Results of database query'"/>
          </xsl:call-template></h1>
    </body>
  </html>
</xsl:template>

You could also do similar stuff when the language is a parameter to
the stylesheet.

Actually, you could use a second stylesheet to generate the named
template above from the sample data shown in other messages in this
thread.  Generating the "zh-tw" and "zh-cn" cases would be tricky if
you are using xml:lang, but those cases wouldn't need the interior
xsl:choose if the language identifier is a parameter to the
stylesheet, since a simple comparison to the language identifier
variable would be sufficient.

Regards,


Tony Graham
======================================================================
Tony Graham                            mailto:tgraham@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9632
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread