~ubuntu-branches/ubuntu/lucid/gnome-doc-utils/lucid

« back to all changes in this revision

Viewing changes to xslt/gettext/format2xsl.xsl

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-19 08:34:21 UTC
  • mto: (2.1.1 etch) (1.1.18 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20051219083421-k72rkh3n6vox1c0t
Tags: upstream-0.5.2
ImportĀ upstreamĀ versionĀ 0.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?xml version='1.0' encoding='utf-8'?><!-- -*- indent-tabs-mode: nil -*- -->
2
 
 
3
 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4
 
                xmlns:msg="http://www.gnome.org/~shaunm/gnome-doc-utils/xsl-format"
5
 
                xmlns:doc="http://www.gnome.org/~shaunm/xsldoc"
6
 
                xmlns:xslt="http://www.example.com/XSLT"
7
 
                exclude-result-prefixes="doc"
8
 
                version="1.0">
9
 
 
10
 
<xsl:namespace-alias stylesheet-prefix="xslt" result-prefix="xsl"/>
11
 
 
12
 
<xsl:include href="gettext.xsl"/>
13
 
 
14
 
<xsl:output method="xml" encoding="UTF-8"/>
15
 
 
16
 
<!-- == format2xsl == -->
17
 
<xsl:template name="format2xsl" mode="format2xsl.mode" match="text()">
18
 
  <xsl:param name="msgstr" select="."/>
19
 
  <xsl:param name="msgstr_cur" select="$msgstr"/>
20
 
  <xsl:param name="template"/>
21
 
  <xsl:param name="lang"/>
22
 
  <xsl:choose>
23
 
    <xsl:when test="contains($msgstr_cur, '%')">
24
 
      <xsl:choose>
25
 
        <xsl:when test="starts-with(substring-after($msgstr_cur, '%'), '%')">
26
 
          <xslt:text>
27
 
            <xsl:value-of select="substring-before($msgstr_cur, '%')"/>
28
 
            <xsl:text>%</xsl:text>
29
 
          </xslt:text>
30
 
        </xsl:when>
31
 
        <xsl:otherwise>
32
 
          <xslt:text>
33
 
            <xsl:value-of select="substring-before($msgstr_cur, '%')"/>
34
 
          </xslt:text>
35
 
          <xsl:variable name="type"
36
 
                        select="substring-before(
37
 
                                  substring-after($msgstr_cur, '%'),
38
 
                                  '{')"/>
39
 
          <xsl:variable name="name"
40
 
                        select="substring-before(
41
 
                                  substring-after($msgstr_cur, '{'),
42
 
                                  '}')"/>
43
 
          <xsl:variable name="msgstr_aft"
44
 
                        select="substring-after($msgstr_cur, '}')"/>
45
 
          <xsl:variable name="arg"
46
 
                        select="$template/msg:arg[@name = $name]"/>
47
 
          <xsl:if test="not($arg)">
48
 
            <xsl:message terminate="yes">
49
 
              <xsl:value-of select="concat(
50
 
                            'format2xsl: Error in template ', $template/@name,
51
 
                            ', format string for ', $lang, ' references ', $name,
52
 
                            ' but no such argument exists')"/>
53
 
            </xsl:message>
54
 
          </xsl:if>
55
 
          <xsl:choose>
56
 
            <xsl:when test="$type = 't'">
57
 
              <xsl:if test="namespace-uri($arg/*[1]) !=
58
 
                              'http://www.w3.org/1999/XSL/Transform'">
59
 
                <xsl:message terminate="yes">
60
 
                  <xsl:value-of select="concat(
61
 
                                'format2xsl: Error in template ', $template/@name,
62
 
                                ', format string for argument ', $name,
63
 
                                ' in locale ', $lang,
64
 
                                ' is type ', $type,
