xsl翻译系统

时间:2011-04-27 14:36:48

标签: php xslt translation

我使用PHP作为编程语言和表示逻辑(对于我的输出)我正在使用XSL。

现在我需要为我的项目创建翻译系统。在xslt中进行翻译的最佳方式是什么?

从谷歌搜索

我看到有两种选择:

  1. 在PHP XSL中注册函数,但我不喜欢这个想法,因为我希望尽可能将我的表示逻辑保持独立
  2. 将带有翻译的xml文件加载到xsl变量,但是如果翻译字符串中有变量会怎样?
  3. 也许还有一些其他选项如何翻译文字?什么是最好的方式?

    谢谢

1 个答案:

答案 0 :(得分:2)

此样式表:

<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>
相关问题