# HG changeset patch # User cmlenz # Date 1177924245 0 # Node ID 5eae4a5c42ac9908cf6c3334fe0b83287c3b15e2 # Parent 67cff2a666e2a295265df0017f0428e604f96e16 The I18n extraction now returns a tuple of strings for `ngettext` and similar functions. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -13,6 +13,10 @@ tuple for a given string. * Added frameset variants to the `DocType` constants for HTML 4.01 and XHTML 1.0. + * Improved I18n extraction for pluralizable messages: for any translation + function with multiple string arguments (such as ``ngettext``), a single + item with a tuple of strings is yielded, instead an item for each string + argument. Version 0.4 diff --git a/genshi/filters/i18n.py b/genshi/filters/i18n.py --- a/genshi/filters/i18n.py +++ b/genshi/filters/i18n.py @@ -174,7 +174,8 @@ * ``lineno`` is the number of the line on which the string was found, * ``function`` is the name of the ``gettext`` function used (if the string was extracted from embedded Python code), and - * ``message`` is the string itself (a ``unicode`` object). + * ``message`` is the string itself (a ``unicode`` object, or a tuple + of ``unicode`` objects for functions with multiple string arguments). >>> from genshi.template import MarkupTemplate >>> @@ -185,6 +186,7 @@ ... ...

Example

...

${_("Hello, %(name)s") % dict(name=username)}

+ ...

${ngettext("You have %d item", "You have %d items", num)}

... ... ''', filename='example.html') >>> @@ -193,12 +195,17 @@ 3, None, u'Example' 6, None, u'Example' 7, '_', u'Hello, %(name)s' - + 8, 'ngettext', (u'You have %d item', u'You have %d items') + :param stream: the event stream to extract strings from; can be a regular stream or a template stream :param gettext_functions: a sequence of function names that should be treated as gettext-style localization functions + + :note: Changed in 0.4.1: For a function with multiple string arguments + (such as ``ngettext``), a single item with a tuple of strings is + yielded, instead an item for each string argument. """ tagname = None skip = 0 @@ -261,8 +268,11 @@ if not isinstance(arg, basestring): break strings.append(unicode(arg)) - for string in strings: - yield pos[1], funcname, string + if len(strings) == 1: + strings = strings[0] + else: + strings = tuple(strings) + yield pos[1], funcname, strings elif kind is SUB: subkind, substream = data