Re: multilanguage support easily or including xml data dynamically

Subject: Re: multilanguage support easily or including xml data dynamically
From: Mike Brown <mike@xxxxxxxx>
Date: Thu, 8 Jun 2000 01:12:15 -0600 (MDT)
> I have a report to do with multilanguage support. It's designed this 
> way:
> 
> XML-Number-Data + XSL --> HTML
> 
> Now, I would like to pass a parameter to maybe an ASP, maybe to 
> the XML directly (I appreciate advices!), to select a language, let's 
> say English :).
> Now this should happen:
> 
> 1.) open XML-Number-Data
> 2.) include chunk of language-data properly (e.g. ENGLISH.XML)
> 3.) label the data correctly via XSL and 
> 4.) create HTML.
> 
> Do I need a scripting language like JScript for this? I would like to 
> use as much pure XML/XSL as possible.

You can use pure XML/XSLT, although not by dynamically selecting from a
master file of all local language info, not dynamically including just the
language you need. A very brief example to demonstrate principles:

<?xml version="1.0"?>
<!-- this is XMLNumberData.xml -->
<XMLNumberData>
  <numberData desc="albedo">0.39</numberData>
  <numberData desc="pi">3.1415926</numberData>
</XMLNumberData>

<?xml version="1.0"?>
<!-- this is LanguageData.xml -->
<!-- forgive my completely made-up Spanish! -->
<phrases>
  <phrase key="albedo" xml:lang="en">The albedo of the earth is: </phrase>
  <phrase key="albedo" xml:lang="es">El albedo de la terra es: </phrase>
  <phrase key="pi" xml:lang="en">The value of pi is: </phrase>
  <phrase key="pi" xml:lang="es">El valor de pi es: </phrase>
</phrases>

<?xml version="1.0"?>
<!-- This is LangTest.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xml" version="1.0" indent="yes"/>
  <xsl:param name="LanguageSelected" select="'en'"/>
  <xsl:variable name="phrases" select="document('LanguageData.xml')/phrases"/>

  <xsl:template match="/">
    <Result>
      <xsl:apply-templates select="XMLNumberData/numberData"/>
    </Result>
  </xsl:template>

  <xsl:template match="numberData">
    <xsl:value-of select="concat('&#xA;',$phrases/phrase[@key=current()/@desc and lang($LanguageSelected)],.)"/>
    <!-- note instead of lang($foo) I could've said @xml:lang=$foo -->
  </xsl:template>
</xsl:stylesheet>


Select the desired language by passing a parameter to the stylesheet,
using the mechanism your XSL processor allows. Default will be 'en'.

Example with Saxon:
  saxon XMLNumberData.xml LangTest.xsl LanguageSelected=es

Output:

<?xml version="1.0" encoding="utf-8" ?>
<Result>
El albedo de la terra es: 0.39
El valor de pi es: 3.1415926</Result>


I would have tried to use <xsl:key/> and key() to make the phrase
selection more efficient, but I couldn't get it to work with the separate
LanguageData document.

   - Mike
____________________________________________________________________
Mike J. Brown, software engineer at         My XML/XSL resources:
webb.net in Denver, Colorado, USA           http://www.skew.org/xml/


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


Current Thread