65
 
                                ' but template calls ', local-name($arg/*[1])
66
 
                                )"/>
67
 
                </xsl:message>
68
 
              </xsl:if>
69
 
              <xsl:apply-templates select="$arg/*"/>
70
 
            </xsl:when>
71
 
            <xsl:when test="$type = 's'">
72
 
              <xsl:if test="not($arg/xsl:value-of)">
73
 
                <xsl:message terminate="yes">
74
 
                  <xsl:value-of select="concat(
75
 
                                'format2xsl: Error in template ', $template/@name,
76
 
                                ', format string for argument ', $name,
77
 
                                ' is type ', $type,
78
 
                                ' but template calls ', local-name($arg/*[1])
79
 
                                )"/>
80
 
                </xsl:message>
81
 
              </xsl:if>
82
 
              <xsl:apply-templates select="$arg/*"/>
83
 
            </xsl:when>
84
 
            <xsl:when test="$type = 'a' or $type = 'A' or
85
 
                            $type = 'i' or $type = 'I' or
86
 
                            $type = '1' ">
87
 
              <xsl:if test="not($arg/xsl:number)">
88
 
                <xsl:message terminate="yes">
89
 
                  <xsl:value-of select="concat(
90
 
                                'format2xsl: Error in template ', $template/@name,
91
 
                                ', format string for argument ', $name,
92
 
                                ' is type ', $type,
93
 
                                ' but template calls ', local-name($arg/*[1])
94
 
                                )"/>
95
 
                </xsl:message>
96
 
              </xsl:if>
97
 
              <xsl:apply-templates select="$arg/*">
98
 
                <xsl:with-param name="format" select="$type"/>
99
 
              </xsl:apply-templates>
100
 
            </xsl:when>
101
 
          </xsl:choose>
102
 
          <xsl:call-template name="format2xsl">
103
 
            <xsl:with-param name="msgstr" select="$msgstr"/>
104
 
            <xsl:with-param name="msgstr_cur" select="$msgstr_aft"/>
105
 
            <xsl:with-param name="template" select="$template"/>
106
 
            <xsl:with-param name="lang" select="$lang"/>
107
 
          </xsl:call-template>
108
 
        </xsl:otherwise>
109
 
      </xsl:choose>
110
 
    </xsl:when>
111
 
    <xsl:otherwise>
112
 
      <xslt:text>
113
 
        <xsl:value-of select="$msgstr_cur"/>
114
 
      </xslt:text>
115
 
    </xsl:otherwise>
116
 
  </xsl:choose>
117
 
</xsl:template>
118
 
 
119
 
<xsl:template mode="format2xsl.mode" match="msg:msgstr">
120
 
  <xsl:param name="template"/>
121
 
  <xsl:param name="lang"/>
122
 
  <xsl:choose>
123
 
    <xsl:when test="xsl:template">
124
 
      <xsl:apply-templates select="xsl:template/node()"/>
125
 
    </xsl:when>
126
 
    <xsl:when test="../self::msg:msg and (msg:msgstr[@role] or
127
 
              ($template/xsl:param[@name = 'role']    and (
128
 
                $template/msg:arg[@name = 'title']        or
129
 
                $template/msg:arg[@name = 'titleabbrev']  or
130
 
                $template/msg:arg[@name = 'subtitle']     or
131
 
                $template/msg:arg[@name = 'number']       )))">
132
 
      <xslt:choose>
133
 
        <xsl:for-each select="msg:msgstr[@role]">
134
 
          <xslt:when test="$role = '{@role}'">
135
 
            <xsl:apply-templates mode="format2xsl.mode" select=".">
136
 
              <xsl:with-param name="template" select="$template"/>
137
 
              <xsl:with-param name="lang" select="$lang"/>
138
 
            </xsl:apply-templates>
139
 
          </xslt:when>
140
 
        </xsl:for-each>
141
 
        <xsl:if test="../self::msg:msg and
142
 
                      $template/xsl:param[@name = 'role']">
143
 
          <xsl:if test="not(msg:msgstr[@role = 'title'])   and
144
 
                        $template/msg:arg[@name = 'title'] ">
145
 
            <xslt:when test="$role = 'title'">
146
 
              <xsl:apply-templates
147
 
               select="$template/msg:arg[@name = 'title']/node()"/>
148
 
            </xslt:when>
149
 
          </xsl:if>
150
 
          <xsl:if test="not(msg:msgstr[@role = 'titleabbrev'])   and
151
 
                        $template/msg:arg[@name = 'titleabbrev'] ">
152
 
            <xslt:when test="$role = 'titleabbrev'">
153
 
              <xsl:apply-templates
154
 
               select="$template/msg:arg[@name = 'titleabbrev']/node()"/>
155
 
            </xslt:when>
156
 
          </xsl:if>
157
 
          <xsl:if test="not(msg:msgstr[@role = 'subtitle'])   and
158
 
                        $template/msg:arg[@name = 'subtitle'] ">
159
 
            <xslt:when test="$role = 'subtitle'">
160
 
              <xsl:apply-templates
161
 
               select="$template/msg:arg[@name = 'subtitle']/node()"/>
162
 
            </xslt:when>
163
 
          </xsl:if>
164
 
          <xsl:if test="not(msg:msgstr[@role = 'number'])   and
165
 
                        $template/msg:arg[@name = 'number'] ">
166
 
            <xslt:when test="$role = 'number'">
167
 
              <xsl:apply-templates
168
 
               select="$template/msg:arg[@name = 'number']/node()"/>
169
 
            </xslt:when>
170
 
          </xsl:if>
171
 
        </xsl:if>
172
 
        <xsl:choose>
173
 
          <xsl:when test="msg:msgstr[not(@role)]">
174
 
            <xslt:otherwise>
175
 
              <xsl:apply-templates mode="format2xsl.mode"
176
 
                                   select="msg:msgstr[not(@role)][1]">
177
 
                <xsl:with-param name="template" select="$template"/>
178
 
                <xsl:with-param name="lang" select="$lang"/>
179
 
              </xsl:apply-templates>
180
 
            </xslt:otherwise>
181
 
          </xsl:when>
182
 
          <xsl:when test="not(msg:msgstr[@role])">
183
 
            <xslt:otherwise>
184
 
              <xsl:for-each select="node()">
185
 
                <xsl:apply-templates mode="format2xsl.mode" select=".">
186
 
                  <xsl:with-param name="template" select="$template"/>
187
 
                  <xsl:with-param name="lang" select="$lang"/>
188
 
                </xsl:apply-templates>
189
 
              </xsl:for-each>
190
 
            </xslt:otherwise>
191
 
          </xsl:when>
192
 
          <xsl:otherwise>
193
 
            <xslt:otherwise>
194
 
              <xslt:message>
195
 
                <xslt:text>No translation for </xslt:text>
196
 
                <xslt:text><xsl:value-of select="$template/@name"/></xslt:text>
197
 
                <xslt:text> with role </xslt:text>
198
 
                <xslt:value-of select="$role"/>
199
 
              </xslt:message>
200
 
            </xslt:otherwise>
201
 
          </xsl:otherwise>
202
 
        </xsl:choose>
203
 
      </xslt:choose>
204
 
    </xsl:when>
205
 
    <xsl:otherwise>
206
 
      <xsl:for-each select="node()">
207
 
        <xsl:apply-templates mode="format2xsl.mode" select=".">
208
 
          <xsl:with-param name="template" select="$template"/>
209
 
          <xsl:with-param name="lang" select="$lang"/>
210
 
        </xsl:apply-templates>
211
 
      </xsl:for-each>
212
 
    </xsl:otherwise>
213
 
  </xsl:choose>
214
 
</xsl:template>
215
 
 
216
 
<xsl:template mode="format2xsl.mode" match="*">
217
 
  <xsl:param name="template"/>
218
 
  <xsl:param name="lang"/>
219
 
  <xslt:variable name="name">
220
 
    <xslt:call-template name="format2xsl.element.name">
221
 
      <xslt:with-param name="name" select="'{local-name(.)}'"/>
222
 
      <xslt:with-param name="namespace" select="'{namespace-uri(.)}'"/>
223
 
    </xslt:call-template>
224
 
  </xslt:variable>
225
 
  <xslt:variable name="namespace">
226
 
    <xslt:call-template name="format2xsl.element.namespace">
227
 
      <xslt:with-param name="name" select="'{local-name(.)}'"/>
228
 
      <xslt:with-param name="namespace" select="'{namespace-uri(.)}'"/>
229
 
    </xslt:call-template>
230
 
  </xslt:variable>
231
 
  <xslt:element>
232
 
    <xsl:attribute name="name">
233
 
      <xsl:text>{$name}</xsl:text>
234
 
    </xsl:attribute>
235
 
    <xsl:attribute name="namespace">
236
 
      <xsl:text>{$namespace}</xsl:text>
237
 
    </xsl:attribute>
238
 
    <xsl:for-each select="* | text()">
239
 
      <xsl:apply-templates mode="format2xsl.mode" select=".">
240
 
        <xsl:with-param name="template" select="$template"/>
241
 
        <xsl:with-param name="lang" select="$lang"/>
242
 
      </xsl:apply-templates>
243
 
    </xsl:for-each>
244
 
  </xslt:element>
245
 
</xsl:template>
246
 
 
247
 
<!-- == msg:msg == -->
248
 
<xsl:template match="msg:msg">
249
 
  <xsl:variable name="msg" select="."/>
250
 
  <xsl:choose>
251
 
    <xsl:when test="(count(msg:msgstr[string(.) != $msg/@id]) != 1)  and
252
 
                    not(count(msg:msgstr[string(.) != $msg/@id]) = 2 and
253
 
                        msg:msgstr[not(@xml:lang)]  and
254
 
                        msg:msgstr[@xml:lang = 'C'] )">
255
 
      <xslt:choose>
256
 
        <xsl:for-each select="msg:msgstr[string(.) != $msg/@id]">
257
 
          <xsl:sort select="contains(@xml:lang, '@') and contains(@xml:lang, '.')"
258
 
                    order="descending"/>
259
 
          <xsl:sort select="contains(@xml:lang, '.')" order="descending"/>
260
 
          <xsl:sort select="contains(@xml:lang, '@')" order="descending"/>
261
 
          <xsl:sort select="contains(@xml:lang, '_')" order="descending"/>
262
 
          <xsl:sort select="string-length(@xml:lang)" order="descending"/>
263
 
          <xsl:variable name="lang_language">
264
 
            <xsl:call-template name="gettext.get.language">
265
 
              <xsl:with-param name="lang" select="@xml:lang"/>
266
 
            </xsl:call-template>
267
 
          </xsl:variable>
268
 
          <xsl:variable name="lang_region">
269
 
            <xsl:call-template name="gettext.get.region">
270
 
              <xsl:with-param name="lang" select="@xml:lang"/>
271
 
            </xsl:call-template>
272
 
          </xsl:variable>
273
 
          <xsl:variable name="lang_variant">
274
 
            <xsl:call-template name="gettext.get.variant">
275
 
              <xsl:with-param name="lang" select="@xml:lang"/>
276
 
            </xsl:call-template>
277
 
          </xsl:variable>
278
 
          <xsl:variable name="lang_charset">
279
 
            <xsl:call-template name="gettext.get.charset">
280
 
              <xsl:with-param name="lang" select="@xml:lang"/>
281
 
            </xsl:call-template>
282
 
          </xsl:variable>
283
 
          <xsl:variable name="element">
284
 
            <xsl:choose>
285
 
              <xsl:when test="@xml:lang and @xml:lang != 'C'">
286
 
                <xsl:text>xslt:when</xsl:text>
287
 
              </xsl:when>
288
 
              <xsl:otherwise>
289
 
                <xsl:text>xslt:otherwise</xsl:text>
290
 
              </xsl:otherwise>
291
 
            </xsl:choose>
292
 
          </xsl:variable>
293
 
          <xsl:if test="@xml:lang or not(../msg:msgstr[@xml:lang = 'C'])">
294
 
            <xsl:element name="{$element}">
295
 
              <xsl:if test="$element = 'xslt:when'">
296
 
                <xsl:attribute name="test">
297
 
                  <xsl:text>$lang_language = '</xsl:text>
298
 
                  <xsl:value-of select="$lang_language"/>
299
 
                  <xsl:text>'</xsl:text>
300
 
                  <xsl:if test="$lang_region != ''">
301
 
                    <xsl:text> and $lang_region = '</xsl:text>
302
 
                    <xsl:value-of select="$lang_region"/>
303
 
                    <xsl:text>'</xsl:text>
304
 
                  </xsl:if>
305
 
                  <xsl:if test="$lang_variant != ''">
306
 
                    <xsl:text> and $lang_variant = '</xsl:text>
307
 
                    <xsl:value-of select="$lang_variant"/>
308
 
                    <xsl:text>'</xsl:text>
309
 
                  </xsl:if>
310
 
                  <xsl:if test="$lang_charset != ''">
311
 
                    <xsl:text> and $lang_charset = '</xsl:text>
312
 
                    <xsl:value-of select="$lang_charset"/>
313
 
                    <xsl:text>'</xsl:text>
314
 
                  </xsl:if>
315
 
                </xsl:attribute>
316
 
              </xsl:if>
317
 
              <xsl:apply-templates mode="format2xsl.mode" select=".">
318
 
                <xsl:with-param name="template" select="../.."/>
319
 
                <xsl:with-param name="lang" select="@xml:lang"/>
320
 
              </xsl:apply-templates>
321
 
            </xsl:element>
322
 
          </xsl:if>
323
 
        </xsl:for-each>
324
 
      </xslt:choose>
325
 
    </xsl:when>
326
 
    <xsl:otherwise>
327
 
      <xsl:choose>
328
 
        <xsl:when test="msg:msgstr[@xml:lang = 'C']">
329
 
          <xsl:for-each select="msg:msgstr[@xml:lang = 'C'][1]">
330
 
            <xsl:apply-templates mode="format2xsl.mode" select=".">
331
 
              <xsl:with-param name="template" select="../.."/>
332
 
              <xsl:with-param name="lang" select="'C'"/>
333
 
            </xsl:apply-templates>
334
 
          </xsl:for-each>
335
 
        </xsl:when>
336
 
        <xsl:otherwise>
337
 
          <xsl:for-each select="msg:msgstr[1]">
338
 
            <xsl:apply-templates mode="format2xsl.mode" select=".">
339
 
              <xsl:with-param name="template" select="../.."/>
340
 
              <xsl:with-param name="lang" select="'C'"/>
341
 
            </xsl:apply-templates>
342
 
          </xsl:for-each>
343
 
        </xsl:otherwise>
344
 
      </xsl:choose>
345
 
    </xsl:otherwise>
346
 
  </xsl:choose>
347
 
</xsl:template>
348
 
 
349
 
<!-- == -->
350
 
<xsl:template match="*">
351
 
  <xsl:copy>
352
 
    <xsl:for-each select="attribute::*">
353
 
      <xsl:copy-of select="."/>
354
 
    </xsl:for-each>
355
 
    <xsl:apply-templates select="node()"/>
356
 
  </xsl:copy>
357
 
</xsl:template>
358
 
 
359
 
<!-- == doc:template == -->
360
 
<xsl:template match="doc:template">
361
 
  <xsl:variable name="doc_template" select="."/>
362
 
  <xsl:variable name="xsl_template" select="following-sibling::xsl:template[1]"/>
363
 
  <xsl:copy>
364
 
    <xsl:for-each select="node()">
365
 
      <xsl:apply-templates select="."/>
366
 
      <xsl:if test="self::doc:purpose">
367
 
        <xsl:if test="not($doc_template/doc:parameter[doc:name='lang'])">
368
 
          <doc:parameter>
369
 
            <doc:name>lang</doc:name>
370
 
            <doc:purpose>
371
 
              The language to use for formatting
372
 
            </doc:purpose>
373
 
          </doc:parameter>
374
 
        </xsl:if>
375
 
      </xsl:if>
376
 
    </xsl:for-each>
377
 
  </xsl:copy>
378
 
</xsl:template>
379
 
 
380
 
<!-- == xsl:* == -->
381
 
<xsl:template match="xsl:*">
382
 
  <xsl:copy>
383
 
    <xsl:for-each select="attribute::*">
384
 
      <xsl:copy/>
385
 
    </xsl:for-each>
386
 
    <xsl:apply-templates/>
387
 
  </xsl:copy>
388
 
</xsl:template>
389
 
 
390
 
<!-- == xsl:apply-templates == -->
391
 
<xsl:template match="xsl:apply-templates">
392
 
  <xslt:apply-templates>
393
 
    <xsl:for-each select="attribute::*">
394
 
      <xsl:copy/>
395
 
    </xsl:for-each>
396
 
    <xsl:apply-templates/>
397
 
  </xslt:apply-templates>
398
 
</xsl:template>
399
 
 
400
 
<!-- == xsl:number == -->
401
 
<xsl:template match="xsl:number">
402
 
  <xsl:param name="format"/>
403
 
  <xslt:number>
404
 
    <xsl:if test="$format">
405
 
      <xsl:attribute name="format">
406
 
        <xsl:value-of select="$format"/>
407
 
      </xsl:attribute>
408
 
    </xsl:if>
409
 
    <xsl:for-each select="attribute::*">
410
 
      <xsl:if test="not(self::format and $format)">
411
 
        <xsl:copy/>
412
 
      </xsl:if>
413
 
    </xsl:for-each>
414
 
  </xslt:number>
415
 
</xsl:template>
416
 
 
417
 
<!-- == xsl:template == -->
418
 
<xsl:template match="xsl:template">
419
 
  <xslt:template name="{@name}">
420
 
    <xsl:apply-templates select="xsl:param"/>
421
 
    <xslt:param name="lang" select="$node/ancestor-or-self::*[@lang][1]/@lang"/>
422
 
    <xslt:variable name="lang_language">
423
 
      <xslt:call-template name="gettext.get.language">
424
 
        <xslt:with-param name="lang" select="$lang"/>
425
 
      </xslt:call-template>
426
 
    </xslt:variable>
427
 
    <xslt:variable name="lang_region">
428
 
      <xslt:call-template name="gettext.get.region">
429
 
        <xslt:with-param name="lang" select="$lang"/>
430
 
      </xslt:call-template>
431
 
    </xslt:variable>
432
 
    <xslt:variable name="lang_variant">
433
 
      <xslt:call-template name="gettext.get.variant">
434
 
        <xslt:with-param name="lang" select="$lang"/>
435
 
      </xslt:call-template>
436
 
    </xslt:variable>
437
 
    <xslt:variable name="lang_charset">
438
 
      <xslt:call-template name="gettext.get.charset">
439
 
        <xslt:with-param name="lang" select="$lang"/>
440
 
      </xslt:call-template>
441
 
    </xslt:variable>
442
 
    <xsl:apply-templates select="msg:msg"/>
443
 
  </xslt:template>
444
 
</xsl:template>
445
 
 
446
 
<!-- == xsl:stylesheet == -->
447
 
<xsl:template match="xsl:stylesheet">
448
 
  <xslt:stylesheet version="1.0">
449
 
    <xsl:apply-templates/>
450
 
  </xslt:stylesheet>
451
 
</xsl:template>
452
 
 
453
 
</xsl:stylesheet>