2011-04-27 63 views
0

我使用PHP作为编程语言和表示逻辑(用于输出)我使用XSL。xsl翻译系统

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

从谷歌搜索我看到有两个选择:在PHP XSL

  1. 注册功能,但我不喜欢这个主意,因为我whant保持我的表示逻辑作为单独地
  2. 加载xml文件并将其翻译为xsl变量,但如果翻译字符串中有变量会怎么样?

也许还有其他一些选择如何翻译文本?什么是最好的方法?

谢谢

+0

@Tomas:通常你会用一个目录XML资源不同的翻译。我不明白_“翻译字符串中有变量”的要求? – 2011-04-27 14:54:33

+0

可以说我需要翻译输入1到10之间的数字,其中'1'和'10'是变量并且可以改变。在我的代码中,这行现在看起来像这样:“在”'之间输入数字。如果我从XML字典加载翻译,我是否必须处理那些可能会改变的部分? – Tomas 2011-04-27 15:07:10

+1

@Tomas:对于你应该使用的人口模式像http://stackoverflow.com/questions/3986408/how-to-fill-text-templates-using-xslt – 2011-04-27 15:35:38

回答

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

@Alejandro:非常感谢您的帮助!它帮了我很多 – Tomas 2011-05-02 08:06:06

+0

@Tomas:不客气! – 2011-05-02 13:15:38