xslt本地化

Ani*_*ani 9 c# xml xslt localization internationalization

我有以下xml文件: -

<?xml version="1.0" encoding="UTF-8"?> 
<directory> 
   <employee> 
      <name>Joe Smith</name> 
      <phone>4-0192</phone> 
   </employee> 
   <employee> 
      <name>Sally Jones</name> 
      <phone>4-2831</phone> 
   </employee> 
</directory>
Run Code Online (Sandbox Code Playgroud)

并关注xslt: -

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html"/>
   <xsl:template match="directory">
      <div>List of Employee<xsl:value-of select="@directory"/>
      </div>
      <br/>
      <table>
        <tr>
          <td>Employee Name</td>
          <td>Contact Details</td>
        </tr>
        <xsl:apply-templates select="employee"></xsl:apply-templates>
      </table>

    </xsl:template>

    <xsl:template match="employee">
      <tr>
        <td>
           <xsl:value-of select="@name"/>
        </td>
        <td>
           <xsl:value-of select="@phone"/>
        </td>
      </tr>
    </xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

我想本地化xslt文本:员工列表,员工姓名和联系人详细信息

如何本地化xslt文本?

And*_*ahl 6

我可以看到三种方法,其中一种最好(或者如果其中任何一种是替代方案)取决于你何时以及如何需要最终的xml:

以编程方式构造xsl

使用例如XmlDocument构建xsl - 然后您可以使用常规字符串资源来填充标签,并可能使用应用程序的区域性设置.

在xsl中嵌入翻译

使用a <xsl:param>告诉转换使用哪种语言,然后<xsl:choose>在每个字符串中放置一个:

<xsl:choose>
    <xsl:when test="$language='en'">Contact Details</xsl:when>
    <xsl:when test="$language='sv'">Kontaktuppgifter</xsl:when>
    <xsl:otherwise>Unknown language <xsl:value-of select="$language"/></xsl:otherwise>
</xsl:choose>
Run Code Online (Sandbox Code Playgroud)

查找翻译作为转换的一部分

将翻译放在自己的xml文档中translation.xml:

<strings>
    <string language="en" key="ContactDetails">Contact Details</string>
    <string language="sv" key="ContactDetails">Kontaktuppgifter</string>
    [...]
</strings>   
Run Code Online (Sandbox Code Playgroud)

然后加载它的内容:

<xsl:variable name="strings" select="document('translation.xml')/strings"/>
Run Code Online (Sandbox Code Playgroud)

...并访问它们:

<xsl:value-of select="$strings/string[@key='ContactDetails' and @language=$language]"/>
Run Code Online (Sandbox Code Playgroud)

  • 我会选择类似的东西,但是用<xsl:variable name ="strings"select ="document($ translation)/ strings"/>其中$ translation是保存所选语言翻译的文件; 那么你不需要谓词的语言部分来选择一个字符串.另外,使用键从翻译文件中选择所需的字符串. (3认同)