-
Notifications
You must be signed in to change notification settings - Fork 59
feat(menu): support entry localization #1403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
f65288e
22d1477
ecd6bfd
d8c4fb3
a06ffbb
83efba0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,39 @@ | ||||||||||
| import { TABS } from "./tabs.js"; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * All localizable menu strings, derived from tabs.js. | ||||||||||
| * Used by the l10n extractor only — not imported at runtime. | ||||||||||
| * | ||||||||||
| * @type {Map<string, string>} | ||||||||||
| */ | ||||||||||
| export const strings = new Map( | ||||||||||
| TABS.flatMap((tab) => { | ||||||||||
| /** @type {[string, string][]} */ | ||||||||||
| const entries = []; | ||||||||||
|
|
||||||||||
| if (typeof tab.buttonL10nId === "string") { | ||||||||||
| entries.push([tab.buttonL10nId, /** @type {string} */ (tab.buttonText)]); | ||||||||||
| } else { | ||||||||||
| entries.push([tab.buttonL10nId.long, tab.buttonText.long], [tab.buttonL10nId.short, tab.buttonText.short]); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (!("panelTitle" in tab)) return entries; | ||||||||||
|
|
||||||||||
| entries.push([tab.panelTitle.l10nId, tab.panelTitle.text]); | ||||||||||
|
|
||||||||||
| for (const group of tab.panelGroups) { | ||||||||||
| if (group.titleL10nId) { | ||||||||||
| entries.push([group.titleL10nId, /** @type {string} */ (group.title)]); | ||||||||||
|
Comment on lines
+31
to
+32
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| } | ||||||||||
| for (const item of group.items) { | ||||||||||
| if (!("l10nId" in item)) continue; | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: similarly, a non-negated if would read better imo |
||||||||||
| entries.push([item.l10nId, item.text]); | ||||||||||
| if (item.labelL10nId) { | ||||||||||
| entries.push([item.labelL10nId, /** @type {string} */ (item.label)]); | ||||||||||
|
Comment on lines
+37
to
+38
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return entries; | ||||||||||
| }), | ||||||||||
| ); | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import tabsData from "./tabs.json" with { type: "json" }; | ||
|
|
||
| /** | ||
| * Converts a string to a kebab-case slug for use in l10n IDs. | ||
| * @param {string} str | ||
| */ | ||
| const slugify = (str) => | ||
| str | ||
| .toLowerCase() | ||
| .replaceAll(/[^\p{L}\p{N}]+/gu, "-") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if fluent ids support non-ascii characters, perhaps simplify to |
||
| .replaceAll(/^-|-$/g, ""); | ||
|
|
||
| /** | ||
| * Menu tabs enriched with l10n IDs. | ||
| * | ||
| * @type {ReadonlyArray<import("./types.js").MenuTab>} | ||
| */ | ||
| export const TABS = Object.freeze( | ||
| /** @type {import("./types.js").MenuTab[]} */ ( | ||
| tabsData.map((tab) => { | ||
| const buttonL10nId = | ||
| typeof tab.buttonText === "string" | ||
| ? `menu-${tab.id}` | ||
| : { | ||
| long: `menu-${tab.id}`, | ||
| short: `menu-${tab.id}-short`, | ||
| }; | ||
|
|
||
| if (!("panelTitle" in tab)) { | ||
| return { ...tab, buttonL10nId }; | ||
| } | ||
|
|
||
| return { | ||
| ...tab, | ||
| buttonL10nId, | ||
| panelTitle: { | ||
| ...tab.panelTitle, | ||
| l10nId: `menu-${tab.id}-panel-title`, | ||
| }, | ||
| panelGroups: tab.panelGroups.map((group, gi) => { | ||
| const groupSlug = group.title ? slugify(group.title) : String(gi); | ||
| return { | ||
| ...group, | ||
| titleL10nId: group.title | ||
| ? `menu-${tab.id}-${groupSlug}` | ||
| : undefined, | ||
| items: group.items.map((item) => { | ||
| if ("render" in item) return item; | ||
| const l10nId = `menu-${tab.id}-${groupSlug}-${slugify(item.text)}`; | ||
| return { | ||
| ...item, | ||
| l10nId, | ||
| labelL10nId: item.label ? `${l10nId}-label` : undefined, | ||
| }; | ||
| }), | ||
| }; | ||
| }), | ||
| }; | ||
| }) | ||
| ), | ||
| ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd prefer an
if("panelTitle" in tab) {block wrapping the below lines, than a negated condition - makes the code read easier IMO