You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Scott Boag/CAM/Lotus <Sc...@lotus.com> on 2000/07/16 22:36:51 UTC

Re: Internationalization Support

A number of people have been asking me for a while how one might do
internationalization support in XSLT, or any resource management, for that
matter.  I've always had a notion that it could be done with xsl:key and an
external xml file.  The following is a simple example of doing this.  One
could make this easier by making an extension that would simplify the
calling process... if anyone wants to do this, let me know, and we can add
it to the standard extensions package.

=== translations.xml ===
<?xml version="1.0" encoding="ISO-8859-1"?>
<translations>
  <!-- excuse my ignorant language translations -- I am non-english
illiterate -->
  <!-- (actually, I'm pretty illiterate with english...)  -->
  <language id="french">
    <word id="button">bouton</word>
    <word id="enter">entrer</word>
    <word id="press">presse</word>
    <word id="find">discerner</word>
    <word id="language">langue</word>
  </language>
  <language id="spanish">
    <word id="button">botón</word>
    <word id="enter">entrar</word>
    <word id="press">apretar</word>
    <word id="find">hallar</word>
    <word id="language">lengua</word>
  </language>
  <language id="english">
    <word id="button">button</word>
    <word id="enter">enter</word>
    <word id="press">press</word>
    <word id="find">find</word>
    <word id="language">language</word>
  </language>
</translations>
==================

=== test.xsl ===
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version
="1.0">
  <xsl:param name="lang" select="english"/>
  <xsl:key name="english" match="language[@id='english']/word" use="@id"/>
  <xsl:key name="french" match="language[@id='french']/word" use="@id"/>
  <xsl:key name="spanish" match="language[@id='spanish']/word" use="@id"/>

  <xsl:template name="translate">
    <xsl:param name="language" select="$lang"/>
    <xsl:param name="word"/>
    <!-- in XSLT2, there will be a way to do this without the for-each -->
    <!-- (I tried to get this into XSLT1!) -->
    <xsl:for-each select="document('translations.xml')">
      <xsl:value-of select="key($language, $word)"/>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="/">
    <out>
      <!-- Using a named template isn't much easier than directly calling
the for-each/value-of, but... -->
      <xsl:call-template name="translate">
        <xsl:with-param name="word" select="'enter'"/>
      </xsl:call-template>
    </out>
  </xsl:template>

</xsl:stylesheet>
=== test.xsl ===

You can invoke this stylesheet from the command line as follows:

C:\x\xml-xalan\sheets\xdtm>java -classpath c:\x\xml-xalan\xalan.jar;c:
\x\xml-xerces\java\bin\xerces.jar;%classpath% org.apache.xalan.xslt.Process
-param lang 'spanish' -xsl c:\x\xml-xalan\sheets\test\test.xsl

And it will yield, in this case:

<out>entrar</out>

The keys are hashed, so lookup should be pretty quick, but the disadvantage
is that the calling method is somewhat verbose.  As I said, an extension
could be made to cut down on this verbosity.

(Notice that the command line did not take in an input XML... Xalan
automatically produces a dummy document for this case, so the "/" template
will be executed.)

I'm not stating that this is the right way to handle internationalization,
but I don't think it's a bad way to do things, and hopefully it will help
someone out.  There are a couple variations of this, like using a different
file for each language.

-scott