Re: [xsl] Localising Submit/Clear buttons in html

Subject: Re: [xsl] Localising Submit/Clear buttons in html
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 12 Nov 2001 17:46:17 +0000
Hi Ian,

> I've got it working:

What a shame; I wanted the roses and chocolate :( Perhaps I can still
get them if I add value to the solution you've got...

You only *need* one template - you can test whether the current
element is an input element within the template with self::input. I'd
only use one template because the paths for getting hold of the
localisation information are pretty complicated and it would make for
easier maintenance if you didn't have to repeat them all the time. To
that end, I'd also add a variable to hold the value retrieved from the
document so that the path to retrieve the relevant entry is only used
once:

<xsl:template match="*[@nlsid]" priority="5">
  <xsl:copy>
    ...
    <xsl:variable name="myKey" select="@nlsid" />
    <xsl:variable name="myEntry"
      select="document($doc-file)/locale/*[name() = $myKey]" />
    <xsl:choose>
      <xsl:when test="$myEntry">
        <xsl:choose>
          <xsl:when test="self::input">
            <xsl:attribute name="value">
              <xsl:value-of select="$myEntry/*[name() =
                                               $currentLocale]" />
            </xsl:attribute>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$myEntry/*[name() =
                                             $currentLocale]" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="." />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:copy>
</xsl:template>

I'd also make a couple of changes to neaten up the code. Rather than:

  <xsl:for-each select="@*">
    <xsl:if test="name() != 'nlsid'">
      <xsl:copy-of select="." />
    </xsl:if>
  </xsl:for-each>

I'd use:
  
    <xsl:copy-of select="@*[name() != 'nlsid']" />

And rather than:
    
    <xsl:apply-templates select="./*" />

I'd use:

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

[Note './' is always redundant at the beginning of a location path
(unless it starts './/') because location paths are always evaluated
relative to the context node.]

You might want to also look at using keys to retrieve the information
from the dictionary file, especially if that file is big. You need
something like the following to index all the elements under the
locale element by their name:

<xsl:key name="localisation" match="/locale/*" use="name()" />

and then you need to use an xsl:for-each to shift focus onto the
$doc-file document in order to search for the key within it:

<xsl:template match="*[@nlsid]" priority="5">
  <xsl:copy>
    ...
    <xsl:variable name="myKey" select="@nlsid" />
    <xsl:variable name="myValue">
      <xsl:for-each select="document($doc-file)">
        <xsl:value-of select="key('localisation', $myKey)
                                /*[name() = $currentLocale]" />
      </xsl:for-each>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="string($myValue)">
        <xsl:choose>
          <xsl:when test="self::input">
            <xsl:attribute name="value">
              <xsl:value-of select="$myValue" />
            </xsl:attribute>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$myValue" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="." />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:copy>
</xsl:template>

Another possibility, if the dictionary files are really large, is to
split them into separate files based on the locale that they contain,
and use a naming scheme for the documents that includes the locale
name. That way you can retrieve all the locale-specific information
for language X at once, and I'd guess it would be more efficient
because the dictionary file is smaller to search (though that rests on
the assumption that you're only using one locale in a particular
document) and probably more manageable because translators could
concentrate on a single file at a time.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread