37 lines
889 B
TypeScript
37 lines
889 B
TypeScript
|
|
import { Controller, Get, Inject, Param } from '@midwayjs/core';
|
|
import { DictService } from '../service/dict.service';
|
|
|
|
/**
|
|
* 国际化语言包
|
|
*/
|
|
@Controller('/locales')
|
|
export class LocaleController {
|
|
@Inject()
|
|
dictService: DictService;
|
|
|
|
/**
|
|
* 根据语言获取所有翻译项
|
|
* @param lang 语言代码,例如 zh-CN, en-US
|
|
* @returns 返回一个包含所有翻译键值对的 JSON 对象
|
|
*/
|
|
@Get('/:lang')
|
|
async getLocale(@Param('lang') lang: string) {
|
|
// 根据语言代码查找对应的字典
|
|
const dict = await this.dictService.getDict({ name: lang }, ['items']);
|
|
|
|
// 如果字典不存在,则返回空对象
|
|
if (!dict) {
|
|
return {};
|
|
}
|
|
|
|
// 将字典项转换为 key-value 对象
|
|
const locale = dict.items.reduce((acc, item) => {
|
|
acc[item.name] = item.title;
|
|
return acc;
|
|
}, {});
|
|
|
|
return locale;
|
|
}
|
|
}
|