From 1290a5d5c8f62069d93a9bbec3c7f98b57b36e3f Mon Sep 17 00:00:00 2001 From: Xavier Fernandez Date: Tue, 21 Mar 2017 16:36:56 +0100 Subject: [PATCH] When msgid are equal, sort according to msgctxt Fix #481 --- babel/messages/catalog.py | 4 ++-- tests/messages/test_pofile.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) 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)]) -- 2.47.2