feat: initial commit - Phase 1 & 2 core features

This commit is contained in:
hiderfong
2026-04-22 17:07:33 +08:00
commit 1773bda06b
25005 changed files with 6252106 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
import { FieldPath } from "../../utils/typescript.js";
import "../../utils/index.js";
import _default from "../../locale/lang/en.js";
import { Language } from "../../locale/index.js";
import { InjectionKey, Ref } from "vue";
import { MaybeRef } from "@vueuse/core";
//#region ../../packages/hooks/use-locale/index.d.ts
type LocaleKeys = Exclude<FieldPath<typeof _default>, 'name' | 'el'> | (string & NonNullable<unknown>);
type TranslatorOption = Record<string, string | number>;
type Translator = (path: LocaleKeys, option?: TranslatorOption) => string;
type LocaleContext = {
locale: Ref<Language>;
lang: Ref<string>;
t: Translator;
};
declare const buildTranslator: (locale: MaybeRef<Language>) => Translator;
declare const translate: (path: LocaleKeys, option: undefined | TranslatorOption, locale: Language) => string;
declare const buildLocaleContext: (locale: MaybeRef<Language>) => LocaleContext;
declare const localeContextKey: InjectionKey<Ref<Language | undefined>>;
declare const useLocale: (localeOverrides?: Ref<Language | undefined>) => LocaleContext;
//#endregion
export { LocaleContext, LocaleKeys, Translator, TranslatorOption, buildLocaleContext, buildTranslator, localeContextKey, translate, useLocale };
+23
View File
@@ -0,0 +1,23 @@
import en_default from "../../locale/lang/en.mjs";
import { get } from "lodash-unified";
import { computed, inject, isRef, ref, unref } from "vue";
//#region ../../packages/hooks/use-locale/index.ts
const buildTranslator = (locale) => (path, option) => translate(path, option, unref(locale));
const translate = (path, option, locale) => get(locale, path, path).replace(/\{(\w+)\}/g, (_, key) => `${option?.[key] ?? `{${key}}`}`);
const buildLocaleContext = (locale) => {
return {
lang: computed(() => unref(locale).name),
locale: isRef(locale) ? locale : ref(locale),
t: buildTranslator(locale)
};
};
const localeContextKey = Symbol("localeContextKey");
const useLocale = (localeOverrides) => {
const locale = localeOverrides || inject(localeContextKey, ref());
return buildLocaleContext(computed(() => locale.value || en_default));
};
//#endregion
export { buildLocaleContext, buildTranslator, localeContextKey, translate, useLocale };
//# sourceMappingURL=index.mjs.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.mjs","names":["English"],"sources":["../../../../../packages/hooks/use-locale/index.ts"],"sourcesContent":["import { computed, inject, isRef, ref, unref } from 'vue'\nimport { get } from 'lodash-unified'\nimport English from '@element-plus/locale/lang/en'\n\nimport type { MaybeRef } from '@vueuse/core'\nimport type { InjectionKey, Ref } from 'vue'\nimport type { FieldPath } from '@element-plus/utils'\nimport type { Language } from '@element-plus/locale'\n\nexport type LocaleKeys =\n | Exclude<FieldPath<typeof English>, 'name' | 'el'>\n | (string & NonNullable<unknown>)\n\nexport type TranslatorOption = Record<string, string | number>\nexport type Translator = (path: LocaleKeys, option?: TranslatorOption) => string\nexport type LocaleContext = {\n locale: Ref<Language>\n lang: Ref<string>\n t: Translator\n}\n\nexport const buildTranslator =\n (locale: MaybeRef<Language>): Translator =>\n (path, option) =>\n translate(path, option, unref(locale))\n\nexport const translate = (\n path: LocaleKeys,\n option: undefined | TranslatorOption,\n locale: Language\n): string =>\n (get(locale, path, path) as string).replace(\n /\\{(\\w+)\\}/g,\n (_, key) => `${option?.[key] ?? `{${key}}`}`\n )\n\nexport const buildLocaleContext = (\n locale: MaybeRef<Language>\n): LocaleContext => {\n const lang = computed(() => unref(locale).name)\n const localeRef = isRef(locale) ? locale : ref(locale)\n return {\n lang,\n locale: localeRef,\n t: buildTranslator(locale),\n }\n}\n\nexport const localeContextKey: InjectionKey<Ref<Language | undefined>> =\n Symbol('localeContextKey')\n\nexport const useLocale = (localeOverrides?: Ref<Language | undefined>) => {\n const locale = localeOverrides || inject(localeContextKey, ref())!\n return buildLocaleContext(computed(() => locale.value || English))\n}\n"],"mappings":";;;;;AAqBA,MAAa,mBACV,YACA,MAAM,WACL,UAAU,MAAM,QAAQ,MAAM,OAAO,CAAC;AAE1C,MAAa,aACX,MACA,QACA,WAEC,IAAI,QAAQ,MAAM,KAAK,CAAY,QAClC,eACC,GAAG,QAAQ,GAAG,SAAS,QAAQ,IAAI,IAAI,KACzC;AAEH,MAAa,sBACX,WACkB;AAGlB,QAAO;EACL,MAHW,eAAe,MAAM,OAAO,CAAC,KAAK;EAI7C,QAHgB,MAAM,OAAO,GAAG,SAAS,IAAI,OAAO;EAIpD,GAAG,gBAAgB,OAAO;EAC3B;;AAGH,MAAa,mBACX,OAAO,mBAAmB;AAE5B,MAAa,aAAa,oBAAgD;CACxE,MAAM,SAAS,mBAAmB,OAAO,kBAAkB,KAAK,CAAC;AACjE,QAAO,mBAAmB,eAAe,OAAO,SAASA,WAAQ,CAAC"}