l10n.xsl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?xml version='1.0'?>
  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"
  5. version='1.0'>
  6. <!-- ********************************************************************
  7. $Id$
  8. ********************************************************************
  9. This file is part of the XSL DocBook Stylesheet distribution.
  10. See ../README or http://docbook.sf.net/release/xsl/current/ for
  11. copyright and other information.
  12. This file contains localization templates (for internationalization)
  13. ******************************************************************** -->
  14. <xsl:param name="l10n.xml" select="document('../common/l10n.xml')"/>
  15. <xsl:param name="local.l10n.xml" select="document('')"/>
  16. <xsl:template name="l10n.language">
  17. <xsl:param name="target" select="."/>
  18. <xsl:param name="xref-context" select="false()"/>
  19. <xsl:variable name="mc-language">
  20. <xsl:choose>
  21. <xsl:when test="$l10n.gentext.language != ''">
  22. <xsl:value-of select="$l10n.gentext.language"/>
  23. </xsl:when>
  24. <xsl:when test="$xref-context or $l10n.gentext.use.xref.language != 0">
  25. <!-- can't do this one step: attributes are unordered! -->
  26. <xsl:variable name="lang-scope"
  27. select="$target/ancestor-or-self::*
  28. [@lang or @xml:lang][1]"/>
  29. <xsl:variable name="lang-attr"
  30. select="($lang-scope/@lang | $lang-scope/@xml:lang)[1]"/>
  31. <xsl:choose>
  32. <xsl:when test="string($lang-attr) = ''">
  33. <xsl:value-of select="$l10n.gentext.default.language"/>
  34. </xsl:when>
  35. <xsl:otherwise>
  36. <xsl:value-of select="$lang-attr"/>
  37. </xsl:otherwise>
  38. </xsl:choose>
  39. </xsl:when>
  40. <xsl:otherwise>
  41. <!-- can't do this one step: attributes are unordered! -->
  42. <xsl:variable name="lang-scope"
  43. select="$target/ancestor-or-self::*
  44. [@lang or @xml:lang][1]"/>
  45. <xsl:variable name="lang-attr"
  46. select="($lang-scope/@lang | $lang-scope/@xml:lang)[1]"/>
  47. <xsl:choose>
  48. <xsl:when test="string($lang-attr) = ''">
  49. <xsl:value-of select="$l10n.gentext.default.language"/>
  50. </xsl:when>
  51. <xsl:otherwise>
  52. <xsl:value-of select="$lang-attr"/>
  53. </xsl:otherwise>
  54. </xsl:choose>
  55. </xsl:otherwise>
  56. </xsl:choose>
  57. </xsl:variable>
  58. <xsl:variable name="language" select="translate($mc-language,
  59. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  60. 'abcdefghijklmnopqrstuvwxyz')"/>
  61. <xsl:variable name="adjusted.language">
  62. <xsl:choose>
  63. <xsl:when test="contains($language,'-')">
  64. <xsl:value-of select="substring-before($language,'-')"/>
  65. <xsl:text>_</xsl:text>
  66. <xsl:value-of select="substring-after($language,'-')"/>
  67. </xsl:when>
  68. <xsl:otherwise>
  69. <xsl:value-of select="$language"/>
  70. </xsl:otherwise>
  71. </xsl:choose>
  72. </xsl:variable>
  73. <xsl:choose>
  74. <xsl:when test="$l10n.xml/l:i18n/l:l10n[@language=$adjusted.language]">
  75. <xsl:value-of select="$adjusted.language"/>
  76. </xsl:when>
  77. <!-- try just the lang code without country -->
  78. <xsl:when test="$l10n.xml/l:i18n/l:l10n[@language=substring-before($adjusted.language,'_')]">
  79. <xsl:value-of select="substring-before($adjusted.language,'_')"/>
  80. </xsl:when>
  81. <!-- or use the default -->
  82. <xsl:otherwise>
  83. <xsl:message>
  84. <xsl:text>No localization exists for "</xsl:text>
  85. <xsl:value-of select="$adjusted.language"/>
  86. <xsl:text>" or "</xsl:text>
  87. <xsl:value-of select="substring-before($adjusted.language,'_')"/>
  88. <xsl:text>". Using default "</xsl:text>
  89. <xsl:value-of select="$l10n.gentext.default.language"/>
  90. <xsl:text>".</xsl:text>
  91. </xsl:message>
  92. <xsl:value-of select="$l10n.gentext.default.language"/>
  93. </xsl:otherwise>
  94. </xsl:choose>
  95. </xsl:template>
  96. <xsl:template name="language.attribute">
  97. <xsl:param name="node" select="."/>
  98. <xsl:variable name="language">
  99. <xsl:choose>
  100. <xsl:when test="$l10n.gentext.language != ''">
  101. <xsl:value-of select="$l10n.gentext.language"/>
  102. </xsl:when>
  103. <xsl:otherwise>
  104. <!-- can't do this one step: attributes are unordered! -->
  105. <xsl:variable name="lang-scope"
  106. select="$node/ancestor-or-self::*
  107. [@lang or @xml:lang][1]"/>
  108. <xsl:variable name="lang-attr"
  109. select="($lang-scope/@lang | $lang-scope/@xml:lang)[1]"/>
  110. <xsl:choose>
  111. <xsl:when test="string($lang-attr) = ''">
  112. <xsl:value-of select="$l10n.gentext.default.language"/>
  113. </xsl:when>
  114. <xsl:otherwise>
  115. <xsl:value-of select="$lang-attr"/>
  116. </xsl:otherwise>
  117. </xsl:choose>
  118. </xsl:otherwise>
  119. </xsl:choose>
  120. </xsl:variable>
  121. <xsl:if test="$language != ''">
  122. <xsl:attribute name="lang">
  123. <xsl:choose>
  124. <xsl:when test="$l10n.lang.value.rfc.compliant != 0">
  125. <xsl:value-of select="translate($language, '_', '-')"/>
  126. </xsl:when>
  127. <xsl:otherwise>
  128. <xsl:value-of select="$language"/>
  129. </xsl:otherwise>
  130. </xsl:choose>
  131. </xsl:attribute>
  132. </xsl:if>
  133. <!-- FIXME: This is sort of hack, but it was the easiest way to add at least partial support for dir attribute -->
  134. <xsl:copy-of select="ancestor-or-self::*[@dir][1]/@dir"/>
  135. </xsl:template>
  136. <xsl:template name="gentext">
  137. <xsl:param name="key" select="local-name(.)"/>
  138. <xsl:param name="lang">
  139. <xsl:call-template name="l10n.language"/>
  140. </xsl:param>
  141. <xsl:variable name="local.l10n.gentext"
  142. select="($local.l10n.xml//l:i18n/l:l10n[@language=$lang]/l:gentext[@key=$key])[1]"/>
  143. <xsl:variable name="l10n.gentext"
  144. select="($l10n.xml/l:i18n/l:l10n[@language=$lang]/l:gentext[@key=$key])[1]"/>
  145. <xsl:choose>
  146. <xsl:when test="$local.l10n.gentext">
  147. <xsl:value-of select="$local.l10n.gentext/@text"/>
  148. </xsl:when>
  149. <xsl:when test="$l10n.gentext">
  150. <xsl:value-of select="$l10n.gentext/@text"/>
  151. </xsl:when>
  152. <xsl:otherwise>
  153. <xsl:message>
  154. <xsl:text>No "</xsl:text>
  155. <xsl:value-of select="$lang"/>
  156. <xsl:text>" localization of "</xsl:text>
  157. <xsl:value-of select="$key"/>
  158. <xsl:text>" exists</xsl:text>
  159. <xsl:choose>
  160. <xsl:when test="$lang = 'en'">
  161. <xsl:text>.</xsl:text>
  162. </xsl:when>
  163. <xsl:otherwise>
  164. <xsl:text>; using "en".</xsl:text>
  165. </xsl:otherwise>
  166. </xsl:choose>
  167. </xsl:message>
  168. <xsl:value-of select="($l10n.xml/l:i18n/l:l10n[@language='en']/l:gentext[@key=$key])[1]/@text"/>
  169. </xsl:otherwise>
  170. </xsl:choose>
  171. </xsl:template>
  172. <xsl:template name="gentext.element.name">
  173. <xsl:param name="element.name" select="local-name(.)"/>
  174. <xsl:param name="lang">
  175. <xsl:call-template name="l10n.language"/>
  176. </xsl:param>
  177. <xsl:call-template name="gentext">
  178. <xsl:with-param name="key" select="$element.name"/>
  179. <xsl:with-param name="lang" select="$lang"/>
  180. </xsl:call-template>
  181. </xsl:template>
  182. <xsl:template name="gentext.space">
  183. <xsl:text> </xsl:text>
  184. </xsl:template>
  185. <xsl:template name="gentext.edited.by">
  186. <xsl:call-template name="gentext">
  187. <xsl:with-param name="key" select="'Editedby'"/>
  188. </xsl:call-template>
  189. </xsl:template>
  190. <xsl:template name="gentext.by">
  191. <xsl:call-template name="gentext">
  192. <xsl:with-param name="key" select="'by'"/>
  193. </xsl:call-template>
  194. </xsl:template>
  195. <xsl:template name="gentext.dingbat">
  196. <xsl:param name="dingbat">bullet</xsl:param>
  197. <xsl:param name="lang">
  198. <xsl:call-template name="l10n.language"/>
  199. </xsl:param>
  200. <xsl:variable name="local.l10n.dingbat"
  201. select="($local.l10n.xml//l:i18n/l:l10n[@language=$lang]/l:dingbat[@key=$dingbat])[1]"/>
  202. <xsl:variable name="l10n.dingbat"
  203. select="($l10n.xml/l:i18n/l:l10n[@language=$lang]/l:dingbat[@key=$dingbat])[1]"/>
  204. <xsl:choose>
  205. <xsl:when test="$local.l10n.dingbat">
  206. <xsl:value-of select="$local.l10n.dingbat/@text"/>
  207. </xsl:when>
  208. <xsl:when test="$l10n.dingbat">
  209. <xsl:value-of select="$l10n.dingbat/@text"/>
  210. </xsl:when>
  211. <xsl:otherwise>
  212. <xsl:message>
  213. <xsl:text>No "</xsl:text>
  214. <xsl:value-of select="$lang"/>
  215. <xsl:text>" localization of dingbat </xsl:text>
  216. <xsl:value-of select="$dingbat"/>
  217. <xsl:text> exists; using "en".</xsl:text>
  218. </xsl:message>
  219. <xsl:value-of select="($l10n.xml/l:i18n/l:l10n[@language='en']/l:dingbat[@key=$dingbat])[1]/@text"/>
  220. </xsl:otherwise>
  221. </xsl:choose>
  222. </xsl:template>
  223. <xsl:template name="gentext.startquote">
  224. <xsl:call-template name="gentext.dingbat">
  225. <xsl:with-param name="dingbat">startquote</xsl:with-param>
  226. </xsl:call-template>
  227. </xsl:template>
  228. <xsl:template name="gentext.endquote">
  229. <xsl:call-template name="gentext.dingbat">
  230. <xsl:with-param name="dingbat">endquote</xsl:with-param>
  231. </xsl:call-template>
  232. </xsl:template>
  233. <xsl:template name="gentext.nestedstartquote">
  234. <xsl:call-template name="gentext.dingbat">
  235. <xsl:with-param name="dingbat">nestedstartquote</xsl:with-param>
  236. </xsl:call-template>
  237. </xsl:template>
  238. <xsl:template name="gentext.nestedendquote">
  239. <xsl:call-template name="gentext.dingbat">
  240. <xsl:with-param name="dingbat">nestedendquote</xsl:with-param>
  241. </xsl:call-template>
  242. </xsl:template>
  243. <xsl:template name="gentext.nav.prev">
  244. <xsl:call-template name="gentext">
  245. <xsl:with-param name="key" select="'nav-prev'"/>
  246. </xsl:call-template>
  247. </xsl:template>
  248. <xsl:template name="gentext.nav.next">
  249. <xsl:call-template name="gentext">
  250. <xsl:with-param name="key" select="'nav-next'"/>
  251. </xsl:call-template>
  252. </xsl:template>
  253. <xsl:template name="gentext.nav.home">
  254. <xsl:call-template name="gentext">
  255. <xsl:with-param name="key" select="'nav-home'"/>
  256. </xsl:call-template>
  257. </xsl:template>
  258. <xsl:template name="gentext.nav.up">
  259. <xsl:call-template name="gentext">
  260. <xsl:with-param name="key" select="'nav-up'"/>
  261. </xsl:call-template>
  262. </xsl:template>
  263. <!-- ============================================================ -->
  264. <xsl:template name="gentext.template">
  265. <xsl:param name="context" select="'default'"/>
  266. <xsl:param name="name" select="'default'"/>
  267. <xsl:param name="origname" select="$name"/>
  268. <xsl:param name="purpose"/>
  269. <xsl:param name="xrefstyle"/>
  270. <xsl:param name="referrer"/>
  271. <xsl:param name="lang">
  272. <xsl:call-template name="l10n.language"/>
  273. </xsl:param>
  274. <xsl:param name="verbose" select="1"/>
  275. <xsl:variable name="local.localization.node"
  276. select="($local.l10n.xml//l:i18n/l:l10n[@language=$lang])[1]"/>
  277. <xsl:variable name="localization.node"
  278. select="($l10n.xml/l:i18n/l:l10n[@language=$lang])[1]"/>
  279. <xsl:if test="count($localization.node) = 0
  280. and count($local.localization.node) = 0
  281. and $verbose != 0">
  282. <xsl:message>
  283. <xsl:text>No "</xsl:text>
  284. <xsl:value-of select="$lang"/>
  285. <xsl:text>" localization exists.</xsl:text>
  286. </xsl:message>
  287. </xsl:if>
  288. <xsl:variable name="local.context.node"
  289. select="$local.localization.node/l:context[@name=$context]"/>
  290. <xsl:variable name="context.node"
  291. select="$localization.node/l:context[@name=$context]"/>
  292. <xsl:if test="count($context.node) = 0
  293. and count($local.context.node) = 0
  294. and $verbose != 0">
  295. <xsl:message>
  296. <xsl:text>No context named "</xsl:text>
  297. <xsl:value-of select="$context"/>
  298. <xsl:text>" exists in the "</xsl:text>
  299. <xsl:value-of select="$lang"/>
  300. <xsl:text>" localization.</xsl:text>
  301. </xsl:message>
  302. </xsl:if>
  303. <xsl:variable name="local.template.node"
  304. select="($local.context.node/l:template[@name=$name
  305. and @style
  306. and @style=$xrefstyle]
  307. |$local.context.node/l:template[@name=$name
  308. and not(@style)])[1]"/>
  309. <xsl:variable name="template.node"
  310. select="($context.node/l:template[@name=$name
  311. and @style
  312. and @style=$xrefstyle]
  313. |$context.node/l:template[@name=$name
  314. and not(@style)])[1]"/>
  315. <xsl:choose>
  316. <xsl:when test="$local.template.node/@text">
  317. <xsl:value-of select="$local.template.node/@text"/>
  318. </xsl:when>
  319. <xsl:when test="$template.node/@text">
  320. <xsl:value-of select="$template.node/@text"/>
  321. </xsl:when>
  322. <xsl:otherwise>
  323. <xsl:choose>
  324. <xsl:when test="contains($name, '/')">
  325. <xsl:call-template name="gentext.template">
  326. <xsl:with-param name="context" select="$context"/>
  327. <xsl:with-param name="name" select="substring-after($name, '/')"/>
  328. <xsl:with-param name="origname" select="$origname"/>
  329. <xsl:with-param name="purpose" select="$purpose"/>
  330. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  331. <xsl:with-param name="referrer" select="$referrer"/>
  332. <xsl:with-param name="lang" select="$lang"/>
  333. <xsl:with-param name="verbose" select="$verbose"/>
  334. </xsl:call-template>
  335. </xsl:when>
  336. <xsl:when test="$verbose = 0">
  337. <!-- silence -->
  338. </xsl:when>
  339. <xsl:otherwise>
  340. <xsl:message>
  341. <xsl:text>No template for "</xsl:text>
  342. <xsl:value-of select="$origname"/>
  343. <xsl:text>" (or any of its leaves) exists
  344. in the context named "</xsl:text>
  345. <xsl:value-of select="$context"/>
  346. <xsl:text>" in the "</xsl:text>
  347. <xsl:value-of select="$lang"/>
  348. <xsl:text>" localization.</xsl:text>
  349. </xsl:message>
  350. </xsl:otherwise>
  351. </xsl:choose>
  352. </xsl:otherwise>
  353. </xsl:choose>
  354. </xsl:template>
  355. <!-- silently test if a gentext template exists -->
  356. <xsl:template name="gentext.template.exists">
  357. <xsl:param name="context" select="'default'"/>
  358. <xsl:param name="name" select="'default'"/>
  359. <xsl:param name="origname" select="$name"/>
  360. <xsl:param name="purpose"/>
  361. <xsl:param name="xrefstyle"/>
  362. <xsl:param name="referrer"/>
  363. <xsl:param name="lang">
  364. <xsl:call-template name="l10n.language"/>
  365. </xsl:param>
  366. <xsl:variable name="template">
  367. <xsl:call-template name="gentext.template">
  368. <xsl:with-param name="context" select="$context"/>
  369. <xsl:with-param name="name" select="$name"/>
  370. <xsl:with-param name="origname" select="$origname"/>
  371. <xsl:with-param name="purpose" select="$purpose"/>
  372. <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
  373. <xsl:with-param name="referrer" select="$referrer"/>
  374. <xsl:with-param name="lang" select="$lang"/>
  375. <xsl:with-param name="verbose" select="0"/>
  376. </xsl:call-template>
  377. </xsl:variable>
  378. <xsl:choose>
  379. <xsl:when test="string-length($template) != 0">1</xsl:when>
  380. <xsl:otherwise>0</xsl:otherwise>
  381. </xsl:choose>
  382. </xsl:template>
  383. </xsl:stylesheet>