# HG changeset patch # User cmlenz # Date 1215536811 0 # Node ID 886934df7fea107e3f37eabfe924b23d531e1dc0 # Parent f459f22f7ad23daf99c34da7bd18c12c2dad0386 Support for parameters in internationalized `i18n:msg` content. See #129. diff --git a/genshi/filters/i18n.py b/genshi/filters/i18n.py --- a/genshi/filters/i18n.py +++ b/genshi/filters/i18n.py @@ -176,7 +176,10 @@ msgbuf.append(kind, data, pos) continue elif i18n_msg in attrs: - msgbuf = MessageBuffer() + params = attrs.get(i18n_msg) + if params and type(params) is list: # event tuple + params = params[0][1] + msgbuf = MessageBuffer(params) attrs -= i18n_msg yield kind, (tag, attrs), pos @@ -190,6 +193,9 @@ else: msgbuf.append(kind, data, pos) + elif msgbuf and kind is EXPR: + msgbuf.append(kind, data, pos) + elif not skip and msgbuf and kind is END: msgbuf.append(kind, data, pos) if not msgbuf.depth: @@ -297,7 +303,10 @@ if msgbuf: msgbuf.append(kind, data, pos) elif i18n_msg in attrs: - msgbuf = MessageBuffer(pos[1]) + params = attrs.get(i18n_msg) + if params and type(params) is list: # event tuple + params = params[0][1] + msgbuf = MessageBuffer(params, pos[1]) elif not skip and search_text and kind is TEXT: if not msgbuf: @@ -314,6 +323,8 @@ msgbuf = None elif kind is EXPR or kind is EXEC: + if msgbuf: + msgbuf.append(kind, data, pos) for funcname, strings in extract_from_code(data, gettext_functions): yield pos[1], funcname, strings @@ -333,15 +344,17 @@ :since: version 0.5 """ - def __init__(self, lineno=-1): + def __init__(self, params=u'', lineno=-1): """Initialize the message buffer. :param lineno: the line number on which the first stream event belonging to the message was found """ + self.params = [name.strip() for name in params.split(',')] self.lineno = lineno self.string = [] self.events = {} + self.values = {} self.depth = 1 self.order = 1 self.stack = [0] @@ -356,6 +369,11 @@ if kind is TEXT: self.string.append(data) self.events.setdefault(self.stack[-1], []).append(None) + elif kind is EXPR: + param = self.params.pop(0) + self.string.append('%%(%s)s' % param) + self.events.setdefault(self.stack[-1], []).append(None) + self.values[param] = (kind, data, pos) else: if kind is START: self.string.append(u'[%d:' % self.order) @@ -376,7 +394,7 @@ """ return u''.join(self.string).strip() - def translate(self, string): + def translate(self, string, regex=re.compile(r'%\((\w+)\)s')): """Interpolate the given message translation with the events in the buffer and return the translated stream. @@ -386,15 +404,19 @@ for order, string in parts: events = self.events[order] while events: - event = self.events[order].pop(0) - if not event: + event = events.pop(0) + if event: + yield event + else: if not string: break - yield TEXT, string, (None, -1, -1) + for idx, part in enumerate(regex.split(string)): + if idx % 2: + yield self.values[part] + elif part: + yield TEXT, part, (None, -1, -1) if not self.events[order] or not self.events[order][0]: break - else: - yield event def parse_msg(string, regex=re.compile(r'(?:\[(\d+)\:)|\]')):