feat: partial locale type

This commit is contained in:
Yidadaa 2023-05-19 00:59:04 +08:00
parent de775511d0
commit 50cfbaaab5
5 changed files with 27 additions and 4 deletions

View File

@ -241,6 +241,11 @@ const cn = {
}, },
}; };
export type LocaleType = typeof cn; type DeepPartial<T> = T extends object
? {
[P in keyof T]?: DeepPartial<T[P]>;
}
: T;
export type LocaleType = DeepPartial<typeof cn>;
export default cn; export default cn;

View File

@ -11,6 +11,7 @@ import VI from "./vi";
import RU from "./ru"; import RU from "./ru";
import CS from "./cs"; import CS from "./cs";
import KO from "./ko"; import KO from "./ko";
import { merge } from "../utils/merge";
export type { LocaleType } from "./cn"; export type { LocaleType } from "./cn";
@ -80,7 +81,8 @@ export function changeLang(lang: Lang) {
location.reload(); location.reload();
} }
export default { const fallbackLang = EN;
const targetLang = {
en: EN, en: EN,
cn: CN, cn: CN,
tw: TW, tw: TW,
@ -95,3 +97,8 @@ export default {
cs: CS, cs: CS,
ko: KO, ko: KO,
}[getLang()] as typeof CN; }[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;

View File

@ -15,7 +15,7 @@ export const BUILTIN_MASK_STORE = {
return this.masks[id] as Mask | undefined; return this.masks[id] as Mask | undefined;
}, },
add(m: BuiltinMask) { add(m: BuiltinMask) {
const mask = { ...m, id: this.buildinId++ }; const mask = { ...m, id: this.buildinId++, builtin: true };
this.masks[mask.id] = mask; this.masks[mask.id] = mask;
return mask; return mask;
}, },

View File

@ -1,3 +1,5 @@
import { type Mask } from "../store/mask"; import { type Mask } from "../store/mask";
export type BuiltinMask = Omit<Mask, "id">; export type BuiltinMask = Omit<Mask, "id"> & {
builtin: true;
};

9
app/utils/merge.ts Normal file
View File

@ -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];
});
}