From a1cac016297f6b02fe4285202b2ee60e9b4ed64c Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Fri, 18 Sep 2026 13:46:51 +0000 Subject: [PATCH 1/3] fix(cli): add translation as the last key of a new message A message added to a catalog on the first extract was built with `translation` first and the extracted fields spread after it, while the same message rebuilt from the previous catalog on any later extract puts `translation` last. The JSON formatter writes whatever key order it gets, so the second extract produced a diff that only moves the `translation` line to the end of every message added by the previous run. Build new messages with `translation` last, matching the merge path, so the key order of a message is stable across extract runs. Fixes #2671 --- .../cli/src/api/catalog/mergeCatalog.test.ts | 55 +++++++++++++++++++ packages/cli/src/api/catalog/mergeCatalog.ts | 5 +- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/api/catalog/mergeCatalog.test.ts b/packages/cli/src/api/catalog/mergeCatalog.test.ts index ec390c21a..7575cb13c 100644 --- a/packages/cli/src/api/catalog/mergeCatalog.test.ts +++ b/packages/cli/src/api/catalog/mergeCatalog.test.ts @@ -188,4 +188,59 @@ describe("mergeCatalog", () => { } `) }) + + describe("key order", () => { + // key order is what a formatter writes out, so it is what shows up in the + // catalog diff, @see https://github.com/lingui/js-lingui/issues/2671 + const serializedKeys = (entry: unknown) => + Object.keys(JSON.parse(JSON.stringify(entry))) + + const locales: [label: string, forSourceLocale: boolean][] = [ + ["for a translated locale", false], + ["for the source locale", true], + ] + + it.each(locales)( + "should add translation as the last key of a new message %s", + (_, forSourceLocale) => { + const result = mergeCatalog( + undefined, + nextCatalog, + forSourceLocale, + defaultMergeOptions, + ) + + expect(serializedKeys(result["custom.id"]).at(-1)).toBe("translation") + expect( + serializedKeys(result["Message with <0>auto-generated ID"]).at( + -1, + ), + ).toBe("translation") + }, + ) + + it.each(locales)( + "should keep the key order of a message stable between extract runs %s", + (_, forSourceLocale) => { + // first extract, both messages are new + const firstRun = mergeCatalog( + undefined, + nextCatalog, + forSourceLocale, + defaultMergeOptions, + ) + // second extract, the very same messages are merged from the catalog + const secondRun = mergeCatalog( + firstRun, + nextCatalog, + forSourceLocale, + defaultMergeOptions, + ) + + expect(JSON.stringify(secondRun, null, 2)).toEqual( + JSON.stringify(firstRun, null, 2), + ) + }, + ) + }) }) diff --git a/packages/cli/src/api/catalog/mergeCatalog.ts b/packages/cli/src/api/catalog/mergeCatalog.ts index 600542c50..b3b415aa3 100644 --- a/packages/cli/src/api/catalog/mergeCatalog.ts +++ b/packages/cli/src/api/catalog/mergeCatalog.ts @@ -31,12 +31,15 @@ export function mergeCatalog( } // Initialize new catalog with new keys + // `translation` is added last, so a message keeps the same key order once it + // is merged from the previous catalog on the next extract + // @see https://github.com/lingui/js-lingui/issues/2671 const newMessages: CatalogType = Object.fromEntries( newKeys.map((key) => [ key, { - translation: forSourceLocale ? nextCatalog[key]!.message || key : "", ...nextCatalog[key], + translation: forSourceLocale ? nextCatalog[key]!.message || key : "", }, ]), ) From a102cc39ae1a18eba897e81ad6a8c6f0f6998454 Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Mon, 21 Sep 2026 18:24:07 +0000 Subject: [PATCH 2/3] chore(cli): drop issue references from key order comments --- packages/cli/src/api/catalog/mergeCatalog.test.ts | 2 +- packages/cli/src/api/catalog/mergeCatalog.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/cli/src/api/catalog/mergeCatalog.test.ts b/packages/cli/src/api/catalog/mergeCatalog.test.ts index 7575cb13c..644460c07 100644 --- a/packages/cli/src/api/catalog/mergeCatalog.test.ts +++ b/packages/cli/src/api/catalog/mergeCatalog.test.ts @@ -191,7 +191,7 @@ describe("mergeCatalog", () => { describe("key order", () => { // key order is what a formatter writes out, so it is what shows up in the - // catalog diff, @see https://github.com/lingui/js-lingui/issues/2671 + // catalog diff const serializedKeys = (entry: unknown) => Object.keys(JSON.parse(JSON.stringify(entry))) diff --git a/packages/cli/src/api/catalog/mergeCatalog.ts b/packages/cli/src/api/catalog/mergeCatalog.ts index b3b415aa3..f71ebeccd 100644 --- a/packages/cli/src/api/catalog/mergeCatalog.ts +++ b/packages/cli/src/api/catalog/mergeCatalog.ts @@ -33,7 +33,6 @@ export function mergeCatalog( // Initialize new catalog with new keys // `translation` is added last, so a message keeps the same key order once it // is merged from the previous catalog on the next extract - // @see https://github.com/lingui/js-lingui/issues/2671 const newMessages: CatalogType = Object.fromEntries( newKeys.map((key) => [ key, From 0619eef19c687c08ca7d604bd266212dd30add46 Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Wed, 23 Sep 2026 01:08:39 +0000 Subject: [PATCH 3/3] test(cli): drop the locale axis from the key order tests --- .../cli/src/api/catalog/mergeCatalog.test.ts | 81 ++++++++----------- 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/packages/cli/src/api/catalog/mergeCatalog.test.ts b/packages/cli/src/api/catalog/mergeCatalog.test.ts index 644460c07..9d48262ff 100644 --- a/packages/cli/src/api/catalog/mergeCatalog.test.ts +++ b/packages/cli/src/api/catalog/mergeCatalog.test.ts @@ -195,52 +195,39 @@ describe("mergeCatalog", () => { const serializedKeys = (entry: unknown) => Object.keys(JSON.parse(JSON.stringify(entry))) - const locales: [label: string, forSourceLocale: boolean][] = [ - ["for a translated locale", false], - ["for the source locale", true], - ] - - it.each(locales)( - "should add translation as the last key of a new message %s", - (_, forSourceLocale) => { - const result = mergeCatalog( - undefined, - nextCatalog, - forSourceLocale, - defaultMergeOptions, - ) - - expect(serializedKeys(result["custom.id"]).at(-1)).toBe("translation") - expect( - serializedKeys(result["Message with <0>auto-generated ID"]).at( - -1, - ), - ).toBe("translation") - }, - ) - - it.each(locales)( - "should keep the key order of a message stable between extract runs %s", - (_, forSourceLocale) => { - // first extract, both messages are new - const firstRun = mergeCatalog( - undefined, - nextCatalog, - forSourceLocale, - defaultMergeOptions, - ) - // second extract, the very same messages are merged from the catalog - const secondRun = mergeCatalog( - firstRun, - nextCatalog, - forSourceLocale, - defaultMergeOptions, - ) - - expect(JSON.stringify(secondRun, null, 2)).toEqual( - JSON.stringify(firstRun, null, 2), - ) - }, - ) + it("should add translation as the last key of a new message", () => { + const result = mergeCatalog( + undefined, + nextCatalog, + false, + defaultMergeOptions, + ) + + expect(serializedKeys(result["custom.id"]).at(-1)).toBe("translation") + expect( + serializedKeys(result["Message with <0>auto-generated ID"]).at(-1), + ).toBe("translation") + }) + + it("should keep the key order of a message stable between extract runs", () => { + // first extract, both messages are new + const firstRun = mergeCatalog( + undefined, + nextCatalog, + false, + defaultMergeOptions, + ) + // second extract, the very same messages are merged from the catalog + const secondRun = mergeCatalog( + firstRun, + nextCatalog, + false, + defaultMergeOptions, + ) + + expect(JSON.stringify(secondRun, null, 2)).toEqual( + JSON.stringify(firstRun, null, 2), + ) + }) }) })