From ef36af0e55d814dc80893af923886ccff40f46e5 Mon Sep 17 00:00:00 2001 From: Adam <2363879+adamdotdevin@users.noreply.github.com> Date: Tue, 20 Jan 2026 11:27:32 -0600 Subject: wip(app): i18n --- packages/ui/src/context/i18n.tsx | 39 +++++++++++++++++++++++++++++++++++++++ packages/ui/src/context/index.ts | 1 + 2 files changed, 40 insertions(+) create mode 100644 packages/ui/src/context/i18n.tsx (limited to 'packages/ui/src/context') diff --git a/packages/ui/src/context/i18n.tsx b/packages/ui/src/context/i18n.tsx new file mode 100644 index 000000000..fd8b05d3c --- /dev/null +++ b/packages/ui/src/context/i18n.tsx @@ -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 + +export type UiI18n = { + locale: Accessor + 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(fallback) + +export function I18nProvider(props: ParentProps<{ value: UiI18n }>) { + return {props.children} +} + +export function useI18n() { + return useContext(Context) +} diff --git a/packages/ui/src/context/index.ts b/packages/ui/src/context/index.ts index 499cb74d4..5615dd0ec 100644 --- a/packages/ui/src/context/index.ts +++ b/packages/ui/src/context/index.ts @@ -2,3 +2,4 @@ export * from "./helper" export * from "./data" export * from "./diff" export * from "./dialog" +export * from "./i18n" -- cgit v1.2.3