2021-06-06 23:36:22 +08:00
|
|
|
import type { LocaleType } from '/#/config';
|
|
|
|
|
|
2021-01-09 23:28:52 +08:00
|
|
|
import { set } from 'lodash-es';
|
|
|
|
|
|
2021-06-06 23:36:22 +08:00
|
|
|
export const loadLocalePool: LocaleType[] = [];
|
|
|
|
|
|
|
|
|
|
export function setHtmlPageLang(locale: LocaleType) {
|
|
|
|
|
document.querySelector('html')?.setAttribute('lang', locale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function setLoadLocalePool(cb: (loadLocalePool: LocaleType[]) => void) {
|
|
|
|
|
cb(loadLocalePool);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-09 23:28:52 +08:00
|
|
|
export function genMessage(langs: Record<string, Record<string, any>>, prefix = 'lang') {
|
|
|
|
|
const obj: Recordable = {};
|
|
|
|
|
|
|
|
|
|
Object.keys(langs).forEach((key) => {
|
2021-02-27 23:08:12 +08:00
|
|
|
const langFileModule = langs[key].default;
|
|
|
|
|
let fileName = key.replace(`./${prefix}/`, '').replace(/^\.\//, '');
|
|
|
|
|
const lastIndex = fileName.lastIndexOf('.');
|
|
|
|
|
fileName = fileName.substring(0, lastIndex);
|
|
|
|
|
const keyList = fileName.split('/');
|
|
|
|
|
const moduleName = keyList.shift();
|
2021-01-09 23:28:52 +08:00
|
|
|
const objKey = keyList.join('.');
|
2021-02-27 23:08:12 +08:00
|
|
|
|
|
|
|
|
if (moduleName) {
|
|
|
|
|
if (objKey) {
|
|
|
|
|
set(obj, moduleName, obj[moduleName] || {});
|
|
|
|
|
set(obj[moduleName], objKey, langFileModule);
|
|
|
|
|
} else {
|
|
|
|
|
set(obj, moduleName, langFileModule || {});
|
|
|
|
|
}
|
2021-01-09 23:28:52 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return obj;
|
|
|
|
|
}
|