1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 """
29
30 """
31
32 from types import *
33 from ll.xist import xsc
34
36 """
37 Someday soon, this function will return a translation for unicode_string
38 suitable for context.
39
40 - If txt is a unicode string it will translate it and return a
41 xsc.Text instance to be used with xist
42 - If a xist DOM tree is supplied, it will recursively traverse it
43 and translate B{all} CharachterData instance's content that are not
44 marked with a __translated__ Flag.
45
46 """
47 if type(txt) == UnicodeType:
48
49
50 ret = xsc.Text(txt)
51 ret.__translated__ = True
52 return ret
53
54 elif type(txt) == StringType:
55 return translate(unicode(txt), context)
56
57 elif isinstance(txt, xsc.Text):
58 if getattr(txt, "__translated__", False):
59 return txt
60 else:
61 return translate(txt.content, context)
62
63 elif isinstance(txt, xsc.Frag):
64 for index, a in enumerate(txt):
65 txt[index] = translate(txt[index], context)
66
67 elif isinstance(txt, xsc.Element):
68 txt.content = translate(txt.content, context)
69
70 else:
71 tpl = ( repr(type(txt)), repr(txt), )
72 raise TypeError("translate can only operate on unicode strings, "+ \
73 "xsc.Element, or xsc.Frag, not %s %s" % tpl)
74
76 """
77 The i18n version of the % opperator: template must be a unicode string,
78 which will be translated (including the placeholders) and then applied to
79 the data passed as either positional arguments or key word arguments.
80
81 Example::
82
83 >>> translate_template(context, '%s %s', one, two)
84 '<content of one> <content of two>'
85 >>> translate_template(context, '%(one)s %(two)s', one='1', two='2')
86 '1 2'
87
88
89 """
90 if type(template) == UnicodeType: template = unicode(template)
91
92 if type(template) != UnicodeType:
93 raise TypeError("tramslate_template can only operate on unicode " + \
94 "strings, not %s %s" % ( repr(type(template)),
95 repr(template), ))
96 char_data = translate(template)
97
98 if args:
99 char_data.content = char_data.content % args
100 elif kw:
101 char_data.content = char_data.content % kw
102 else:
103 raise ValueError("You must either supply positional or key words " + \
104 "arguments for the % opperator to work on!")
105
106 return char_data
107
108 _ = translate
109
110
111
112
113
114