i18n: add createTranslator

This commit is contained in:
thislight 2024-09-26 19:42:44 +08:00
parent c49ae6fe0a
commit 55705b0a6d
No known key found for this signature in database
GPG key ID: A50F9451AC56A63E
2 changed files with 11 additions and 7 deletions

View file

@ -2,7 +2,6 @@ import {
ParentComponent, ParentComponent,
createContext, createContext,
createMemo, createMemo,
createRenderEffect,
createResource, createResource,
useContext, useContext,
} from "solid-js"; } from "solid-js";
@ -12,7 +11,7 @@ import { $settings } from "../settings/stores";
import { enGB } from "date-fns/locale/en-GB"; import { enGB } from "date-fns/locale/en-GB";
import { useStore } from "@nanostores/solid"; import { useStore } from "@nanostores/solid";
import type { Locale } from "date-fns"; 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( async function synchronised(
name: string, name: string,
@ -181,3 +180,9 @@ export function createStringResource<
}, },
); );
} }
export function createTranslator<T extends ImportFn<Record<string, string | Template<any> | undefined>>[],>(...importFns: T) {
const res = createStringResource(...importFns)
return [translator(res[0], resolveTemplate), res] as const
}

View file

@ -27,33 +27,32 @@ import { Title } from "../material/typography.jsx";
import { css } from "solid-styled"; import { css } from "solid-styled";
import { useSignedInProfiles } from "../masto/acct.js"; import { useSignedInProfiles } from "../masto/acct.js";
import { signOut, type Account } from "../accounts/stores.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 { useStore } from "@nanostores/solid";
import { $settings } from "./stores.js"; import { $settings } from "./stores.js";
import { useRegisterSW } from "virtual:pwa-register/solid"; import { useRegisterSW } from "virtual:pwa-register/solid";
import { import {
autoMatchLangTag, autoMatchLangTag,
autoMatchRegion, autoMatchRegion,
createStringResource, createTranslator,
SUPPORTED_LANGS, SUPPORTED_LANGS,
SUPPORTED_REGIONS, SUPPORTED_REGIONS,
useDateFnLocale, useDateFnLocale,
} from "../platform/i18n.jsx"; } from "../platform/i18n.jsx";
import { resolveTemplate, translator, type Template } from "@solid-primitives/i18n"; import { type Template } from "@solid-primitives/i18n";
type Strings = { type Strings = {
["lang.auto"]: Template<{detected: string}> ["lang.auto"]: Template<{detected: string}>
} & Record<string, string | undefined> } & Record<string, string | undefined>
const Settings: ParentComponent = () => { const Settings: ParentComponent = () => {
const [strings] = createStringResource( const [t] = createTranslator(
(code) => (code) =>
import(`./i18n/${code}.json`) as Promise<{ import(`./i18n/${code}.json`) as Promise<{
default: Strings; default: Strings;
}>, }>,
() => import(`./i18n/lang-names.json`) () => import(`./i18n/lang-names.json`)
); );
const t = translator(strings, resolveTemplate);
const navigate = useNavigate(); const navigate = useNavigate();
const settings$ = useStore($settings); const settings$ = useStore($settings);
const { const {