Repository navigation
fix(seed): menu item i18n - #943
Conversation
… adding ID, locale, and translation tracking.
🦋 Changeset detectedLatest commit: fcf7fc5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/blocks
@emdash-cms/cloudflare
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
944f674 to
fcf7fc5
Compare
|
/review |
There was a problem hiding this comment.
Solid work. The i18n round-trip for menu items follows the existing pattern for taxonomies/menus closely, the export/apply tests cover the happy path and the missing-anchor fallback, and the shared itemSeedIdMap plus stable seed-id scheme look correct. The DB-id collision fallback for duplicate sibling labels is a nice touch.
A few non-blocking observations below — mostly around an inaccurate comment, a coverage gap on nested translated items, and a minor correctness concern about the implicit assumption that the anchor locale sorts alphabetically first.
| }); | ||
|
|
||
| // Sibling order is preserved (maps to sort_order on import). Cross-menu | ||
| // `translationOf` already resolves because exportMenus sorts anchors first. |
There was a problem hiding this comment.
This comment is a little misleading. exportMenus does a post-hoc result.sort(...) on line 364 after the loop, so the sort doesn't influence processing order — items have already been emitted by the time the sort runs.
The real reason cross-menu translationOf resolves is that menus are queried with .orderBy(["name", "locale"]) (line 315). For the typical case where the anchor menu's locale (often en) sorts before localized siblings (es, fr, ja), the anchor's items get registered in itemGroupToSeedId first. But this isn't strictly guaranteed — if a site uses de and en in the same translation_group with en as the conceptual anchor, de would be processed first alphabetically and become the recorded anchor in the seed.
That's a pre-existing quirk for menus/taxonomies (not introduced here), and round-trip still works because whatever the export picks as anchor, apply will accept. Worth either rephrasing the comment to point at orderBy(["name", "locale"]) as the real mechanism, or noting that "anchor" here just means "first-seen", not "the default-locale row":
| // `translationOf` already resolves because exportMenus sorts anchors first. | |
| // Sibling order is preserved (maps to sort_order on import). Cross-menu | |
| // `translationOf` resolves because exportMenus iterates menus ordered | |
| // by (name, locale), so the first-seen menu in each translation_group | |
| // becomes the anchor and its items register in `itemGroupToSeedId` | |
| // before later locales reference them. |
| if (item.translationOf) { | ||
| const source = itemSeedIdMap.get(item.translationOf); | ||
| if (source) translationGroup = source.translationGroup; | ||
| else |
There was a problem hiding this comment.
The new test falls back to fresh group when item translationOf is missing triggers this console.warn, which means test output gets a (single) noisy warning line. Not blocking, but worth either spying on console.warn to assert the warning fires (better — it's part of the documented behaviour) or stubbing it to silence noise. The taxonomy tests have the same pattern, so consistency would say leave it; but explicit assertion would tighten coverage.
| id: esMenuId, | ||
| name: "primary", | ||
| label: "Principal", | ||
| locale: "es", |
There was a problem hiding this comment.
Nice coverage for top-level translated items, anchor resolution, and the collision fallback. One coverage gap worth considering: nested items with their own translationOf aren't exercised in either this test or the apply test. applyMenuItems recurses with the shared itemSeedIdMap (apply.ts:1006), and buildLevel recurses on children inheriting i18nCtx, so the path is reachable but currently untested. A test where an EN parent item with EN child gets translated to ES (parent + child both with translationOf) would lock that down.
|
|
||
| function makeSeedId(item: (typeof items)[number]): string { | ||
| const base = slugify(item.label || "") || item.id; | ||
| const locale = i18nCtx.i18nEnabled ? (item.locale ?? i18nCtx.menuLocale) : null; |
There was a problem hiding this comment.
Minor: when slugify(item.label) returns an empty string (e.g. label is entirely non-Latin like الرئيسية — slugify strips diacritics but not Arabic/CJK code points, so the regex on line 4 of slugify.ts wipes them all), base falls back to item.id (a ULID). The resulting seed id then looks like item:primary:01HXYZABC...:ar, which works correctly but loses the human-readable property the item:menu:slug:locale scheme is going for. Not a correctness bug — just noting that for non-Latin-script-only sites the seed file won't be as legible. Could be worth a Unicode-aware slugify follow-up at some point, but not for this PR.
|
Review posted successfully. Summary of feedback:
Overall the PR looks correct and well-aligned with the existing taxonomy/menu i18n pattern. |
What does this PR do?
Extends seed and locale support to menus and taxonomies to ensure a complete i18n round-trip, addressing the missing fields and locale-aware existence checks identified during the initial i18n implementation.
Key changes:
SeedMenuItemi18n fields: Added optionalid,locale, andtranslationOftoSeedMenuItemso per-item translation links survive export → apply, mirroring the pattern already used forSeedContentEntry,SeedTaxonomy, andSeedTaxonomyTerm. Previously each item got a freshtranslation_groupon re-apply, breaking cross-locale "same nav entry" links.buildMenuItemTree(incli/commands/export-seed.ts) now emits stable seed-local IDs (item:{menuName}:{slug(label)}:{locale}, with a DB-id fallback on label collisions) and atranslationOfreference back to the anchor menu's item. The map oftranslation_group → seed idis shared across menus so translated items resolve their anchor correctly.applyMenuItemsaccepts anitemSeedIdMapshared across all menus in the seed. When an item carriestranslationOf, the import resolves it to the anchor'stranslation_group; when missing it falls back to a fresh group with a warning, matching the existing taxonomy/menu behaviour.tests/unit/seed/apply.test.tsadds two i18n round-trip cases for menu items, and a newtests/unit/seed/export-menu-items-i18n.test.tsasserts the export side (stable IDs, anchor/translation linkage, collision fallback, and the non-i18n shape).exportSeedfunction incli/commands/export-seed.tsis now exported. This is the function the CLI command already invokes; no behaviour change. Same shape as the already-publicapplySeed.translation_group, aligning with the content translation pattern.Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpassespnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.AI-generated code disclosure