使用外部 XML 文档本地化 XSLT 输出
我有一个 XSLT 样式表,可将 XML 数据转换为 XSL-FO(然后使用 Apache FOP 将其呈现为 PDF)。
现在,我需要将输出本地化为几种不同的语言。例如,XSLT 生成的表的列标题需要本地化。下面是 XSLT 的一个简化片段,生成一个带有列标题 “日期”、“名称” 和 “某事” 的表标题:
...
<fo:table>
<fo:table-header>
<fo:table-row>
<fo:table-cell>
<fo:block>Date</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>Name</fo:block>
</fo:table-cell>
<fo:table-cell>
</fo:block>Something</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-header>
</fo:table-body>
<xsl:apply-templates select="item"/>
</fo:table-body>
</fo:table>
...
我们已经使用 TMX 文件在系统的其他部分进行国际化,所以这似乎是一种自然的方式关于 它。下面是一个 TMX 文件的简化示例,其中包含两种语言的几个字符串:
<tmx version="1.4b">
<body>
<tu tuid="DATE">
<tuv xml:lang="en"><seg>Date</seg></tuv>
<tuv xml:lang="sv"><seg>Datum</seg></tuv>
</tu>
<tu tuid="NAME">
<tuv xml:lang="en"><seg>Name</seg></tuv>
<tuv xml:lang="sv"><seg>Namn</seg></tuv>
</tu>
</body>
</tmx>
所以基本上我想用对 TMX 文件中字符串的引用替换 XSLT 中的硬编码列标题(使用 tuid< /em> 属性),以便相同的 XSLT 可以产生不同语言的输出。
但我似乎找不到任何有关如何将 TMX 文件与 XSLT 结合使用的资源。示例或指向一些好的教程的指针将不胜感激。
I have an XSLT stylesheet that transforms the XML data into XSL-FO (which is then rendered as a PDF using Apache FOP).
Now, I would need to localize the output to a couple of different languages. For instance, column headers for the tables produced by the XSLT need to be localized. Here's a simplified snippet of the XSLT producing a table header with column captions "Date", "Name" and "Something" :
...
<fo:table>
<fo:table-header>
<fo:table-row>
<fo:table-cell>
<fo:block>Date</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>Name</fo:block>
</fo:table-cell>
<fo:table-cell>
</fo:block>Something</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-header>
</fo:table-body>
<xsl:apply-templates select="item"/>
</fo:table-body>
</fo:table>
...
We are already using TMX files for internationalization in other parts of the system, so that seems like a natural way to go about it. Here's a simplified example of a TMX file with a couple of strings in two languages:
<tmx version="1.4b">
<body>
<tu tuid="DATE">
<tuv xml:lang="en"><seg>Date</seg></tuv>
<tuv xml:lang="sv"><seg>Datum</seg></tuv>
</tu>
<tu tuid="NAME">
<tuv xml:lang="en"><seg>Name</seg></tuv>
<tuv xml:lang="sv"><seg>Namn</seg></tuv>
</tu>
</body>
</tmx>
So basically I'd like to replace the hard-coded column headers in the XSLT with references to the strings in the TMX file (using tuid attributes), so that the same XSLT could produce output in different languages.
But I can't seem to find any resources on how to use TMX files with XSLT. Examples or pointers to some good tutorials would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XSLT 中的文本可以通过从 XML 文档读取翻译后的字符串来本地化。数字也可以本地化。
XML 文档可以包含一种语言,每种语言都有一个 XML 文档,或者包含所有语言的单个 XML 文档。以下示例中的 XML 格式遵循 Microsoft .NET 资源 (.resx) 文件(每种语言一个文件)或所有语言的单个 TMX(翻译记忆库交换)文档。但是,只要用于读取文本的 XPath 一致,就可以使用任何格式。
这两个选项都使用 XPath '
document
' 函数来读取包含已翻译字符串的 XML。为 XSLT 中使用的每个字符串定义参数。使用参数而不是变量允许在转换 XSLT 时覆盖值。使用xsl:value-of
显示翻译后的文本。处理转换时,传递语言代码(例如“fr”)和所需语言的资源 XML 文档的 URL。请参阅我的文章“如何本地化 XSLT”,获取完整的功能示例,网址为 http://www.codeproject.com/Articles/338731/LocalizeXSLT。
Text in an XSLT may be localized by reading the translated strings from an XML document. Numbers may also be localized.
The XML document may contain either one language with one XML document for each language, or alternatively, a single XML document with all languages. The XML formats in the following example follows Microsoft .NET resource (.resx) files (one file per language) or a single TMX (translation memory exchange) document with all languages. Any format, however, may be used as long as the XPath used to read the text is consistent.
Both options use the XPath '
document
' function to read the XML with the translated strings. Define parameters for each string used in the XSLT. Using parameters rather than variables allows the values to be overridden when the XSLT is transformed. Usexsl:value-of
to display the translated text. When the transform is processed, pass the language code, for example, 'fr', and the URL to the resource XML document for the desired language.See my article on "How to localize XSLT" for a complete, functional sample at http://www.codeproject.com/Articles/338731/LocalizeXSLT.