Skip to content

Commit

Permalink
feat: partial locale type
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed May 18, 2023
1 parent de77551 commit 50cfbaa
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
7 changes: 6 additions & 1 deletion app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 8 additions & 1 deletion app/locales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -80,7 +81,8 @@ export function changeLang(lang: Lang) {
location.reload();
}

export default {
const fallbackLang = EN;
const targetLang = {
en: EN,
cn: CN,
tw: TW,
Expand All @@ -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;
2 changes: 1 addition & 1 deletion app/masks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
4 changes: 3 additions & 1 deletion app/masks/typing.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type Mask } from "../store/mask";

export type BuiltinMask = Omit<Mask, "id">;
export type BuiltinMask = Omit<Mask, "id"> & {
builtin: true;
};
9 changes: 9 additions & 0 deletions app/utils/merge.ts
Original file line number Diff line number Diff line change
@@ -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];
});
}

0 comments on commit 50cfbaa

Please sign in to comment.