Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions packages/cli/src/api/catalog/mergeCatalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,46 @@ describe("mergeCatalog", () => {
}
`)
})

describe("key order", () => {
// key order is what a formatter writes out, so it is what shows up in the
// catalog diff
const serializedKeys = (entry: unknown) =>
Object.keys(JSON.parse(JSON.stringify(entry)))

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</0> 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),
)
})
})
})
4 changes: 3 additions & 1 deletion packages/cli/src/api/catalog/mergeCatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ 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
const newMessages: CatalogType = Object.fromEntries(
newKeys.map((key) => [
key,
{
translation: forSourceLocale ? nextCatalog[key]!.message || key : "",
...nextCatalog[key],
translation: forSourceLocale ? nextCatalog[key]!.message || key : "",
},
]),
)
Expand Down
Loading