如何解决xsl翻译系统
| 我正在使用PHP作为一种编程语言,并且为了表示逻辑(用于输出),我正在使用XSL。 现在,我需要为我的项目创建翻译系统。在xslt中进行翻译的最佳方法是什么? 从Google搜索中,我看到有两种选择: 在PHP XSL中注册函数,但是我不喜欢这个主意,因为我希望将表示逻辑尽可能地分开 将翻译后的XML文件加载到xsl变量中,但是如果翻译字符串中有变量怎么办? 也许还有其他一些如何翻译文本的选项?什么是最好的方法? 谢谢解决方法
此样式表:
<xsl:stylesheet version=\"1.0\"
xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"
xmlns:html=\"http://www.w3.org/1999/xhtml\"
exclude-result-prefixes=\"html\">
<xsl:strip-space elements=\"*\"/>
<xsl:preserve-space elements=\"translation html:*\"/>
<xsl:key name=\"kResourceById\" match=\"resource\" use=\"@id\"/>
<xsl:variable name=\"vConfig\" select=\"/config\"/>
<xsl:variable name=\"vCatalog\" select=\"document(\'catalog.xml\')\"/>
<xsl:template match=\"/\">
<xsl:apply-templates select=\"document(\'layout.xml\')/node()\"/>
</xsl:template>
<xsl:template match=\"node()|@*\">
<xsl:copy>
<xsl:apply-templates select=\"node()|@*\"/>
</xsl:copy>
</xsl:template>
<xsl:template match=\"html:*[@id]\">
<xsl:variable name=\"vCurrent\" select=\".\"/>
<xsl:copy>
<xsl:apply-templates select=\"@*\"/>
<xsl:for-each select=\"$vCatalog\">
<xsl:variable name=\"vResource\"
select=\"key(\'kResourceById\',$vCurrent/@id)\"/>
<xsl:apply-templates
select=\"($vResource/translation[@xml:lang=$vConfig/lang]
|$vCurrent[not($vResource)])/node()\"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match=\"*[not(self::html:*)]\">
<xsl:apply-templates
select=\"$vConfig/*[name()=name(current())]/node()\"/>
</xsl:template>
</xsl:stylesheet>
使用此输入:
<config>
<lang>en</lang>
<sn>1</sn>
<en>10</en>
</config>
和catalog.xml
:
<catalog>
<resource id=\"str1\">
<translation xml:lang=\"en\"
>enter number between <sn/> and <en/>.</translation>
<translation xml:lang=\"lt\"
>iveskite skaiciu tarp <sn/> ir <en/>.</translation>
</resource>
</catalog>
和layout.xml
:
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<body>
<form>
<label id=\"str1\"/>
<input id=\"val1\" type=\"input\"/>
<input type=\"submit\"/>
</form>
</body>
</html>
输出:
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<body>
<form>
<label id=\"str1\">enter number between 1 and 10.</label>
<input id=\"val1\" type=\"input\"></input>
<input type=\"submit\"></input>
</form>
</body>
</html>
使用此输入:
<config>
<lang>lt</lang>
<sn>20</sn>
<en>30</en>
</config>
输出:
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<body>
<form>
<label id=\"str1\">iveskite skaiciu tarp 20 ir 30.</label>
<input id=\"val1\" type=\"input\"></input>
<input type=\"submit\"></input>
</form>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。