feat(admin-i18n): add cookie-based locale runtime and switcher baseline
This commit is contained in:
27
apps/admin/src/i18n/messages.ts
Normal file
27
apps/admin/src/i18n/messages.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import type enMessages from "../messages/en.json"
|
||||
|
||||
export type AdminMessages = typeof enMessages
|
||||
|
||||
function resolveNestedValue(source: unknown, key: string): unknown {
|
||||
let current: unknown = source
|
||||
|
||||
for (const segment of key.split(".")) {
|
||||
if (!current || typeof current !== "object") {
|
||||
return null
|
||||
}
|
||||
|
||||
current = (current as Record<string, unknown>)[segment]
|
||||
}
|
||||
|
||||
return current
|
||||
}
|
||||
|
||||
export function translateMessage(messages: AdminMessages, key: string, fallback?: string): string {
|
||||
const resolved = resolveNestedValue(messages, key)
|
||||
|
||||
if (typeof resolved === "string") {
|
||||
return resolved
|
||||
}
|
||||
|
||||
return fallback ?? key
|
||||
}
|
||||
Reference in New Issue
Block a user