mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2025-12-12 09:32:49 +00:00
新增特殊场景翻译
This commit is contained in:
parent
23002bffd7
commit
17cad1b1ca
@ -55,13 +55,15 @@ export class BaseTranslateService {
|
|||||||
|
|
||||||
msgMap: Record<string, string> = {};
|
msgMap: Record<string, string> = {};
|
||||||
|
|
||||||
|
commMap: Record<string, string> = {};
|
||||||
|
|
||||||
// 添加字典映射
|
// 添加字典映射
|
||||||
dictMap: Record<string, string> = {};
|
dictMap: Record<string, string> = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查是否存在锁文件
|
* 检查是否存在锁文件
|
||||||
*/
|
*/
|
||||||
private checkLockFile(type: 'menu' | 'msg'): boolean {
|
private checkLockFile(type: 'menu' | 'msg' | 'comm'): boolean {
|
||||||
const lockFile = path.join(this.basePath, type, '.lock');
|
const lockFile = path.join(this.basePath, type, '.lock');
|
||||||
return fs.existsSync(lockFile);
|
return fs.existsSync(lockFile);
|
||||||
}
|
}
|
||||||
@ -69,7 +71,7 @@ export class BaseTranslateService {
|
|||||||
/**
|
/**
|
||||||
* 创建锁文件
|
* 创建锁文件
|
||||||
*/
|
*/
|
||||||
private createLockFile(type: 'menu' | 'msg'): void {
|
private createLockFile(type: 'menu' | 'msg' | 'comm'): void {
|
||||||
const lockFile = path.join(this.basePath, type, '.lock');
|
const lockFile = path.join(this.basePath, type, '.lock');
|
||||||
fs.writeFileSync(lockFile, new Date().toISOString());
|
fs.writeFileSync(lockFile, new Date().toISOString());
|
||||||
}
|
}
|
||||||
@ -89,13 +91,16 @@ export class BaseTranslateService {
|
|||||||
this.menuMap = {};
|
this.menuMap = {};
|
||||||
this.msgMap = {};
|
this.msgMap = {};
|
||||||
this.dictMap = {};
|
this.dictMap = {};
|
||||||
|
this.commMap = {};
|
||||||
// 加载菜单翻译
|
// 加载菜单翻译
|
||||||
await this.loadTypeTranslations('menu', this.menuMap);
|
await this.loadTypeTranslations('menu', this.menuMap);
|
||||||
|
|
||||||
// 加载消息翻译
|
// 加载消息翻译
|
||||||
await this.loadTypeTranslations('msg', this.msgMap);
|
await this.loadTypeTranslations('msg', this.msgMap);
|
||||||
|
|
||||||
|
// 加载通用消息翻译
|
||||||
|
await this.loadTypeTranslations('comm', this.commMap);
|
||||||
|
|
||||||
// 加载字典翻译
|
// 加载字典翻译
|
||||||
await this.loadDictTranslations();
|
await this.loadDictTranslations();
|
||||||
}
|
}
|
||||||
@ -106,7 +111,7 @@ export class BaseTranslateService {
|
|||||||
* @param map 映射对象
|
* @param map 映射对象
|
||||||
*/
|
*/
|
||||||
private async loadTypeTranslations(
|
private async loadTypeTranslations(
|
||||||
type: 'menu' | 'msg',
|
type: 'menu' | 'msg' | 'comm',
|
||||||
map: Record<string, string>
|
map: Record<string, string>
|
||||||
) {
|
) {
|
||||||
const dirPath = path.join(this.basePath, type);
|
const dirPath = path.join(this.basePath, type);
|
||||||
@ -178,7 +183,7 @@ export class BaseTranslateService {
|
|||||||
* @returns 翻译后的文本
|
* @returns 翻译后的文本
|
||||||
*/
|
*/
|
||||||
translate(
|
translate(
|
||||||
type: 'menu' | 'msg' | 'dict:info' | 'dict:type',
|
type: 'menu' | 'msg' | 'dict:info' | 'dict:type' | 'comm',
|
||||||
language: string,
|
language: string,
|
||||||
text: string
|
text: string
|
||||||
): string {
|
): string {
|
||||||
@ -202,9 +207,10 @@ export class BaseTranslateService {
|
|||||||
this.basePath = path.join(this.app.getBaseDir(), '..', 'src', 'locales');
|
this.basePath = path.join(this.app.getBaseDir(), '..', 'src', 'locales');
|
||||||
const menuLockExists = this.checkLockFile('menu');
|
const menuLockExists = this.checkLockFile('menu');
|
||||||
const msgLockExists = this.checkLockFile('msg');
|
const msgLockExists = this.checkLockFile('msg');
|
||||||
|
const commLockExists = this.checkLockFile('comm');
|
||||||
const dictLockExists = this.checkDictLockFile();
|
const dictLockExists = this.checkDictLockFile();
|
||||||
|
|
||||||
if (!menuLockExists || !msgLockExists || !dictLockExists) {
|
if (!menuLockExists || !msgLockExists || !dictLockExists || !commLockExists) {
|
||||||
const tasks = [];
|
const tasks = [];
|
||||||
if (!msgLockExists) {
|
if (!msgLockExists) {
|
||||||
tasks.push(this.genBaseMsg());
|
tasks.push(this.genBaseMsg());
|
||||||
@ -215,6 +221,9 @@ export class BaseTranslateService {
|
|||||||
if (!dictLockExists) {
|
if (!dictLockExists) {
|
||||||
tasks.push(this.genBaseDict());
|
tasks.push(this.genBaseDict());
|
||||||
}
|
}
|
||||||
|
if (!commLockExists) {
|
||||||
|
tasks.push(this.genCommMsg());
|
||||||
|
}
|
||||||
// 启动旋转动画
|
// 启动旋转动画
|
||||||
const spinner = ['|', '/', '-', '\\'];
|
const spinner = ['|', '/', '-', '\\'];
|
||||||
let index = 0;
|
let index = 0;
|
||||||
@ -464,6 +473,87 @@ export class BaseTranslateService {
|
|||||||
this.createLockFile('msg');
|
this.createLockFile('msg');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成通用消息
|
||||||
|
*/
|
||||||
|
async genCommMsg(){
|
||||||
|
const file = path.join(this.basePath, 'comm', 'zh-cn.json');
|
||||||
|
const scanPath = path.join(this.app.getBaseDir(), '..', 'src', 'modules');
|
||||||
|
const messages = {};
|
||||||
|
|
||||||
|
// 递归扫描目录
|
||||||
|
const scanDir = (dir: string) => {
|
||||||
|
const files = fs.readdirSync(dir);
|
||||||
|
for (const file of files) {
|
||||||
|
const fullPath = path.join(dir, file);
|
||||||
|
const stat = fs.statSync(fullPath);
|
||||||
|
|
||||||
|
if (stat.isDirectory()) {
|
||||||
|
scanDir(fullPath);
|
||||||
|
} else if (file.endsWith('.ts')) {
|
||||||
|
const content = fs.readFileSync(fullPath, 'utf-8');
|
||||||
|
const matches = content.match(
|
||||||
|
/this.translate.comm\((['"])(.*?)\1\)/g
|
||||||
|
);
|
||||||
|
if (matches) {
|
||||||
|
matches.forEach(match => {
|
||||||
|
const message = match.match(/(['"])(.*?)\1/)[2];
|
||||||
|
messages[message] = message;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 开始扫描
|
||||||
|
scanDir(scanPath);
|
||||||
|
|
||||||
|
// 确保目录存在
|
||||||
|
const msgDir = path.dirname(file);
|
||||||
|
if (!fs.existsSync(msgDir)) {
|
||||||
|
fs.mkdirSync(msgDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入文件
|
||||||
|
const text = JSON.stringify(messages, null, 2);
|
||||||
|
fs.writeFileSync(file, text);
|
||||||
|
this.logger.debug('base comm generate success');
|
||||||
|
|
||||||
|
const translatePromises = [];
|
||||||
|
for (const language of this.config.languages) {
|
||||||
|
if (language !== 'zh-cn') {
|
||||||
|
translatePromises.push(
|
||||||
|
this.invokeTranslate(
|
||||||
|
text,
|
||||||
|
language,
|
||||||
|
path.join(this.basePath, 'comm'),
|
||||||
|
'comm'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await Promise.all(translatePromises);
|
||||||
|
this.createLockFile('comm');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用消息翻译
|
||||||
|
* @param text 文本
|
||||||
|
* @returns 翻译后的文本对象,包含各语言的翻译
|
||||||
|
*/
|
||||||
|
comm(text: string) {
|
||||||
|
const translations = {};
|
||||||
|
for (const lang of this.config.languages) {
|
||||||
|
const langFile = path.join(this.basePath, 'comm', `${lang}.json`);
|
||||||
|
if (fs.existsSync(langFile)) {
|
||||||
|
const content = JSON.parse(fs.readFileSync(langFile, 'utf-8'));
|
||||||
|
translations[lang] = content[text] || text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return translations;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调用翻译
|
* 调用翻译
|
||||||
* @param text 文本
|
* @param text 文本
|
||||||
@ -476,7 +566,7 @@ export class BaseTranslateService {
|
|||||||
text: string,
|
text: string,
|
||||||
language: string,
|
language: string,
|
||||||
dirPath: string,
|
dirPath: string,
|
||||||
type: 'menu' | 'msg' | 'dict' = 'msg'
|
type: 'menu' | 'msg' | 'dict' | 'comm' = 'msg'
|
||||||
) {
|
) {
|
||||||
this.logger.debug(`${type} ${language} translate start`);
|
this.logger.debug(`${type} ${language} translate start`);
|
||||||
const response = await axios.post(I18N.DEFAULT_SERVICE_URL, {
|
const response = await axios.post(I18N.DEFAULT_SERVICE_URL, {
|
||||||
|
|||||||
18
src/modules/demo/controller/open/i18n.ts
Normal file
18
src/modules/demo/controller/open/i18n.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { CoolController, BaseController } from '@cool-midway/core';
|
||||||
|
import { DemoI18nService } from '../../service/i18n';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 国际化
|
||||||
|
*/
|
||||||
|
@CoolController({
|
||||||
|
serviceApis: [{
|
||||||
|
method: 'en',
|
||||||
|
summary: '翻译成英文',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
method: 'tw',
|
||||||
|
summary: '翻译成繁体',
|
||||||
|
}],
|
||||||
|
service: DemoI18nService,
|
||||||
|
})
|
||||||
|
export class DemoI18nController extends BaseController {}
|
||||||
29
src/modules/demo/service/i18n.ts
Normal file
29
src/modules/demo/service/i18n.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import { Inject, Provide } from '@midwayjs/core';
|
||||||
|
import { BaseTranslateService } from '../../base/service/translate';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 国际化服务
|
||||||
|
*/
|
||||||
|
@Provide()
|
||||||
|
export class DemoI18nService {
|
||||||
|
@Inject()
|
||||||
|
translate: BaseTranslateService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 翻译成英文
|
||||||
|
*/
|
||||||
|
async en() {
|
||||||
|
const value = this.translate.comm('一个很Cool的框架')['en'];
|
||||||
|
console.log(value);
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 翻译成繁体
|
||||||
|
*/
|
||||||
|
async tw() {
|
||||||
|
const value = this.translate.comm('一个很Cool的框架')['zh-tw'];
|
||||||
|
console.log(value);
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user