From 4ae3def190c3e713403b902a62bf35d87ed0c189 Mon Sep 17 00:00:00 2001 From: thislight Date: Thu, 26 Sep 2024 19:42:44 +0800 Subject: [PATCH] i18n: add createTranslator --- src/platform/i18n.tsx | 9 +++++++-- src/settings/Settings.tsx | 9 ++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/platform/i18n.tsx b/src/platform/i18n.tsx index d291677..a43a574 100644 --- a/src/platform/i18n.tsx +++ b/src/platform/i18n.tsx @@ -2,7 +2,6 @@ import { ParentComponent, createContext, createMemo, - createRenderEffect, createResource, useContext, } from "solid-js"; @@ -12,7 +11,7 @@ import { $settings } from "../settings/stores"; import { enGB } from "date-fns/locale/en-GB"; import { useStore } from "@nanostores/solid"; import type { Locale } from "date-fns"; -import type { Template } from "@solid-primitives/i18n"; +import { resolveTemplate, translator, type Template } from "@solid-primitives/i18n"; async function synchronised( name: string, @@ -181,3 +180,9 @@ export function createStringResource< }, ); } + +export function createTranslator | undefined>>[],>(...importFns: T) { + const res = createStringResource(...importFns) + + return [translator(res[0], resolveTemplate), res] as const +} diff --git a/src/settings/Settings.tsx b/src/settings/Settings.tsx index 1b9605c..bc35b7d 100644 --- a/src/settings/Settings.tsx +++ b/src/settings/Settings.tsx @@ -27,33 +27,32 @@ import { Title } from "../material/typography.jsx"; import { css } from "solid-styled"; import { useSignedInProfiles } from "../masto/acct.js"; import { signOut, type Account } from "../accounts/stores.js"; -import { format, intlFormat } from "date-fns"; +import { format } from "date-fns"; import { useStore } from "@nanostores/solid"; import { $settings } from "./stores.js"; import { useRegisterSW } from "virtual:pwa-register/solid"; import { autoMatchLangTag, autoMatchRegion, - createStringResource, + createTranslator, SUPPORTED_LANGS, SUPPORTED_REGIONS, useDateFnLocale, } from "../platform/i18n.jsx"; -import { resolveTemplate, translator, type Template } from "@solid-primitives/i18n"; +import { type Template } from "@solid-primitives/i18n"; type Strings = { ["lang.auto"]: Template<{detected: string}> } & Record const Settings: ParentComponent = () => { - const [strings] = createStringResource( + const [t] = createTranslator( (code) => import(`./i18n/${code}.json`) as Promise<{ default: Strings; }>, () => import(`./i18n/lang-names.json`) ); - const t = translator(strings, resolveTemplate); const navigate = useNavigate(); const settings$ = useStore($settings); const {