Using xsl to create localized content

2019-07-26 02:19发布

问题:

Given an input file with the following structure:

<resources>
    <text name="property.to.match">
        <en_US>The American translation</en_US>
        <en_GB>The British translation</en_GB>
        <en>The language localized, but non locale based generic translation</en>
    </text>
    <text name="other.property.to.match">
        <en>The other language localized, but non locale based generic translation</en>
    </text
</resources>

And a template file that I can read into the stylesheet with the following structure:

<html>
    <div>Lot's of html</div>
    <div>[property.to.match]</div>
    <div>[other.property.to.match]</div>
</html>

How can I get an xsl to output a localized version of the template... So for example, if I pass en_US as a parameter to the stylesheet I would like the following output:

<html>
    <div>Lot's of html</div>
    <div>The American translation</div>
    <div>The other language localized, but non locale based generic translation</div>
</html>

Thanks.

回答1:

This is a more efficient, key-based lookup:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pLookupPath" select="'file:///c:/temp/delete/lookup2.xml'"/>
 <xsl:param name="pLang" select="'en_GB'"/>

 <xsl:key name="kLookup" match="text/*"
  use="concat(../@name, '+', name())"/>

 <xsl:variable name="vDict" select="document($pLookupPath)"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match=
  "div/text()
     [starts-with(., '[')
    and substring(., string-length(),  1) = ']'
      ]">

  <xsl:variable name="vTextName" select="substring(., 2, string-length() -2)"/>

  <xsl:for-each select="$vDict">
   <xsl:value-of select=
    "(key('kLookup', concat($vTextName, '+', $pLang))
    |
      key('kLookup', concat($vTextName, '+', substring-before($pLang, '_')))
      )[1]
    "/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

If the provided translation file resides at c:/temp/delete/lookup2.xml and when the transformation is applied on the provided XML document:

<html>
    <div>Lot's of html</div>
    <div>[property.to.match]</div>
    <div>[other.property.to.match]</div>
</html>

the wanted, correct result is produced:

<html>
   <div>Lot's of html</div>
   <div>The British translation</div>
   <div>The other language localized, but non locale based generic translation</div>
</html>

II. XSLT 2.0 Solution:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pLookupPath" select="'file:///c:/temp/delete/lookup2.xml'"/>
 <xsl:param name="pLang" select="'en_GB'"/>

 <xsl:key name="kLookup" match="text/*"
  use="concat(../@name, '+', name())"/>

 <xsl:variable name="vDict" select="document($pLookupPath)"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match=
  "div/text()
     [starts-with(., '[') and ends-with(., ']')]">

  <xsl:variable name="vTextName" select="substring(., 2, string-length() -2)"/>

   <xsl:sequence select=
    "(key('kLookup', concat($vTextName, '+', $pLang), $vDict)
    ,
      key('kLookup', concat($vTextName, '+', substring-before($pLang, '_')),
          $vDict)
      )[1]
    "/>
 </xsl:template>
</xsl:stylesheet>


回答2:

Here is a quickly written stylesheet that should do the job:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="lang">en_US</xsl:param>
  <xsl:variable name="text" select="document('resources.xml')//text"/>

  <xsl:template match="html">
    <html>
      <xsl:apply-templates/>
    </html>
  </xsl:template>

  <xsl:template match="div"> 
    <xsl:variable name="id" select="substring(., 2 , string-length(.)-2)"/>
    <xsl:variable name="repl">
      <xsl:choose>
        <xsl:when test="$text[@name=$id]/*[name()=$lang]">
          <xsl:value-of select="$text[@name=$id]/*[name()=$lang]"/>          
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$text[@name=$id]/*[name()=substring-before($lang, '_')]"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>    
    <div>
      <xsl:choose>
        <xsl:when test="substring(.,1,1)='[' and substring(.,string-length(.))=']' and $repl!=''">
          <xsl:value-of select="$repl"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </div>
  </xsl:template>
</xsl:stylesheet>

Output for $lang='en_US':

<html>
  <div>Lot's of html</div>
  <div>The American translation</div>
  <div>The other language localized, but non locale based generic translation</div>
</html>

Output for $lang='en_GB':

<html>
  <div>Lot's of html</div>
  <div>The British translation</div>
  <div>The other language localized, but non locale based generic translation</div>
</html>