feat(admin-*,dashboard): i18n labels for menu item extensions (#13843)
* i18n menu item labels * changeset * changeset
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/admin-vite-plugin": patch
|
||||||
|
"@medusajs/admin-sdk": patch
|
||||||
|
"@medusajs/dashboard": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(admin-\*,dashboard): i18n labels for menu item extensions
|
||||||
@@ -30,6 +30,17 @@ export interface RouteConfig {
|
|||||||
* The nested route to display under existing route in the sidebar.
|
* The nested route to display under existing route in the sidebar.
|
||||||
*/
|
*/
|
||||||
nested?: NestedRoutePosition
|
nested?: NestedRoutePosition
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An optional i18n namespace for translating the label. When provided, the label will be treated as a translation key.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* label: "menuItems.customFeature"
|
||||||
|
* translationNs: "my-plugin"
|
||||||
|
* // Will translate using: t("menuItems.customFeature", { ns: "my-plugin" })
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
translationNs?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CustomFormField<
|
export type CustomFormField<
|
||||||
|
|||||||
@@ -69,19 +69,22 @@ const expectedMenuItems = `
|
|||||||
label: RouteConfig0.label,
|
label: RouteConfig0.label,
|
||||||
icon: RouteConfig0.icon,
|
icon: RouteConfig0.icon,
|
||||||
path: "/one",
|
path: "/one",
|
||||||
nested: undefined
|
nested: undefined,
|
||||||
|
translationNs: undefined
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: RouteConfig1.label,
|
label: RouteConfig1.label,
|
||||||
icon: undefined,
|
icon: undefined,
|
||||||
path: "/two",
|
path: "/two",
|
||||||
nested: undefined
|
nested: undefined,
|
||||||
|
translationNs: undefined
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: RouteConfig2.label,
|
label: RouteConfig2.label,
|
||||||
icon: RouteConfig2.icon,
|
icon: RouteConfig2.icon,
|
||||||
path: "/three",
|
path: "/three",
|
||||||
nested: "/products"
|
nested: "/products",
|
||||||
|
translationNs: undefined
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
`
|
`
|
||||||
@@ -137,4 +140,49 @@ describe("generateMenuItems", () => {
|
|||||||
utils.normalizeString(expectedMenuItems)
|
utils.normalizeString(expectedMenuItems)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should handle translationNs field", async () => {
|
||||||
|
const mockFileWithTranslation = `
|
||||||
|
import { defineRouteConfig } from "@medusajs/admin-sdk"
|
||||||
|
|
||||||
|
const Page = () => {
|
||||||
|
return <div>Custom Page</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
export const config = defineRouteConfig({
|
||||||
|
label: "menuItems.customFeature",
|
||||||
|
translationNs: "my-plugin",
|
||||||
|
})
|
||||||
|
|
||||||
|
export default Page
|
||||||
|
`
|
||||||
|
|
||||||
|
const mockFiles = ["Users/user/medusa/src/admin/routes/custom/page.tsx"]
|
||||||
|
vi.mocked(utils.crawl).mockResolvedValue(mockFiles)
|
||||||
|
vi.mocked(fs.readFile).mockResolvedValue(mockFileWithTranslation)
|
||||||
|
|
||||||
|
const result = await generateMenuItems(
|
||||||
|
new Set(["Users/user/medusa/src/admin"])
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(result.imports).toEqual([
|
||||||
|
`import { config as RouteConfig0 } from "Users/user/medusa/src/admin/routes/custom/page.tsx"`,
|
||||||
|
])
|
||||||
|
|
||||||
|
const expectedOutput = `
|
||||||
|
menuItems: [
|
||||||
|
{
|
||||||
|
label: RouteConfig0.label,
|
||||||
|
icon: undefined,
|
||||||
|
path: "/custom",
|
||||||
|
nested: undefined,
|
||||||
|
translationNs: RouteConfig0.translationNs
|
||||||
|
}
|
||||||
|
]
|
||||||
|
`
|
||||||
|
|
||||||
|
expect(utils.normalizeString(result.code)).toEqual(
|
||||||
|
utils.normalizeString(expectedOutput)
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ type RouteConfig = {
|
|||||||
label: boolean
|
label: boolean
|
||||||
icon: boolean
|
icon: boolean
|
||||||
nested?: string
|
nested?: string
|
||||||
|
translationNs?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type MenuItem = {
|
type MenuItem = {
|
||||||
@@ -35,6 +36,7 @@ type MenuItem = {
|
|||||||
label: string
|
label: string
|
||||||
path: string
|
path: string
|
||||||
nested?: string
|
nested?: string
|
||||||
|
translationNs?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type MenuItemResult = {
|
type MenuItemResult = {
|
||||||
@@ -64,12 +66,13 @@ function generateCode(results: MenuItemResult[]): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function formatMenuItem(route: MenuItem): string {
|
function formatMenuItem(route: MenuItem): string {
|
||||||
const { label, icon, path, nested } = route
|
const { label, icon, path, nested, translationNs } = route
|
||||||
return `{
|
return `{
|
||||||
label: ${label},
|
label: ${label},
|
||||||
icon: ${icon || "undefined"},
|
icon: ${icon || "undefined"},
|
||||||
path: "${path}",
|
path: "${path}",
|
||||||
nested: ${nested ? `"${nested}"` : "undefined"}
|
nested: ${nested ? `"${nested}"` : "undefined"},
|
||||||
|
translationNs: ${translationNs ? `${translationNs}` : "undefined"}
|
||||||
}`
|
}`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,6 +133,7 @@ function generateMenuItem(
|
|||||||
icon: config.icon ? `${configName}.icon` : undefined,
|
icon: config.icon ? `${configName}.icon` : undefined,
|
||||||
path: getRoute(file),
|
path: getRoute(file),
|
||||||
nested: config.nested,
|
nested: config.nested,
|
||||||
|
translationNs: config.translationNs ? `${configName}.translationNs` : undefined,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,10 +244,22 @@ function processConfigProperties(
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const translationNs = properties.find(
|
||||||
|
(prop) =>
|
||||||
|
isObjectProperty(prop) && isIdentifier(prop.key, { name: "translationNs" })
|
||||||
|
) as ObjectProperty | undefined
|
||||||
|
|
||||||
|
let translationNsValue: string | undefined = undefined
|
||||||
|
|
||||||
|
if (isStringLiteral(translationNs?.value)) {
|
||||||
|
translationNsValue = translationNs.value.value
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
label: hasLabel,
|
label: hasLabel,
|
||||||
icon: hasProperty("icon"),
|
icon: hasProperty("icon"),
|
||||||
nested: nestedValue,
|
nested: nestedValue,
|
||||||
|
translationNs: translationNsValue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -348,6 +348,7 @@ const ExtensionRouteSection = () => {
|
|||||||
label={item.label}
|
label={item.label}
|
||||||
icon={item.icon ? item.icon : <SquaresPlus />}
|
icon={item.icon ? item.icon : <SquaresPlus />}
|
||||||
items={item.items}
|
items={item.items}
|
||||||
|
translationNs={item.translationNs}
|
||||||
type="extension"
|
type="extension"
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ type ItemType = "core" | "extension" | "setting"
|
|||||||
type NestedItemProps = {
|
type NestedItemProps = {
|
||||||
label: string
|
label: string
|
||||||
to: string
|
to: string
|
||||||
|
translationNs?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type INavItem = {
|
export type INavItem = {
|
||||||
@@ -27,6 +28,7 @@ export type INavItem = {
|
|||||||
type?: ItemType
|
type?: ItemType
|
||||||
from?: string
|
from?: string
|
||||||
nested?: string
|
nested?: string
|
||||||
|
translationNs?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const BASE_NAV_LINK_CLASSES =
|
const BASE_NAV_LINK_CLASSES =
|
||||||
@@ -90,10 +92,15 @@ export const NavItem = ({
|
|||||||
items,
|
items,
|
||||||
type = "core",
|
type = "core",
|
||||||
from,
|
from,
|
||||||
|
translationNs,
|
||||||
}: INavItem) => {
|
}: INavItem) => {
|
||||||
|
const { t } = useTranslation(translationNs as any)
|
||||||
const { pathname } = useLocation()
|
const { pathname } = useLocation()
|
||||||
const [open, setOpen] = useState(getIsOpen(to, items, pathname))
|
const [open, setOpen] = useState(getIsOpen(to, items, pathname))
|
||||||
|
|
||||||
|
// Use translation if translationNs is provided, otherwise use label as-is
|
||||||
|
const displayLabel: string = translationNs ? t(label) : label
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setOpen(getIsOpen(to, items, pathname))
|
setOpen(getIsOpen(to, items, pathname))
|
||||||
}, [pathname, to, items])
|
}, [pathname, to, items])
|
||||||
@@ -150,7 +157,7 @@ export const NavItem = ({
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<Text size="small" weight="plus" leading="compact">
|
<Text size="small" weight="plus" leading="compact">
|
||||||
{label}
|
{displayLabel}
|
||||||
</Text>
|
</Text>
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItemTooltip>
|
</NavItemTooltip>
|
||||||
@@ -166,7 +173,7 @@ export const NavItem = ({
|
|||||||
<Icon icon={icon} type={type} />
|
<Icon icon={icon} type={type} />
|
||||||
</div>
|
</div>
|
||||||
<Text size="small" weight="plus" leading="compact">
|
<Text size="small" weight="plus" leading="compact">
|
||||||
{label}
|
{displayLabel}
|
||||||
</Text>
|
</Text>
|
||||||
</RadixCollapsible.Trigger>
|
</RadixCollapsible.Trigger>
|
||||||
<RadixCollapsible.Content>
|
<RadixCollapsible.Content>
|
||||||
@@ -189,12 +196,15 @@ export const NavItem = ({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Text size="small" weight="plus" leading="compact">
|
<Text size="small" weight="plus" leading="compact">
|
||||||
{label}
|
{displayLabel}
|
||||||
</Text>
|
</Text>
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItemTooltip>
|
</NavItemTooltip>
|
||||||
</li>
|
</li>
|
||||||
{items.map((item) => {
|
{items.map((item) => {
|
||||||
|
const { t: itemT } = useTranslation(item.translationNs as any)
|
||||||
|
const itemLabel: string = item.translationNs ? itemT(item.label) : item.label
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li key={item.to} className="flex h-7 items-center">
|
<li key={item.to} className="flex h-7 items-center">
|
||||||
<NavItemTooltip to={item.to}>
|
<NavItemTooltip to={item.to}>
|
||||||
@@ -213,7 +223,7 @@ export const NavItem = ({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Text size="small" weight="plus" leading="compact">
|
<Text size="small" weight="plus" leading="compact">
|
||||||
{item.label}
|
{itemLabel}
|
||||||
</Text>
|
</Text>
|
||||||
</NavLink>
|
</NavLink>
|
||||||
</NavItemTooltip>
|
</NavItemTooltip>
|
||||||
|
|||||||
@@ -187,6 +187,7 @@ export class DashboardApp {
|
|||||||
icon: item.icon ? <item.icon /> : undefined,
|
icon: item.icon ? <item.icon /> : undefined,
|
||||||
items: [],
|
items: [],
|
||||||
nested: item.nested,
|
nested: item.nested,
|
||||||
|
translationNs: item.translationNs,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parentPath !== "/" && tempRegistry[parentPath]) {
|
if (parentPath !== "/" && tempRegistry[parentPath]) {
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ export type MenuItemExtension = {
|
|||||||
path: string
|
path: string
|
||||||
icon?: ComponentType
|
icon?: ComponentType
|
||||||
nested?: NestedRoutePosition
|
nested?: NestedRoutePosition
|
||||||
|
translationNs?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type WidgetExtension = {
|
export type WidgetExtension = {
|
||||||
|
|||||||
Reference in New Issue
Block a user