This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+64
View File
@@ -0,0 +1,64 @@
// Extensions
import { Service } from '../service'; // Utilities
import { getObjectValueByPath } from '../../util/helpers';
import { consoleError, consoleWarn } from '../../util/console';
const LANG_PREFIX = '$vuetify.';
const fallback = Symbol('Lang fallback');
function getTranslation(locale, key, usingDefault = false, defaultLocale) {
const shortKey = key.replace(LANG_PREFIX, '');
let translation = getObjectValueByPath(locale, shortKey, fallback);
if (translation === fallback) {
if (usingDefault) {
consoleError(`Translation key "${shortKey}" not found in fallback`);
translation = key;
} else {
consoleWarn(`Translation key "${shortKey}" not found, falling back to default`);
translation = getTranslation(defaultLocale, key, true, defaultLocale);
}
}
return translation;
}
export class Lang extends Service {
constructor(preset) {
super();
this.defaultLocale = 'en';
const {
current,
locales,
t
} = preset[Lang.property];
this.current = current;
this.locales = locales;
this.translator = t || this.defaultTranslator;
}
currentLocale(key) {
const translation = this.locales[this.current];
const defaultLocale = this.locales[this.defaultLocale];
return getTranslation(translation, key, false, defaultLocale);
}
t(key, ...params) {
if (!key.startsWith(LANG_PREFIX)) return this.replace(key, params);
return this.translator(key, ...params);
}
defaultTranslator(key, ...params) {
return this.replace(this.currentLocale(key), params);
}
replace(str, params) {
return str.replace(/\{(\d+)\}/g, (match, index) => {
/* istanbul ignore next */
return String(params[+index]);
});
}
}
Lang.property = 'lang';
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/services/lang/index.ts"],"names":[],"mappings":"AAAA;AACA,SAAS,OAAT,QAAwB,YAAxB,C,CAEA;;AACA,SAAS,oBAAT,QAAqC,oBAArC;AACA,SAAS,YAAT,EAAuB,WAAvB,QAA0C,oBAA1C;AASA,MAAM,WAAW,GAAG,WAApB;AACA,MAAM,QAAQ,GAAG,MAAM,CAAC,eAAD,CAAvB;;AAEA,SAAS,cAAT,CACE,MADF,EAEE,GA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AAe,WAAf,CAAL,EAAkC,OAAO,KAAK,OAAL,CAAa,GAAb,EAAkB,MAAlB,CAAP;AAElC,WAAO,KAAK,UAAL,CAAgB,GAAhB,EAAqB,GAAG,MAAxB,CAAP;AACD;;AAEO,EAAA,iBAAiB,CAAE,GAAF,EAAe,GAAG,MAAlB,EAA+B;AACtD,WAAO,KAAK,OAAL,CAAa,KAAK,aAAL,CAAmB,GAAnB,CAAb,EAAsC,MAAtC,CAAP;AACD;;AAEO,EAAA,OAAO,CAAE,GAAF,EAAe,MAAf,EAA4B;AACzC,WAAO,GAAG,CAAC,OAAJ,CAAY,YAAZ,EAA0B,CAAC,KAAD,EAAgB,KAAhB,KAAiC;AAChE;AACA,aAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAF,CAAP,CAAb;AACD,KAHM,CAAP;AAID;;AA/C8B;AACxB,IAAA,CAAA,QAAA,GAAmB,MAAnB","sourcesContent":["// Extensions\nimport { Service } from '../service'\n\n// Utilities\nimport { getObjectValueByPath } from '../../util/helpers'\nimport { consoleError, consoleWarn } from '../../util/console'\n\n// Types\nimport { VuetifyPreset } from 'vuetify/types/services/presets'\nimport {\n VuetifyLocale,\n Lang as ILang,\n} from 'vuetify/types/services/lang'\n\nconst LANG_PREFIX = '$vuetify.'\nconst fallback = Symbol('Lang fallback')\n\nfunction getTranslation (\n locale: VuetifyLocale,\n key: string,\n usingDefault = false,\n defaultLocale: VuetifyLocale\n): string {\n const shortKey = key.replace(LANG_PREFIX, '')\n let translation = getObjectValueByPath(locale, shortKey, fallback) as string | typeof fallback\n\n if (translation === fallback) {\n if (usingDefault) {\n consoleError(`Translation key \"${shortKey}\" not found in fallback`)\n translation = key\n } else {\n consoleWarn(`Translation key \"${shortKey}\" not found, falling back to default`)\n translation = getTranslation(defaultLocale, key, true, defaultLocale)\n }\n }\n\n return translation\n}\n\nexport class Lang extends Service implements ILang {\n static property: 'lang' = 'lang'\n\n public current: ILang['current']\n\n public defaultLocale = 'en'\n\n public locales: ILang['locales']\n\n private translator: ILang['t']\n\n constructor (preset: VuetifyPreset) {\n super()\n\n const {\n current,\n locales,\n t,\n } = preset[Lang.property]\n\n this.current = current\n this.locales = locales\n this.translator = t || this.defaultTranslator\n }\n\n public currentLocale (key: string) {\n const translation = this.locales[this.current]\n const defaultLocale = this.locales[this.defaultLocale]\n\n return getTranslation(translation, key, false, defaultLocale)\n }\n\n public t (key: string, ...params: any[]) {\n if (!key.startsWith(LANG_PREFIX)) return this.replace(key, params)\n\n return this.translator(key, ...params)\n }\n\n private defaultTranslator (key: string, ...params: any[]) {\n return this.replace(this.currentLocale(key), params)\n }\n\n private replace (str: string, params: any[]) {\n return str.replace(/\\{(\\d+)\\}/g, (match: string, index: string) => {\n /* istanbul ignore next */\n return String(params[+index])\n })\n }\n}\n"],"sourceRoot":"","file":"index.js"}