Ensure a better deterministic message order in the PO template
Marco Ricci

Marco Ricci commited on 2025-02-11 23:01:00
Zeige 1 geänderte Dateien mit 23 Einfügungen und 3 Löschungen.


Make the PO template message order deterministic: sort by message class
(labels, debug messages, info messages, warning messages, error
messages), then by lexicographically by context, then lexicographically
by enum name.  While the PO template was already deterministic in this
regard, the new message order is also the desired source code order.
... ...
@@ -2348,10 +2348,30 @@ def _write_po_file(  # noqa: C901,PLR0912
2348 2348
             'Language-Team': 'English',
2349 2349
         })
2350 2350
     print(*_format_po_info(po_info), sep='\n', end='\n', file=fileobj)
2351
-    for _ctx, subdict in sorted(entries.items()):
2351
+
2352
+    context_class = {
2353
+        'Label': Label,
2354
+        'Debug message': DebugMsgTemplate,
2355
+        'Info message': InfoMsgTemplate,
2356
+        'Warning message': WarnMsgTemplate,
2357
+        'Error message': ErrMsgTemplate,
2358
+    }
2359
+
2360
+    def _sort_position_msg_template_class(
2361
+        item: tuple[str, Any],
2362
+        /,
2363
+    ) -> tuple[int, str]:
2364
+        context_type = item[0].split(' :: ')[0]
2365
+        return (
2366
+            MSG_TEMPLATE_CLASSES.index(context_class[context_type]),
2367
+            item[0],
2368
+        )
2369
+
2370
+    for _ctx, subdict in sorted(
2371
+        entries.items(), key=_sort_position_msg_template_class
2372
+    ):
2352 2373
         for _msg, enum_value in sorted(
2353
-            subdict.items(),
2354
-            key=lambda kv: str(kv[1]),
2374
+            subdict.items(), key=lambda kv: str(kv[1])
2355 2375
         ):
2356 2376
             value = cast('TranslatableString', enum_value.value)
2357 2377
             value2 = value.maybe_without_filename()
2358 2378