From 50cfbaaab535041e765473205137443f6bd3d9a1 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Fri, 19 May 2023 00:59:04 +0800 Subject: [PATCH] feat: partial locale type --- app/locales/cn.ts | 7 ++++++- app/locales/index.ts | 9 ++++++++- app/masks/index.ts | 2 +- app/masks/typing.ts | 4 +++- app/utils/merge.ts | 9 +++++++++ 5 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 app/utils/merge.ts diff --git a/app/locales/cn.ts b/app/locales/cn.ts index 9814c9f9deb..035e9c2e2bd 100644 --- a/app/locales/cn.ts +++ b/app/locales/cn.ts @@ -241,6 +241,11 @@ const cn = { }, }; -export type LocaleType = typeof cn; +type DeepPartial = T extends object + ? { + [P in keyof T]?: DeepPartial; + } + : T; +export type LocaleType = DeepPartial; export default cn; diff --git a/app/locales/index.ts b/app/locales/index.ts index b45ceebd4fd..e44b7589142 100644 --- a/app/locales/index.ts +++ b/app/locales/index.ts @@ -11,6 +11,7 @@ import VI from "./vi"; import RU from "./ru"; import CS from "./cs"; import KO from "./ko"; +import { merge } from "../utils/merge"; export type { LocaleType } from "./cn"; @@ -80,7 +81,8 @@ export function changeLang(lang: Lang) { location.reload(); } -export default { +const fallbackLang = EN; +const targetLang = { en: EN, cn: CN, tw: TW, @@ -95,3 +97,8 @@ export default { cs: CS, ko: KO, }[getLang()] as typeof CN; + +// if target lang missing some fields, it will use fallback lang string +merge(fallbackLang, targetLang); + +export default fallbackLang as typeof CN; diff --git a/app/masks/index.ts b/app/masks/index.ts index ea0bf32bf4e..07c6a3e8cda 100644 --- a/app/masks/index.ts +++ b/app/masks/index.ts @@ -15,7 +15,7 @@ export const BUILTIN_MASK_STORE = { return this.masks[id] as Mask | undefined; }, add(m: BuiltinMask) { - const mask = { ...m, id: this.buildinId++ }; + const mask = { ...m, id: this.buildinId++, builtin: true }; this.masks[mask.id] = mask; return mask; }, diff --git a/app/masks/typing.ts b/app/masks/typing.ts index 5f39ccdc87c..510d94a2c20 100644 --- a/app/masks/typing.ts +++ b/app/masks/typing.ts @@ -1,3 +1,5 @@ import { type Mask } from "../store/mask"; -export type BuiltinMask = Omit; +export type BuiltinMask = Omit & { + builtin: true; +}; diff --git a/app/utils/merge.ts b/app/utils/merge.ts new file mode 100644 index 00000000000..758d6df84fd --- /dev/null +++ b/app/utils/merge.ts @@ -0,0 +1,9 @@ +export function merge(target: any, source: any) { + Object.keys(source).forEach(function (key) { + if (source[key] && typeof source[key] === "object") { + merge((target[key] = target[key] || {}), source[key]); + return; + } + target[key] = source[key]; + }); +}