This commit is contained in:
2025-09-19 14:25:20 +08:00
parent 269893a435
commit fbf3f77229
24949 changed files with 2839404 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
import English from 'element-plus/es/locale/lang/en';
import type { MaybeRef } from '@vueuse/core';
import type { InjectionKey, Ref } from 'vue';
import type { FieldPath } from 'element-plus/es/utils';
import type { Language } from 'element-plus/es/locale';
export type LocaleKeys = Exclude<FieldPath<typeof English>, 'name' | 'el'> | (string & NonNullable<unknown>);
export type TranslatorOption = Record<string, string | number>;
export type Translator = (path: LocaleKeys, option?: TranslatorOption) => string;
export type LocaleContext = {
locale: Ref<Language>;
lang: Ref<string>;
t: Translator;
};
export declare const buildTranslator: (locale: MaybeRef<Language>) => Translator;
export declare const translate: (path: LocaleKeys, option: undefined | TranslatorOption, locale: Language) => string;
export declare const buildLocaleContext: (locale: MaybeRef<Language>) => LocaleContext;
export declare const localeContextKey: InjectionKey<Ref<Language | undefined>>;
export declare const useLocale: (localeOverrides?: Ref<Language | undefined>) => LocaleContext;

View File

@@ -0,0 +1,34 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var vue = require('vue');
var lodashUnified = require('lodash-unified');
var en = require('../../locale/lang/en.js');
const buildTranslator = (locale) => (path, option) => translate(path, option, vue.unref(locale));
const translate = (path, option, locale) => lodashUnified.get(locale, path, path).replace(/\{(\w+)\}/g, (_, key) => {
var _a;
return `${(_a = option == null ? void 0 : option[key]) != null ? _a : `{${key}}`}`;
});
const buildLocaleContext = (locale) => {
const lang = vue.computed(() => vue.unref(locale).name);
const localeRef = vue.isRef(locale) ? locale : vue.ref(locale);
return {
lang,
locale: localeRef,
t: buildTranslator(locale)
};
};
const localeContextKey = Symbol("localeContextKey");
const useLocale = (localeOverrides) => {
const locale = localeOverrides || vue.inject(localeContextKey, vue.ref());
return buildLocaleContext(vue.computed(() => locale.value || en["default"]));
};
exports.buildLocaleContext = buildLocaleContext;
exports.buildTranslator = buildTranslator;
exports.localeContextKey = localeContextKey;
exports.translate = translate;
exports.useLocale = useLocale;
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","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"],"names":["unref","get","computed","isRef","ref","inject","English"],"mappings":";;;;;;;;AAGY,MAAC,eAAe,GAAG,CAAC,MAAM,KAAK,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,CAAC,IAAI,EAAE,MAAM,EAAEA,SAAK,CAAC,MAAM,CAAC,EAAE;AACx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