😎增加前端自动翻译组件vite-auto-i18n-plugin
This commit is contained in:
parent
d29f5572ad
commit
bda7c4d71d
87
Web/lang/index.js
Normal file
87
Web/lang/index.js
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
|
||||||
|
// 导入国际化JSON文件
|
||||||
|
import langJSON from './index.json'
|
||||||
|
(function () {
|
||||||
|
// 定义翻译函数
|
||||||
|
let $t = function (key, val, nameSpace) {
|
||||||
|
// 获取指定命名空间下的语言包
|
||||||
|
const langPackage = $t[nameSpace];
|
||||||
|
// 返回翻译结果,如果不存在则返回默认值
|
||||||
|
return (langPackage || {})[key] || val;
|
||||||
|
};
|
||||||
|
// 定义简单翻译函数,直接返回传入的值
|
||||||
|
let $$t = function (val) {
|
||||||
|
return val;
|
||||||
|
};
|
||||||
|
globalThis.$deepScan = function (val) {
|
||||||
|
return val;
|
||||||
|
};
|
||||||
|
globalThis.$iS = function (val, args) {
|
||||||
|
// 如果参数不是字符串或数组,直接返回原值
|
||||||
|
if (typeof val !== 'string' || !Array.isArray(args)) {
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// 使用更安全的正则表达式替换方式
|
||||||
|
return val.replace(/\$(?:\{|\{)(\d+)(?:\}|\})/g, (match, index) => {
|
||||||
|
// 将index转换为数字
|
||||||
|
const position = parseInt(index, 10);
|
||||||
|
// 如果args[position]存在则替换,否则保留原占位符
|
||||||
|
return args[position] !== undefined ? String(args[position]) : match;
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('字符串替换过程出现异常:', error);
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 定义设置语言包的方法
|
||||||
|
$t.locale = function (locale, nameSpace) {
|
||||||
|
// 将指定命名空间下的语言包设置为传入的locale
|
||||||
|
$t[nameSpace] = locale || {};
|
||||||
|
};
|
||||||
|
// 将翻译函数挂载到globalThis对象上,如果已经存在则使用已有的
|
||||||
|
globalThis.$t = globalThis.$t || $t;
|
||||||
|
// 将简单翻译函数挂载到globalThis对象上
|
||||||
|
globalThis.$$t = $$t;
|
||||||
|
// 定义从JSON文件中获取指定键的语言对象的方法
|
||||||
|
globalThis._getJSONKey = function (key, insertJSONObj = undefined) {
|
||||||
|
// 获取JSON对象
|
||||||
|
const JSONObj = insertJSONObj;
|
||||||
|
// 初始化语言对象
|
||||||
|
const langObj = {};
|
||||||
|
// 遍历JSON对象的所有键
|
||||||
|
Object.keys(JSONObj).forEach((value) => {
|
||||||
|
// 将每个语言的对应键值添加到语言对象中
|
||||||
|
langObj[value] = JSONObj[value][key];
|
||||||
|
});
|
||||||
|
// 返回语言对象
|
||||||
|
return langObj;
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
// 定义语言映射对象
|
||||||
|
const langMap = {
|
||||||
|
'en': (globalThis && globalThis.lang && globalThis.lang.en) ? globalThis.lang.en : globalThis._getJSONKey('en', langJSON),
|
||||||
|
'zhcn': (globalThis && globalThis.lang && globalThis.lang.zhcn) ? globalThis.lang.zhcn : globalThis._getJSONKey('zh-cn', langJSON)
|
||||||
|
};
|
||||||
|
globalThis.langMap = langMap;
|
||||||
|
// 存储语言是否存在
|
||||||
|
// 判断 globalThis.localStorage.getItem 是否为函数
|
||||||
|
const isFunction = (fn) => {
|
||||||
|
return typeof fn === 'function';
|
||||||
|
};
|
||||||
|
|
||||||
|
const withStorageLang = isFunction && globalThis && globalThis.localStorage &&
|
||||||
|
isFunction(globalThis.localStorage.getItem) && globalThis.localStorage.getItem('lang');
|
||||||
|
const withStorageCommonLang = isFunction && globalThis && globalThis.localStorage &&
|
||||||
|
isFunction(globalThis.localStorage.getItem) && globalThis.localStorage.getItem('');
|
||||||
|
// 从本地存储中获取通用语言,如果不存在则使用空字符串
|
||||||
|
const commonLang = withStorageCommonLang ? globalThis.localStorage.getItem('') : '';
|
||||||
|
// 从本地存储中获取当前语言,如果不存在则使用源语言
|
||||||
|
const baseLang = withStorageLang ? globalThis.localStorage.getItem('lang') : 'zhcn';
|
||||||
|
const lang = commonLang ? commonLang : baseLang;
|
||||||
|
// 根据当前语言设置翻译函数的语言包
|
||||||
|
globalThis.$t.locale(globalThis.langMap[lang], 'lang');
|
||||||
|
globalThis.$changeLang = (lang) => {
|
||||||
|
globalThis.$t.locale(globalThis.langMap[lang], 'lang');
|
||||||
|
};
|
||||||
|
|
||||||
5522
Web/lang/index.json
Normal file
5522
Web/lang/index.json
Normal file
File diff suppressed because it is too large
Load Diff
@ -24,7 +24,7 @@
|
|||||||
"@vue-office/docx": "^1.6.3",
|
"@vue-office/docx": "^1.6.3",
|
||||||
"@vue-office/excel": "^1.7.14",
|
"@vue-office/excel": "^1.7.14",
|
||||||
"@vue-office/pdf": "^2.0.10",
|
"@vue-office/pdf": "^2.0.10",
|
||||||
"@vueuse/core": "^13.5.0",
|
"@vueuse/core": "^13.6.0",
|
||||||
"@vxe-ui/plugin-export-xlsx": "^4.2.3",
|
"@vxe-ui/plugin-export-xlsx": "^4.2.3",
|
||||||
"@vxe-ui/plugin-render-element": "^4.0.15",
|
"@vxe-ui/plugin-render-element": "^4.0.15",
|
||||||
"@wangeditor/editor": "^5.1.23",
|
"@wangeditor/editor": "^5.1.23",
|
||||||
@ -110,6 +110,7 @@
|
|||||||
"terser": "^5.43.1",
|
"terser": "^5.43.1",
|
||||||
"typescript": "^5.8.3",
|
"typescript": "^5.8.3",
|
||||||
"vite": "^7.0.6",
|
"vite": "^7.0.6",
|
||||||
|
"vite-auto-i18n-plugin": "^1.1.6",
|
||||||
"vite-plugin-cdn-import": "^1.0.1",
|
"vite-plugin-cdn-import": "^1.0.1",
|
||||||
"vite-plugin-compression2": "^2.2.0",
|
"vite-plugin-compression2": "^2.2.0",
|
||||||
"vite-plugin-vue-setup-extend": "^0.4.0",
|
"vite-plugin-vue-setup-extend": "^0.4.0",
|
||||||
|
|||||||
@ -155,6 +155,7 @@ const state = reactive({
|
|||||||
tagsViewList: [],
|
tagsViewList: [],
|
||||||
tagsViewRoutesList: [],
|
tagsViewRoutesList: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
// 设置分割样式
|
// 设置分割样式
|
||||||
const layoutUserFlexNum = computed(() => {
|
const layoutUserFlexNum = computed(() => {
|
||||||
let num: string | number = '';
|
let num: string | number = '';
|
||||||
@ -246,6 +247,7 @@ const onComponentSizeChange = (size: string) => {
|
|||||||
initI18nOrSize('globalComponentSize', 'disabledSize');
|
initI18nOrSize('globalComponentSize', 'disabledSize');
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
};
|
};
|
||||||
|
|
||||||
// 语言切换
|
// 语言切换
|
||||||
const onLanguageChange = (lang: string) => {
|
const onLanguageChange = (lang: string) => {
|
||||||
Local.remove('themeConfig');
|
Local.remove('themeConfig');
|
||||||
@ -256,6 +258,9 @@ const onLanguageChange = (lang: string) => {
|
|||||||
other.useTitle();
|
other.useTitle();
|
||||||
initI18nOrSize('globalI18n', 'disabledI18n');
|
initI18nOrSize('globalI18n', 'disabledI18n');
|
||||||
refreshCurrentTabpage();
|
refreshCurrentTabpage();
|
||||||
|
|
||||||
|
if (lang == 'en') window.$changeLang('en');
|
||||||
|
else window.$changeLang('zhcn');
|
||||||
};
|
};
|
||||||
|
|
||||||
// 聊天点击
|
// 聊天点击
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import '../lang/index';
|
||||||
import { createApp } from 'vue';
|
import { createApp } from 'vue';
|
||||||
import pinia from '/@/stores/index';
|
import pinia from '/@/stores/index';
|
||||||
import App from '/@/App.vue';
|
import App from '/@/App.vue';
|
||||||
|
|||||||
3
Web/src/types/global.d.ts
vendored
3
Web/src/types/global.d.ts
vendored
@ -12,7 +12,7 @@ declare module 'vcrontab-3';
|
|||||||
declare module 'vue-signature-pad';
|
declare module 'vue-signature-pad';
|
||||||
declare module 'vform3-builds';
|
declare module 'vform3-builds';
|
||||||
declare module 'jwchat';
|
declare module 'jwchat';
|
||||||
declare module 'crypto-js'
|
declare module 'crypto-js';
|
||||||
|
|
||||||
// 声明一个模块,防止引入文件时报错
|
// 声明一个模块,防止引入文件时报错
|
||||||
declare module '*.json';
|
declare module '*.json';
|
||||||
@ -36,6 +36,7 @@ declare interface Window {
|
|||||||
BMAP_SATELLITE_MAP: any;
|
BMAP_SATELLITE_MAP: any;
|
||||||
BMap: any;
|
BMap: any;
|
||||||
__env__: any;
|
__env__: any;
|
||||||
|
$changeLang: (lang: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 声明路由当前项类型
|
// 声明路由当前项类型
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import { webUpdateNotice } from '@plugin-web-update-notification/vite';
|
|||||||
// monaco 菜单汉化 https://wf0.github.io/example/plugins/I18n.html
|
// monaco 菜单汉化 https://wf0.github.io/example/plugins/I18n.html
|
||||||
import monacoZhHans from './public/monaco/zh-hans.json';
|
import monacoZhHans from './public/monaco/zh-hans.json';
|
||||||
import nlsPlugin, { Languages, esbuildPluginMonacoEditorNls } from './public/monaco/vite-plugin-i18n-nls';
|
import nlsPlugin, { Languages, esbuildPluginMonacoEditorNls } from './public/monaco/vite-plugin-i18n-nls';
|
||||||
|
import vitePluginsAutoI18n, { EmptyTranslator, VolcengineTranslator, YoudaoTranslator } from 'vite-auto-i18n-plugin';
|
||||||
|
|
||||||
const pathResolve = (dir: string) => {
|
const pathResolve = (dir: string) => {
|
||||||
return resolve(__dirname, '.', dir);
|
return resolve(__dirname, '.', dir);
|
||||||
@ -61,6 +62,24 @@ const viteConfig = defineConfig((mode: ConfigEnv) => {
|
|||||||
locale: Languages.zh_hans,
|
locale: Languages.zh_hans,
|
||||||
localeData: monacoZhHans,
|
localeData: monacoZhHans,
|
||||||
}),
|
}),
|
||||||
|
vitePluginsAutoI18n({
|
||||||
|
// 是否启用插件
|
||||||
|
enabled: false,
|
||||||
|
// 翻译器(支持 doubao 或 deepseek)
|
||||||
|
translator: new VolcengineTranslator({
|
||||||
|
apiKey: '',
|
||||||
|
model: '',
|
||||||
|
}),
|
||||||
|
// translator: new YoudaoTranslator({
|
||||||
|
// appId: '',
|
||||||
|
// appKey: '',
|
||||||
|
// }),
|
||||||
|
// translator: new EmptyTranslator(),
|
||||||
|
// 指定需要翻译文件的目录路径正则(白名单)
|
||||||
|
includePath: [/src\/views\//],
|
||||||
|
// 配置文件生成位置
|
||||||
|
globalPath: './lang',
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
root: process.cwd(),
|
root: process.cwd(),
|
||||||
resolve: { alias },
|
resolve: { alias },
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user