From: Xavier Fernandez Date: Tue, 21 Mar 2017 15:36:56 +0000 (+0100) Subject: When msgid are equal, sort according to msgctxt X-Git-Tag: v2.5.0~11^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F482%2Fhead;p=thirdparty%2Fbabel.git When msgid are equal, sort according to msgctxt Fix #481 --- diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index 193b72bc..3145fa37 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -120,8 +120,8 @@ class Message(object): """Compare Messages, taking into account plural ids""" def values_to_compare(obj): if isinstance(obj, Message) and obj.pluralizable: - return obj.id[0] - return obj.id + return (obj.id[0], obj.context or '') + return (obj.id, obj.context or '') return cmp(values_to_compare(self), values_to_compare(other)) def __gt__(self, other): diff --git a/tests/messages/test_pofile.py b/tests/messages/test_pofile.py index cef69138..ebf9702d 100644 --- a/tests/messages/test_pofile.py +++ b/tests/messages/test_pofile.py @@ -680,6 +680,41 @@ msgstr[0] "Voh" msgstr[1] "Voeh"''' in value assert value.find(b'msgid ""') < value.find(b'msgid "bar"') < value.find(b'msgid "foo"') + def test_sorted_po_context(self): + catalog = Catalog() + catalog.add((u'foo', u'foos'), (u'Voh', u'Voeh'), + locations=[('main.py', 1)], + context='there') + catalog.add((u'foo', u'foos'), (u'Voh', u'Voeh'), + locations=[('main.py', 1)]) + catalog.add((u'foo', u'foos'), (u'Voh', u'Voeh'), + locations=[('main.py', 1)], + context='here') + buf = BytesIO() + pofile.write_po(buf, catalog, sort_output=True) + value = buf.getvalue().strip() + # We expect the foo without ctx, followed by "here" foo and "there" foo + assert b'''\ +#: main.py:1 +msgid "foo" +msgid_plural "foos" +msgstr[0] "Voh" +msgstr[1] "Voeh" + +#: main.py:1 +msgctxt "here" +msgid "foo" +msgid_plural "foos" +msgstr[0] "Voh" +msgstr[1] "Voeh" + +#: main.py:1 +msgctxt "there" +msgid "foo" +msgid_plural "foos" +msgstr[0] "Voh" +msgstr[1] "Voeh"''' in value + def test_file_sorted_po(self): catalog = Catalog() catalog.add(u'bar', locations=[('utils.py', 3)])