使用xsl创建本地化内容

时间:2012-04-26 03:49:59

标签: xml xslt localization

给定具有以下结构的输入文件:

<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>

我可以使用以下结构读入样式表的模板文件:

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

如何让xsl输出模板的本地化版本...例如,如果我将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>

感谢。

2 个答案:

答案 0 :(得分:0)

这是一个应该完成工作的快速编写的样式表:

<?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>

$ 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>

$ 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>

答案 1 :(得分:0)

这是一种更有效的基于密钥的查找

<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>

如果提供的翻译文件位于c:/temp/delete/lookup2.xml,并且在提供的XML文档上应用了转换

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

产生了想要的正确结果

<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解决方案:

<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>