如何使用XSL和XML执行I18N [英] How to do I18N with xsl and xml

查看:89
本文介绍了如何使用XSL和XML执行I18N的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用xml/xsl用不同的语言创建一个页面.我只想拥有一个xml和一个xsl.在我的页面网址中,我有一个参数pLanguage,我可以用来查看是否选择了英语或荷兰语.

I am trying to do a page in different languages with xml/xsl. I want to have only one xml and one xsl. On my page Url I have a parameter pLanguage that I think I can use to look if I have selected English or Dutch.

我尝试使用此代码,但不知道如何将其组合在一起:

I tried with this code but I don’t know how I put it together:

首先,我将必须翻译的所有单词制作为变量:

First I make variables of all the words who has to been translated like this:

<xsl:variable name="lang.pageTitle" select="'This is the title in English'"/>

要在模板中获取pageTitle,我现在可以使用

To get the pageTitle in the template I now can use

<xsl:value-of select="$lang.pageTitle"/>

我想通过使用if-else语句来替换上面的第一行代码,以测试我选择的语言是EN还是NL这样的语言:

I thought to replace the first line of code above by using a if-else statement to test if my choosen language is EN or NL like this:

<xsl:choose>
      <xsl:when test="$choosenLanguage &#61; ‘NL’">
        <xsl:variable name="lang.pageTitle" select="Titel in het nederlands'"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="lang.pageTitle" select="'This is the title in English'"/>
      </xsl:otherwise>
    </xsl:choose>

但是我得到了错误: java.lang.IllegalArgumentException:无法解析参数编号$ lang.opdracht

But I get the error: java.lang.IllegalArgumentException: can't parse argument number $lang.opdracht

推荐答案

下面是一个完整的示例,介绍了如何以通用方式完成此操作:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" exclude-result-prefixes="my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pLang" select="'nl'"/>

 <my:texts>
  <pageTitle lang="en">This is the title in English</pageTitle>
  <pageTitle lang="nl">Titel in het nederlands</pageTitle>
 </my:texts>

 <xsl:variable name="vTexts" select="document('')/*/my:texts"/>

 <xsl:template match="/">
     <html>
      <title>
        <xsl:value-of select="$vTexts/pageTitle[@lang = $pLang]"/>
      </title>
     </html>
 </xsl:template>
</xsl:stylesheet>

将此转换应用于任何XML文档(未使用)时,将生成所需的正确结果(标题是根据全局/外部参数$pLang生成的):

When this transformation is applied on any XML document (not used), the wanted, correct result (the title is generated in accordance with the global/external parameter $pLang) is produced:

<html>
   <title>Titel in het nederlands</title>
</html>

记录:

建议所有字符串都保存在与XSLT样式表文件不同的XML文档中.这允许在不更改XSLT代码的情况下修改/添加/删除字符串.

It is recommended that all strings be maintained in an XML document that is separate from the XSLT stylesheet file(s). This allows the strings to be modified/added/deleted without changing the XSLT code.

要从另一个XML文档访问字符串,代码几乎保持不变,唯一的区别是,现在document()函数的参数是字符串XML文档的URI.

To access the strings from another XML document the code remains almost the same, the only difference is that the argument to the document() function now is the URI to the strings XML document.

这篇关于如何使用XSL和XML执行I18N的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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