2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3 xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0"
4 exclude-result-prefixes="l"
7 <!-- ********************************************************************
8 $Id: l10n.xsl 8904 2010-09-11 01:56:22Z kosek $
9 ********************************************************************
11 This file is part of the XSL DocBook Stylesheet distribution.
12 See ../README or http://docbook.sf.net/release/xsl/current/ for
13 copyright and other information.
15 This file contains localization templates (for internationalization)
16 ******************************************************************** -->
18 <xsl:param name="l10n.xml" select="document('../common/l10n.xml')"/>
19 <xsl:param name="local.l10n.xml" select="document('')"/>
20 <xsl:param name="empty.local.l10n.xml" select="not($local.l10n.xml//l:l10n)"/>
22 <xsl:key name="l10n-lang" match="l:l10n" use="@language"/>
23 <xsl:key name="l10n-gentext" match="l:l10n/l:gentext" use="@key"/>
24 <xsl:key name="l10n-dingbat" match="l:l10n/l:dingbat" use="@key"/>
25 <xsl:key name="l10n-context" match="l:l10n/l:context" use="@name"/>
26 <xsl:key name="l10n-template" match="l:l10n/l:context/l:template[not(@style)]" use="concat(../@name, '#', @name)"/>
27 <xsl:key name="l10n-template-style" match="l:l10n/l:context/l:template[@style]" use="concat(../@name, '#', @name, '#', @style)"/>
29 <xsl:template name="l10n.language">
30 <xsl:param name="target" select="."/>
31 <xsl:param name="xref-context" select="false()"/>
33 <xsl:variable name="mc-language">
35 <xsl:when test="$l10n.gentext.language != ''">
36 <xsl:value-of select="$l10n.gentext.language"/>
39 <xsl:when test="$xref-context or $l10n.gentext.use.xref.language != 0">
40 <!-- can't do this one step: attributes are unordered! -->
41 <xsl:variable name="lang-scope"
42 select="$target/ancestor-or-self::*
43 [@lang or @xml:lang][1]"/>
44 <xsl:variable name="lang-attr"
45 select="($lang-scope/@lang | $lang-scope/@xml:lang)[1]"/>
47 <xsl:when test="string($lang-attr) = ''">
48 <xsl:value-of select="$l10n.gentext.default.language"/>
51 <xsl:value-of select="$lang-attr"/>
57 <!-- can't do this one step: attributes are unordered! -->
58 <xsl:variable name="lang-scope"
59 select="$target/ancestor-or-self::*
60 [@lang or @xml:lang][1]"/>
61 <xsl:variable name="lang-attr"
62 select="($lang-scope/@lang | $lang-scope/@xml:lang)[1]"/>
65 <xsl:when test="string($lang-attr) = ''">
66 <xsl:value-of select="$l10n.gentext.default.language"/>
69 <xsl:value-of select="$lang-attr"/>
76 <xsl:variable name="language" select="translate($mc-language,
77 'ABCDEFGHIJKLMNOPQRSTUVWXYZ-',
78 'abcdefghijklmnopqrstuvwxyz_')"/>
80 <xsl:for-each select="$l10n.xml"> <!-- We need to change context in order to get key work -->
82 <xsl:when test="key('l10n-lang', $language)">
83 <xsl:value-of select="$language"/>
85 <!-- try just the lang code without country -->
86 <xsl:when test="key('l10n-lang', substring-before($language,'_'))">
87 <xsl:value-of select="substring-before($language,'_')"/>
89 <!-- or use the default -->
92 <xsl:text>No localization exists for "</xsl:text>
93 <xsl:value-of select="$language"/>
94 <xsl:text>" or "</xsl:text>
95 <xsl:value-of select="substring-before($language,'_')"/>
96 <xsl:text>". Using default "</xsl:text>
97 <xsl:value-of select="$l10n.gentext.default.language"/>
98 <xsl:text>".</xsl:text>
100 <xsl:value-of select="$l10n.gentext.default.language"/>
106 <xsl:template name="l10n.language.name">
107 <xsl:param name="lang">
108 <xsl:call-template name="l10n.language"/>
111 <xsl:for-each select="$l10n.xml">
113 select="document(key('l10n-lang', $lang)/@href)/l:l10n/@english-language-name"/>
117 <xsl:template name="language.attribute">
118 <xsl:param name="node" select="."/>
120 <xsl:variable name="language">
122 <xsl:when test="$l10n.gentext.language != ''">
123 <xsl:value-of select="$l10n.gentext.language"/>
127 <!-- can't do this one step: attributes are unordered! -->
128 <xsl:variable name="lang-scope"
129 select="$node/ancestor-or-self::*
130 [@lang or @xml:lang][1]"/>
131 <xsl:variable name="lang-attr"
132 select="($lang-scope/@lang | $lang-scope/@xml:lang)[1]"/>
135 <xsl:when test="string($lang-attr) = ''">
136 <xsl:value-of select="$l10n.gentext.default.language"/>
139 <xsl:value-of select="$lang-attr"/>
146 <xsl:if test="$language != ''">
147 <xsl:attribute name="lang">
149 <xsl:when test="$l10n.lang.value.rfc.compliant != 0">
150 <xsl:value-of select="translate($language, '_', '-')"/>
153 <xsl:value-of select="$language"/>
159 <!-- FIXME: This is sort of hack, but it was the easiest way to add at least partial support for dir attribute -->
160 <xsl:copy-of select="ancestor-or-self::*[@dir][1]/@dir"/>
163 <!-- Duplication of language.attribute template to allow for xml:lang attribute
164 creation for XHTML 1.1 and epub target -->
165 <xsl:template name="xml.language.attribute">
166 <xsl:param name="node" select="."/>
168 <xsl:variable name="language">
170 <xsl:when test="$l10n.gentext.language != ''">
171 <xsl:value-of select="$l10n.gentext.language"/>
175 <!-- can't do this one step: attributes are unordered! -->
176 <xsl:variable name="lang-scope"
177 select="$node/ancestor-or-self::*
178 [@lang or @xml:lang][1]"/>
179 <xsl:variable name="lang-attr"
180 select="($lang-scope/@lang | $lang-scope/@xml:lang)[1]"/>
183 <xsl:when test="string($lang-attr) = ''">
184 <xsl:value-of select="$l10n.gentext.default.language"/>
187 <xsl:value-of select="$lang-attr"/>
194 <xsl:if test="$language != ''">
195 <xsl:attribute name="xml:lang">
197 <xsl:when test="$l10n.lang.value.rfc.compliant != 0">
198 <xsl:value-of select="translate($language, '_', '-')"/>
201 <xsl:value-of select="$language"/>
207 <!-- FIXME: This is sort of hack, but it was the easiest way to add at least partial support for dir attribute -->
208 <xsl:copy-of select="ancestor-or-self::*[@dir][1]/@dir"/>
211 <xsl:template name="gentext">
212 <xsl:param name="key" select="local-name(.)"/>
213 <xsl:param name="lang">
214 <xsl:call-template name="l10n.language"/>
217 <xsl:for-each select="$l10n.xml"> <!-- We need to switch context in order to make key() work -->
218 <xsl:for-each select="document(key('l10n-lang', $lang)/@href)">
219 <xsl:variable name="local.l10n.gentext"
220 select="($local.l10n.xml//l:i18n/l:l10n[@language=$lang]/l:gentext[@key=$key])[1]"/>
222 <xsl:variable name="l10n.gentext"
223 select="key('l10n-gentext', $key)[1]"/>
226 <xsl:when test="$local.l10n.gentext">
227 <xsl:value-of select="$local.l10n.gentext/@text"/>
229 <xsl:when test="$l10n.gentext">
230 <xsl:value-of select="$l10n.gentext/@text"/>
234 <xsl:text>No "</xsl:text>
235 <xsl:value-of select="$lang"/>
236 <xsl:text>" localization of "</xsl:text>
237 <xsl:value-of select="$key"/>
238 <xsl:text>" exists</xsl:text>
240 <xsl:when test="$lang = 'en'">
241 <xsl:text>.</xsl:text>
244 <xsl:text>; using "en".</xsl:text>
249 <xsl:for-each select="$l10n.xml"> <!-- We need to switch context in order to make key() work -->
250 <xsl:for-each select="document(key('l10n-lang', 'en')/@href)">
251 <xsl:value-of select="key('l10n-gentext', $key)[1]/@text"/>
260 <xsl:template name="gentext.element.name">
261 <xsl:param name="element.name" select="local-name(.)"/>
262 <xsl:param name="lang">
263 <xsl:call-template name="l10n.language"/>
266 <xsl:call-template name="gentext">
267 <xsl:with-param name="key" select="$element.name"/>
268 <xsl:with-param name="lang" select="$lang"/>
272 <xsl:template name="gentext.space">
273 <xsl:text> </xsl:text>
276 <xsl:template name="gentext.edited.by">
277 <xsl:call-template name="gentext">
278 <xsl:with-param name="key" select="'Editedby'"/>
282 <xsl:template name="gentext.by">
283 <xsl:call-template name="gentext">
284 <xsl:with-param name="key" select="'by'"/>
288 <xsl:template name="gentext.dingbat">
289 <xsl:param name="dingbat">bullet</xsl:param>
290 <xsl:param name="lang">
291 <xsl:call-template name="l10n.language"/>
294 <xsl:for-each select="$l10n.xml"> <!-- We need to switch context in order to make key() work -->
295 <xsl:for-each select="document(key('l10n-lang', $lang)/@href)">
296 <xsl:variable name="local.l10n.dingbat"
297 select="($local.l10n.xml//l:i18n/l:l10n[@language=$lang]/l:dingbat[@key=$dingbat])[1]"/>
299 <xsl:variable name="l10n.dingbat"
300 select="key('l10n-dingbat', $dingbat)[1]"/>
303 <xsl:when test="$local.l10n.dingbat">
304 <xsl:value-of select="$local.l10n.dingbat/@text"/>
306 <xsl:when test="$l10n.dingbat">
307 <xsl:value-of select="$l10n.dingbat/@text"/>
311 <xsl:text>No "</xsl:text>
312 <xsl:value-of select="$lang"/>
313 <xsl:text>" localization of dingbat </xsl:text>
314 <xsl:value-of select="$dingbat"/>
315 <xsl:text> exists; using "en".</xsl:text>
318 <xsl:for-each select="$l10n.xml"> <!-- We need to switch context in order to make key() work -->
319 <xsl:for-each select="document(key('l10n-lang', 'en')/@href)">
320 <xsl:value-of select="key('l10n-dingbat', $dingbat)[1]/@text"/>
329 <xsl:template name="gentext.startquote">
330 <xsl:call-template name="gentext.dingbat">
331 <xsl:with-param name="dingbat">startquote</xsl:with-param>
335 <xsl:template name="gentext.endquote">
336 <xsl:call-template name="gentext.dingbat">
337 <xsl:with-param name="dingbat">endquote</xsl:with-param>
341 <xsl:template name="gentext.nestedstartquote">
342 <xsl:call-template name="gentext.dingbat">
343 <xsl:with-param name="dingbat">nestedstartquote</xsl:with-param>
347 <xsl:template name="gentext.nestedendquote">
348 <xsl:call-template name="gentext.dingbat">
349 <xsl:with-param name="dingbat">nestedendquote</xsl:with-param>
353 <xsl:template name="gentext.nav.prev">
354 <xsl:call-template name="gentext">
355 <xsl:with-param name="key" select="'nav-prev'"/>
359 <xsl:template name="gentext.nav.next">
360 <xsl:call-template name="gentext">
361 <xsl:with-param name="key" select="'nav-next'"/>
365 <xsl:template name="gentext.nav.home">
366 <xsl:call-template name="gentext">
367 <xsl:with-param name="key" select="'nav-home'"/>
371 <xsl:template name="gentext.nav.up">
372 <xsl:call-template name="gentext">
373 <xsl:with-param name="key" select="'nav-up'"/>
377 <!-- ============================================================ -->
379 <xsl:template name="gentext.template">
380 <xsl:param name="context" select="'default'"/>
381 <xsl:param name="name" select="'default'"/>
382 <xsl:param name="origname" select="$name"/>
383 <xsl:param name="purpose"/>
384 <xsl:param name="xrefstyle"/>
385 <xsl:param name="referrer"/>
386 <xsl:param name="lang">
387 <xsl:call-template name="l10n.language"/>
389 <xsl:param name="verbose" select="1"/>
392 <xsl:when test="$empty.local.l10n.xml">
393 <xsl:for-each select="$l10n.xml"> <!-- We need to switch context in order to make key() work -->
394 <xsl:for-each select="document(key('l10n-lang', $lang)/@href)">
396 <xsl:variable name="localization.node"
397 select="key('l10n-lang', $lang)[1]"/>
399 <xsl:if test="count($localization.node) = 0
402 <xsl:text>No "</xsl:text>
403 <xsl:value-of select="$lang"/>
404 <xsl:text>" localization exists.</xsl:text>
408 <xsl:variable name="context.node"
409 select="key('l10n-context', $context)[1]"/>
411 <xsl:if test="count($context.node) = 0
414 <xsl:text>No context named "</xsl:text>
415 <xsl:value-of select="$context"/>
416 <xsl:text>" exists in the "</xsl:text>
417 <xsl:value-of select="$lang"/>
418 <xsl:text>" localization.</xsl:text>
422 <xsl:for-each select="$context.node">
423 <xsl:variable name="template.node"
424 select="(key('l10n-template-style', concat($context, '#', $name, '#', $xrefstyle))
425 |key('l10n-template', concat($context, '#', $name)))[1]"/>
428 <xsl:when test="$template.node/@text">
429 <xsl:value-of select="$template.node/@text"/>
433 <xsl:when test="contains($name, '/')">
434 <xsl:call-template name="gentext.template">
435 <xsl:with-param name="context" select="$context"/>
436 <xsl:with-param name="name" select="substring-after($name, '/')"/>
437 <xsl:with-param name="origname" select="$origname"/>
438 <xsl:with-param name="purpose" select="$purpose"/>
439 <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
440 <xsl:with-param name="referrer" select="$referrer"/>
441 <xsl:with-param name="lang" select="$lang"/>
442 <xsl:with-param name="verbose" select="$verbose"/>
445 <xsl:when test="$verbose = 0">
450 <xsl:text>No template for "</xsl:text>
451 <xsl:value-of select="$origname"/>
452 <xsl:text>" (or any of its leaves) exists in the context named "</xsl:text>
453 <xsl:value-of select="$context"/>
454 <xsl:text>" in the "</xsl:text>
455 <xsl:value-of select="$lang"/>
456 <xsl:text>" localization.</xsl:text>
467 <xsl:for-each select="$l10n.xml"> <!-- We need to switch context in order to make key() work -->
468 <xsl:for-each select="document(key('l10n-lang', $lang)/@href)">
470 <xsl:variable name="local.localization.node"
471 select="($local.l10n.xml//l:i18n/l:l10n[@language=$lang])[1]"/>
473 <xsl:variable name="localization.node"
474 select="key('l10n-lang', $lang)[1]"/>
476 <xsl:if test="count($localization.node) = 0
477 and count($local.localization.node) = 0
480 <xsl:text>No "</xsl:text>
481 <xsl:value-of select="$lang"/>
482 <xsl:text>" localization exists.</xsl:text>
486 <xsl:variable name="local.context.node"
487 select="$local.localization.node/l:context[@name=$context]"/>
489 <xsl:variable name="context.node"
490 select="key('l10n-context', $context)[1]"/>
492 <xsl:if test="count($context.node) = 0
493 and count($local.context.node) = 0
496 <xsl:text>No context named "</xsl:text>
497 <xsl:value-of select="$context"/>
498 <xsl:text>" exists in the "</xsl:text>
499 <xsl:value-of select="$lang"/>
500 <xsl:text>" localization.</xsl:text>
504 <xsl:variable name="local.template.node"
505 select="($local.context.node/l:template[@name=$name
507 and @style=$xrefstyle]
508 |$local.context.node/l:template[@name=$name
509 and not(@style)])[1]"/>
511 <xsl:for-each select="$context.node">
512 <xsl:variable name="template.node"
513 select="(key('l10n-template-style', concat($context, '#', $name, '#', $xrefstyle))
514 |key('l10n-template', concat($context, '#', $name)))[1]"/>
517 <xsl:when test="$local.template.node/@text">
518 <xsl:value-of select="$local.template.node/@text"/>
520 <xsl:when test="$template.node/@text">
521 <xsl:value-of select="$template.node/@text"/>
525 <xsl:when test="contains($name, '/')">
526 <xsl:call-template name="gentext.template">
527 <xsl:with-param name="context" select="$context"/>
528 <xsl:with-param name="name" select="substring-after($name, '/')"/>
529 <xsl:with-param name="origname" select="$origname"/>
530 <xsl:with-param name="purpose" select="$purpose"/>
531 <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
532 <xsl:with-param name="referrer" select="$referrer"/>
533 <xsl:with-param name="lang" select="$lang"/>
534 <xsl:with-param name="verbose" select="$verbose"/>
537 <xsl:when test="$verbose = 0">
542 <xsl:text>No template for "</xsl:text>
543 <xsl:value-of select="$origname"/>
544 <xsl:text>" (or any of its leaves) exists in the context named "</xsl:text>
545 <xsl:value-of select="$context"/>
546 <xsl:text>" in the "</xsl:text>
547 <xsl:value-of select="$lang"/>
548 <xsl:text>" localization.</xsl:text>
561 <!-- silently test if a gentext template exists -->
563 <xsl:template name="gentext.template.exists">
564 <xsl:param name="context" select="'default'"/>
565 <xsl:param name="name" select="'default'"/>
566 <xsl:param name="origname" select="$name"/>
567 <xsl:param name="purpose"/>
568 <xsl:param name="xrefstyle"/>
569 <xsl:param name="referrer"/>
570 <xsl:param name="lang">
571 <xsl:call-template name="l10n.language"/>
574 <xsl:variable name="template">
575 <xsl:call-template name="gentext.template">
576 <xsl:with-param name="context" select="$context"/>
577 <xsl:with-param name="name" select="$name"/>
578 <xsl:with-param name="origname" select="$origname"/>
579 <xsl:with-param name="purpose" select="$purpose"/>
580 <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
581 <xsl:with-param name="referrer" select="$referrer"/>
582 <xsl:with-param name="lang" select="$lang"/>
583 <xsl:with-param name="verbose" select="0"/>
588 <xsl:when test="string-length($template) != 0">1</xsl:when>
589 <xsl:otherwise>0</xsl:otherwise>