97
97
d = frame.f_globals.copy()
98
98
d.update(frame.f_locals)
99
99
use_templates = d.get('__i18n_templates__', False)
100
# Translating the string returns an encoded 8-bit string. Rather than
101
# turn that into a Unicode, we turn any Unicodes in the dictionary values
102
# into encoded 8-bit strings. XXX: Returning a Unicode here broke too
103
# much other stuff and _() has many tentacles. Eventually I think we want
104
# to use Unicode everywhere.
105
tns = _translation.gettext(s)
106
charset = _translation.charset()
100
# Mailman must be unicode safe internally (i.e. all strings inside Mailman
101
# must be unicodes). The translation service is one boundary to the
102
# outside world, so to honor this constraint, make sure that all strings
103
# to come out of _() are unicodes, even if the translated string or
104
# dictionary values are 8-bit strings.
105
tns = _translation.ugettext(s)
106
charset = _translation.charset() or 'us-ascii'
109
107
for k, v in d.items():
110
if isinstance(v, unicode):
111
d[k] = v.encode(charset, 'replace')
108
if isinstance(v, str):
109
d[k] = unicode(v, charset, 'replace')
112
110
# Are we using $-strings or %-strings?
113
111
if use_templates:
114
return Template(tns).safe_substitute(attrdict(d))
116
tns = unicode(tns, charset)
117
return SafeDict(d, charset=charset).interpolate(tns)
112
translated_string = Template(tns).safe_substitute(attrdict(d))
114
translated_string = SafeDict(d, charset=charset).interpolate(tns)
115
if isinstance(translated_string, str):
116
translated_string = unicode(translated_string, charset)
117
return translated_string