2011-04-27 61 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