wip(app): i18n
This commit is contained in:
39
packages/ui/src/context/i18n.tsx
Normal file
39
packages/ui/src/context/i18n.tsx
Normal file
@@ -0,0 +1,39 @@
|
||||
import { createContext, useContext, type Accessor, type ParentProps } from "solid-js"
|
||||
import { dict as en } from "../i18n/en"
|
||||
|
||||
export type UiI18nKey = keyof typeof en
|
||||
|
||||
export type UiI18nParams = Record<string, string | number | boolean | null | undefined>
|
||||
|
||||
export type UiI18n = {
|
||||
locale: Accessor<string>
|
||||
t: (key: UiI18nKey, params?: UiI18nParams) => string
|
||||
}
|
||||
|
||||
function resolveTemplate(text: string, params?: UiI18nParams) {
|
||||
if (!params) return text
|
||||
return text.replace(/{{\s*([^}]+?)\s*}}/g, (_, rawKey) => {
|
||||
const key = String(rawKey)
|
||||
const value = params[key]
|
||||
if (value === undefined || value === null) return ""
|
||||
return String(value)
|
||||
})
|
||||
}
|
||||
|
||||
const fallback: UiI18n = {
|
||||
locale: () => "en",
|
||||
t: (key, params) => {
|
||||
const value = en[key] ?? String(key)
|
||||
return resolveTemplate(value, params)
|
||||
},
|
||||
}
|
||||
|
||||
const Context = createContext<UiI18n>(fallback)
|
||||
|
||||
export function I18nProvider(props: ParentProps<{ value: UiI18n }>) {
|
||||
return <Context.Provider value={props.value}>{props.children}</Context.Provider>
|
||||
}
|
||||
|
||||
export function useI18n() {
|
||||
return useContext(Context)
|
||||
}
|
||||
@@ -2,3 +2,4 @@ export * from "./helper"
|
||||
export * from "./data"
|
||||
export * from "./diff"
|
||||
export * from "./dialog"
|
||||
export * from "./i18n"
|
||||
|
||||
Reference in New Issue
Block a user