xslt本地化 [英] xslt localization

查看:73
本文介绍了xslt本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

然后跟随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>

我想本地化xslt文本:员工列表,员工姓名和姓名;联系方式

I would like to localize xslt text : List of Employee, Employee Name & Contact Details

如何本地化xslt文本?

How to localize the xslt text?

推荐答案

我可以看到三种方法来做到这一点,哪种方法最好(或者是否有其他方法可供选择)取决于何时以及如何需要最终的xml. :

I can see three ways to do this, which one is best (or if any of these is an alternative) depends on when and how you need the final xml:

以编程方式构造xsl

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

Build the xsl using for example XmlDocument - then you can use regular string resources to fill in the labels, and possibly make use of the culture settings of your application.

将翻译内容嵌入xsl

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

在转换中查找翻译

将翻译内容放入其自己的translation.xml的xml文档中:

Put the translations in an xml documents of its own translation.xml:

<strings>
    <string language="en" key="ContactDetails">Contact Details</string>
    <string language="sv" key="ContactDetails">Kontaktuppgifter</string>
    [...]
</strings>   

然后使用以下内容加载其内容:

Then load it's contents with:

<xsl:variable name="strings" select="document('translation.xml')/strings"/>

...并通过以下方式访问它们:

...and access them with:

<xsl:value-of select="$strings/string[@key='ContactDetails' and @language=$language]"/>

这篇关于xslt本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