Skip to content

fix(seed): menu item i18n - #943

Merged
ascorbic merged 2 commits into
emdash-cms:mainfrom
Rimander:fix/seed-menu-item-i18n
May 8, 2026
Merged

ascorbic merged 2 commits into
emdash-cms:mainfrom
Rimander:fix/seed-menu-item-i18n

Conversation

@Rimander

@Rimander Rimander commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • SeedMenuItem i18n fields: Added optional id, locale, and translationOf to SeedMenuItem so per-item translation links survive export → apply, mirroring the pattern already used for SeedContentEntry, SeedTaxonomy, and SeedTaxonomyTerm. Previously each item got a fresh translation_group on re-apply, breaking cross-locale "same nav entry" links.
  • Export wiring: buildMenuItemTree (in cli/commands/export-seed.ts) now emits stable seed-local IDs (item:{menuName}:{slug(label)}:{locale}, with a DB-id fallback on label collisions) and a translationOf reference back to the anchor menu's item. The map of translation_group → seed id is shared across menus so translated items resolve their anchor correctly.
  • Apply wiring: applyMenuItems accepts an itemSeedIdMap shared across all menus in the seed. When an item carries translationOf, the import resolves it to the anchor's translation_group; when missing it falls back to a fresh group with a warning, matching the existing taxonomy/menu behaviour.
  • Tests: New unit coverage on both sides — tests/unit/seed/apply.test.ts adds two i18n round-trip cases for menu items, and a new tests/unit/seed/export-menu-items-i18n.test.ts asserts the export side (stable IDs, anchor/translation linkage, collision fallback, and the non-i18n shape).
  • Minor API exposure: To enable the export-side tests, the previously CLI-internal exportSeed function in cli/commands/export-seed.ts is now exported. This is the function the CLI command already invokes; no behaviour change. Same shape as the already-public applySeed.
  • Menu Cloning: Cloned menu items now correctly inherit the source item's translation_group, aligning with the content translation pattern.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude 4.7 Opus

@changeset-bot

changeset-bot Bot commented May 7, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fcf7fc5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

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

@pkg-pr-new

pkg-pr-new Bot commented May 7, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@943

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@943

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@943

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@943

emdash

npm i https://pkg.pr.new/emdash@943

create-emdash

npm i https://pkg.pr.new/create-emdash@943

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@943

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@943

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@943

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@943

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@943

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@943

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@943

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@943

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@943

commit: fcf7fc5

@Rimander
Rimander force-pushed the fix/seed-menu-item-i18n branch from 944f674 to fcf7fc5 Compare May 7, 2026 22:05
@ascorbic

ascorbic commented May 8, 2026

Copy link
Copy Markdown
Member

/review

@ask-bonk ask-bonk Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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":

Suggested change
// `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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ask-bonk

ask-bonk Bot commented May 8, 2026

Copy link
Copy Markdown
Contributor

Review posted successfully. Summary of feedback:

  • Comment accuracy: The comment in buildMenuItemTree claiming "exportMenus sorts anchors first" is misleading — the post-hoc result.sort() doesn't influence processing order. Suggested rephrase pointing at orderBy(["name", "locale"]) as the actual mechanism.
  • Test noise/coverage: The new "missing translationOf" apply test triggers a console.warn. Worth either silencing it or asserting on it.
  • Coverage gap: Nested translated items aren't covered in either test file, even though the code path is reachable.
  • Non-Latin labels: When slugify(label) returns empty (e.g. Arabic/CJK-only labels), seed IDs fall back to ULIDs, losing the human-readable property. Not a bug, just a follow-up consideration.

Overall the PR looks correct and well-aligned with the existing taxonomy/menu i18n pattern.

github run

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good!

@ascorbic
ascorbic merged commit 514d32d into emdash-cms:main May 8, 2026
38 checks passed
@emdashbot emdashbot Bot mentioned this pull request May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants