听海 a303e171c9
feat: 暴露语言变化事件 & layout的title支持配置国际化 (#255)
* feat: layout的title支持国际化

* feat: locale插件暴漏onLocaleChange事件

* chore: change docs

* fix: 去掉无用代码

---------

Co-authored-by: winixt <haizekuo@gmail.com>
2024-10-23 18:58:36 +08:00

107 lines
2.9 KiB
JavaScript

import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { name } from '../package.json';
const namespace = 'plugin-locale';
export default (api) => {
const {
utils: { Mustache },
} = api;
api.describe({
key: 'locale',
config: {
schema(joi) {
return joi.object();
},
default: {},
onChange: api.ConfigChangeType.regenerateTmpFiles,
},
});
const { getLocales } = require('./utils');
api.addRuntimePluginKey(() => 'locale');
api.addRuntimePluginKey(() => 'onLocaleChange');
const absoluteFilePath = join(namespace, 'core.js');
const absRuntimeFilePath = join(namespace, 'runtime.js');
function getLocaleFileBasePath() {
return join(api.paths.absSrcPath, api.config.singular ? 'locale' : 'locales');
}
api.register({
key: 'addExtraLocales',
fn: () => [
getLocaleFileBasePath(),
],
});
// 监听 locale 文件改变,重新生成文件
api.addTmpGenerateWatcherPaths(getLocaleFileBasePath);
api.onGenerateFiles(async () => {
// .fes配置
const userConfig = {
locale: 'zh-CN', // default locale
fallbackLocale: 'zh-CN', // set fallback locale
legacy: false,
baseNavigator: true, // 开启浏览器语言检测
...api.config.locale,
};
const additionalLocales = await api.applyPlugins({
key: 'addExtraLocales',
type: api.ApplyPluginsType.add,
initialValue: [],
});
const { files, locales } = getLocales(additionalLocales);
const { baseNavigator, ...otherConfig } = userConfig;
api.writeTmpFile({
path: join(namespace, 'locales.js'),
content: Mustache.render(readFileSync(join(__dirname, 'runtime/locales.js.tpl'), 'utf-8'), {
REPLACE_IMPORTS: files,
REPLACE_LOCALES: locales.map(item => ({
locale: item.locale,
importNames: item.importNames.join(', '),
})),
}),
});
api.writeTmpFile({
path: absoluteFilePath,
content: Mustache.render(readFileSync(join(__dirname, 'runtime/core.js.tpl'), 'utf-8'), {
REPLACE_DEFAULT_OPTIONS: JSON.stringify(otherConfig, null, 2),
BASE_NAVIGATOR: baseNavigator,
VUE_I18N_PATH: 'vue-i18n',
}),
});
api.copyTmpFiles({
namespace,
path: join(__dirname, 'runtime'),
ignore: ['.tpl'],
});
});
api.addPluginExports(() => [
{
specifiers: ['useI18n', 'locale'],
source: absoluteFilePath,
},
]);
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
api.addConfigType(() => ({
source: name,
}));
};