This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

localization techniques and code review


A few notes about my request:

1. I'm a beginner and have just started to learn XSL
2. I'm using Cocoon 1.8 for XSLT on Win 2000 professional.
3. Boy is there a lot to learn here....
4. I have looked at the archives and still feel the need to post so humor
me, please

The objectives are:
1. To learn more about XSL
2. To get some advice on how to handle localization issues
3. What is xml:lang and should I have been using that instead?

I looked at the archives and came across a partially broken example, which I
adapted to my needs and I got it work. However, I am interested in finding
out if I should be using a better methodology (since there is more than one
way to do everything in XSL. (Hey! that's just like perl).

I want to iterate over the fields in this file...

<?xml version="1.0"?>
<!-- test.xml -->
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<?cocoon-process type="xslt"?>
<page>
    <field>_FIRST_NAME</field>
    <field>_OCCUPATION</field>
</page>

and look up their translations in this file...

<?xml version="1.0"?>
<!--  localization.xml -->
<localization>
    <word name="_FIRST_NAME">
        <translation lang="es">Nombre</translation>
        <translation lang="en">First Name</translation>
        <translation lang="it">Nome</translation>
    </word>
     <word name="_OCCUPATION">
                <translation lang="es">Ocupacion</translation>
                <translation lang="en">Occupation</translation>
                <translation lang="it">Occupazione</translation>
     </word>
</localization>

this is how I did it. The variable selectLang defaults to 'en' but can be
supplied as a URL parameter, thereby giving the translation in another
language (no error checking is being done, if the translation doesn't
exist).

<?xml version="1.0"?>
<!-- test.xsl -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <!-- set default language to 'en' if not supplied from URL paramter -->
  <xsl:param name="selectLang" select="'en'" />
  <xsl:variable name="trans" select="document('localization.xml')" />
    <!-- set a key called translate     -->
    <!-- which indexes the name property of ancestor word's -->
    <!-- concatenated with a hyphen and the lang property -->
    <!-- of all translation nodes    -->
    <xsl:key name="translate" match="translation"
use="concat(ancestor::word/@name,'-',@lang)" />
    <xsl:template match="page">
      <html><body>
        <xsl:value-of select="$selectLang" />
        <hr />
        <xsl:for-each select="//field">
          <xsl:param name="field" select="." />
          <xsl:value-of select="$field" />
          <h1>
            <xsl:for-each select="document('localization.xml')">
              <xsl:value-of
select="key('translate',concat($field,'-',$selectLang))" />
            </xsl:for-each>
          </h1>
        </xsl:for-each>
      </body></html>
    </xsl:template>
</xsl:stylesheet>

Expected output (with URL test.xml):
<html><body>en<hr>_FIRST_NAME<h1>First
Name</h1>_OCCUPATION<h1>Occupation</h1></body></html>

Expected output (with URL test.xml?selectLang=es)
<html><body>es<hr>_FIRST_NAME<h1>Nombre</h1>_OCCUPATION<h1>Ocupacion</h1></b
ody></html>

Questions:
1. Am I on the right track? What about the use of nested
<xsl:for-each select="document('localization.xml')">
2. What if I need to re-print the table again? Could I have somehow stored
the results of the document() call to be able to reuse it at a later stage?

Thanks for your help,
--
Haroon Rafique



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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]