feat(admin-*,dashboard): add dashboard i18n extensions (#13763)

* virtual i18n module

* changeset

* fallback ns

fallback to the default "translation" ns if the key isnt found. Allows to use a single "useTranslation("customNs")" hook for both custom and medusa-provided keys

* simplify merges

* optional for backward compat

* fix HMR

* fix generated deepMerge

* test
This commit is contained in:
Leonardo Benini
2025-10-23 21:16:43 +02:00
committed by GitHub
parent 012e30801e
commit 226984cf0f
25 changed files with 314 additions and 9 deletions

View File

@@ -2,3 +2,4 @@ export * from "./extensions/custom-fields"
export * from "./extensions/routes"
export * from "./extensions/widgets"
export * from "./virtual-modules"
export * from "./utils"

View File

@@ -0,0 +1,26 @@
import { isObject } from "./is-object"
export function deepMerge(target: any, source: any) {
const recursive = (a:any, b:any) => {
if (!isObject(a)) {
return b
}
if (!isObject(b)) {
return a
}
Object.keys(b).forEach((key) => {
if (isObject((b as any)[key])) {
(a as any)[key] ??= {};
(a as any)[key] = deepMerge((a as any)[key], (b as any)[key])
} else {
(a as any)[key] = (b as any)[key]
}
})
return a
}
const copy = { ...target }
return recursive(copy, source)
}

View File

@@ -0,0 +1 @@
export * from "./deep-merge"

View File

@@ -0,0 +1,3 @@
export function isObject(obj: any): obj is object {
return obj != null && obj?.constructor?.name === "Object"
}

View File

@@ -4,6 +4,7 @@ export const DISPLAY_VIRTUAL_MODULE = `virtual:medusa/displays`
export const ROUTE_VIRTUAL_MODULE = `virtual:medusa/routes`
export const MENU_ITEM_VIRTUAL_MODULE = `virtual:medusa/menu-items`
export const WIDGET_VIRTUAL_MODULE = `virtual:medusa/widgets`
export const I18N_VIRTUAL_MODULE = `virtual:medusa/i18n`
export const VIRTUAL_MODULES = [
LINK_VIRTUAL_MODULE,
@@ -12,4 +13,5 @@ export const VIRTUAL_MODULES = [
ROUTE_VIRTUAL_MODULE,
MENU_ITEM_VIRTUAL_MODULE,
WIDGET_VIRTUAL_MODULE,
I18N_VIRTUAL_MODULE,
] as const