From 8a9c788fe14cc83c9c7ac08d9f4b7c4012fe9ca7 Mon Sep 17 00:00:00 2001 From: thislight Date: Thu, 26 Sep 2024 19:36:46 +0800 Subject: [PATCH] createStringsResource: support merged imports --- src/platform/i18n.tsx | 25 ++++++++++++---- src/settings/Settings.tsx | 48 ++++++++++++++++++------------- src/settings/i18n/en.json | 6 ++-- src/settings/i18n/lang-names.json | 4 +++ src/settings/i18n/zh-Hans.json | 6 ++-- tsconfig.json | 1 + 6 files changed, 58 insertions(+), 32 deletions(-) create mode 100644 src/settings/i18n/lang-names.json diff --git a/src/platform/i18n.tsx b/src/platform/i18n.tsx index 7627ea1..d291677 100644 --- a/src/platform/i18n.tsx +++ b/src/platform/i18n.tsx @@ -35,7 +35,7 @@ export function autoMatchLangTag() { return match(Array.from(navigator.languages), SUPPORTED_LANGS, DEFAULT_LANG); } -const DateFnLocaleCx = createContext>(() => enGB); +const DateFnLocaleCx = /* __@PURE__ */createContext>(() => enGB); const cachedDateFnLocale: Record = { enGB, @@ -149,11 +149,20 @@ export function useLanguage() { return () => settings().language || autoMatchLangTag(); } +type ImportFn = (name: string) => Promise<{default: T}> + +type ImportedModule = F extends ImportFn ? T: never + +type MergedImportedModule = + T extends [] ? {} : + T extends [infer I] ? ImportedModule : + T extends [infer I, ...infer J] ? ImportedModule & MergedImportedModule : never + export function createStringResource< - M extends Record>, ->(importFn: (code: string) => Promise<{ default: M }>) { + T extends ImportFn | undefined>>[], +>(...importFns: T) { const language = useLanguage(); - const cache: Record = {}; + const cache: Record> = {}; return createResource( () => [language()] as const, @@ -162,9 +171,13 @@ export function createStringResource< return cache[nlang]; } - const { default: dict } = await importFn(`${nlang}`); + const results = await Promise.all(importFns.map(x => x(nlang).then(v => v.default))) - return dict; + const merged: MergedImportedModule = Object.assign({}, ...results) + + cache[nlang] = merged; + + return merged; }, ); } diff --git a/src/settings/Settings.tsx b/src/settings/Settings.tsx index 65d94b9..1b9605c 100644 --- a/src/settings/Settings.tsx +++ b/src/settings/Settings.tsx @@ -17,6 +17,7 @@ import { } from "@suid/material"; import { Close as CloseIcon, + Logout, Public as PublicIcon, Refresh as RefreshIcon, Translate as TranslateIcon, @@ -38,11 +39,19 @@ import { SUPPORTED_REGIONS, useDateFnLocale, } from "../platform/i18n.jsx"; -import { resolveTemplate, translator } from "@solid-primitives/i18n"; +import { resolveTemplate, translator, type Template } from "@solid-primitives/i18n"; + +type Strings = { + ["lang.auto"]: Template<{detected: string}> +} & Record const Settings: ParentComponent = () => { const [strings] = createStringResource( - (code) => import(`./i18n/${code}.json`), + (code) => + import(`./i18n/${code}.json`) as Promise<{ + default: Strings; + }>, + () => import(`./i18n/lang-names.json`) ); const t = translator(strings, resolveTemplate); const navigate = useNavigate(); @@ -87,31 +96,34 @@ const Settings: ParentComponent = () => {
    • {t("Accounts")} - + {t("All Notifications")} - + - + - + {t("Sign in...")} - +
    {({ account: acct, inf }) => (
      {`@${inf?.username ?? "..."}@${new URL(acct.site).host}`} - - Notifications + + {t("Notifications")} - + - + - Sign out + + + + {t("Sign out")}
    @@ -120,11 +132,7 @@ const Settings: ParentComponent = () => {
  • {t("Reading")} - - Fonts - - - $settings.setKey( "prefetchTootsDisabled", @@ -138,7 +146,7 @@ const Settings: ParentComponent = () => { - +
  • @@ -162,7 +170,7 @@ const Settings: ParentComponent = () => { > @@ -191,7 +199,7 @@ const Settings: ParentComponent = () => { > diff --git a/src/settings/i18n/en.json b/src/settings/i18n/en.json index 07fb85d..1149933 100644 --- a/src/settings/i18n/en.json +++ b/src/settings/i18n/en.json @@ -15,11 +15,11 @@ "Language": "Language", "Region": "Region", "lang.auto": "Auto({{detected}})", - "lang.zh-Hans": "中文(简体)", - "lang.en": "English", "region.auto": "Auto({{detected}})", "region.en_GB": "Great Britan (English)", "region.en_US": "United States (English)", "region.zh_CN": "China Mainland (Chinese)", - "datefmt": "yyyy/MM/dd" + "datefmt": "yyyy/MM/dd", + "Sign out": "Sign out", + "Notifications": "Notifications" } \ No newline at end of file diff --git a/src/settings/i18n/lang-names.json b/src/settings/i18n/lang-names.json new file mode 100644 index 0000000..6d250bd --- /dev/null +++ b/src/settings/i18n/lang-names.json @@ -0,0 +1,4 @@ +{ + "lang.zh-Hans": "中文(简体)", + "lang.en": "English" +} \ No newline at end of file diff --git a/src/settings/i18n/zh-Hans.json b/src/settings/i18n/zh-Hans.json index df9941e..4d046df 100644 --- a/src/settings/i18n/zh-Hans.json +++ b/src/settings/i18n/zh-Hans.json @@ -15,11 +15,11 @@ "Language": "语言", "Region": "区域", "lang.auto": "自动({{detected}})", - "lang.zh-Hans": "中文(简体)", - "lang.en": "English", "region.auto": "自动({{detected}})", "region.en_GB": "英国和苏格兰(英语)", "region.en_US": "美国(英语)", "region.zh_CN": "中国大陆(中文)", - "datefmt": "yyyy年MM月dd日" + "datefmt": "yyyy年MM月dd日", + "Sign out": "登出此账户", + "Notifications": "通知" } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index ccd018b..6cc68b0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,5 +11,6 @@ "types": ["vite/client", "vite-plugin-pwa/solid"], "noEmit": true, "isolatedModules": true, + "resolveJsonModule": true, } }