Xml 使用xsl创建本地化内容第2部分
与此稍有不同的要求 字典保持相同的格式,但会增长:Xml 使用xsl创建本地化内容第2部分,xml,xslt,localization,Xml,Xslt,Localization,与此稍有不同的要求 字典保持相同的格式,但会增长: <resources> <text name="property.to.match"> <en_US>The American translation</en_US> <en_GB>The British translation</en_GB> <en>The language localized, but
<resources>
<text name="property.to.match">
<en_US>The American translation</en_US>
<en_GB>The British translation</en_GB>
<en>The language localized, but non locale based generic translation</en>
</text>
<text name="other.property.to.match">
<en>The other language localized, but non locale based generic translation</en>
</text
<text name="another.property.to.match">
<en>Second generic property</en>
</text>
<text name="a.label.to.match">
<en>Generic Checkbox label</en>
</text>
<text name="some.text.to.translate">
<en>This text follows a static bit of text</en>
</text>
</resources>
美国翻译
英译
语言本地化,但非基于区域设置的泛型翻译
另一种语言是本地化的,但不是基于区域设置的泛型翻译
此XSLT 1.0转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pLookupPath" select="'file:///c:/temp/delete/lookup2.xml'"/>
<xsl:param name="pLang" select="'en_GB'"/>
<xsl:key name="kLookup" match="text/*"
use="concat(../@name, '+', name())"/>
<xsl:variable name="vDict" select="document($pLookupPath)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template name="replaceWithLookup" match=
"text()[contains(., '[') and contains(., ']')]">
<xsl:param name="pText" select="."/>
<xsl:if test="string-length($pText)">
<xsl:value-of select=
"substring-before(concat($pText, '['), '[')"/>
<xsl:variable name="vToken" select=
"substring-before(substring-after($pText, '['), ']')"/>
<xsl:variable name="vReplacement">
<xsl:for-each select="$vDict">
<xsl:value-of select=
"(key('kLookup', concat($vToken, '+', $pLang))
|
key('kLookup', concat($vToken, '+', substring-before($pLang, '_')))
)[1]"/>
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<xsl:when test="$vReplacement">
<xsl:value-of select="$vReplacement"/>
</xsl:when>
<xsl:when test="$vToken">
<xsl:value-of select="concat('[', $vToken, ']')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select=
"substring-after
($pText,
substring-before(concat($pText, '['), '[')
)"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="$vToken">
<xsl:call-template name="replaceWithLookup">
<xsl:with-param name="pText" select=
"substring-after($pText, concat('[', $vToken, ']'))"/>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
应用于以下XML文档时(基于提供的文档,但变得更加困难和具有挑战性——最后的文本节点包含两个替换目标):
大量的html
[属性.to.match]
[其他.property.to.match]
[另一个.property.to.match]
[a.标签到.匹配]
下面是一些不应该翻译的文本
由[some.text.to.translate]
XXX[其他.属性.到.匹配]
生成所需的正确结果:
<html>
<div>Lot's of html</div>
<div>The British translation</div>
<div>
<h1>The other language localized, but non locale based generic translation</h1>
<p><img src="x.jpg">Second generic property
</p><input type="checkbox">Generic Checkbox label
<p>Some text that shouldn't be translated followed
by This text follows a static bit of text
XXX The other language localized, but non locale based generic translation
</p>
</div>
</html>
大量的html
英译
另一种语言是本地化的,但不是基于区域设置的泛型翻译
第二类属性
通用复选框标签
下面是一些不应该翻译的文本
在此文本后面是一个静态的文本位
XXX其他语言本地化,但非基于区域设置的通用翻译
此XSLT 1.0转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pLookupPath" select="'file:///c:/temp/delete/lookup2.xml'"/>
<xsl:param name="pLang" select="'en_GB'"/>
<xsl:key name="kLookup" match="text/*"
use="concat(../@name, '+', name())"/>
<xsl:variable name="vDict" select="document($pLookupPath)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template name="replaceWithLookup" match=
"text()[contains(., '[') and contains(., ']')]">
<xsl:param name="pText" select="."/>
<xsl:if test="string-length($pText)">
<xsl:value-of select=
"substring-before(concat($pText, '['), '[')"/>
<xsl:variable name="vToken" select=
"substring-before(substring-after($pText, '['), ']')"/>
<xsl:variable name="vReplacement">
<xsl:for-each select="$vDict">
<xsl:value-of select=
"(key('kLookup', concat($vToken, '+', $pLang))
|
key('kLookup', concat($vToken, '+', substring-before($pLang, '_')))
)[1]"/>
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<xsl:when test="$vReplacement">
<xsl:value-of select="$vReplacement"/>
</xsl:when>
<xsl:when test="$vToken">
<xsl:value-of select="concat('[', $vToken, ']')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select=
"substring-after
($pText,
substring-before(concat($pText, '['), '[')
)"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="$vToken">
<xsl:call-template name="replaceWithLookup">
<xsl:with-param name="pText" select=
"substring-after($pText, concat('[', $vToken, ']'))"/>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
应用于以下XML文档时(基于提供的文档,但变得更加困难和具有挑战性——最后的文本节点包含两个替换目标):
大量的html
[属性.to.match]
[其他.property.to.match]
[另一个.property.to.match]
[a.标签到.匹配]
下面是一些不应该翻译的文本
由[some.text.to.translate]
XXX[其他.属性.到.匹配]
生成所需的正确结果:
<html>
<div>Lot's of html</div>
<div>The British translation</div>
<div>
<h1>The other language localized, but non locale based generic translation</h1>
<p><img src="x.jpg">Second generic property
</p><input type="checkbox">Generic Checkbox label
<p>Some text that shouldn't be translated followed
by This text follows a static bit of text
XXX The other language localized, but non locale based generic translation
</p>
</div>
</html>
大量的html
英译
另一种语言是本地化的,但不是基于区域设置的泛型翻译
第二类属性
通用复选框标签
下面是一些不应该翻译的文本
在此文本后面是一个静态的文本位
XXX其他语言本地化,但非基于区域设置的通用翻译