lobe-chat
54 строки · 1.8 Кб
1import { consola } from 'consola';
2import { colors } from 'consola/utils';
3import { readFileSync, writeFileSync } from 'node:fs';
4import { resolve } from 'node:path';
5
6import i18nConfig from '../../.i18nrc';
7
8export const readJSON = (filePath: string) => {
9const data = readFileSync(filePath, 'utf8');
10return JSON.parse(data);
11};
12
13export const writeJSON = (filePath: string, data: any) => {
14const jsonStr = JSON.stringify(data, null, 2);
15writeFileSync(filePath, jsonStr, 'utf8');
16};
17
18export const genResourcesContent = (locales: string[]) => {
19let index = '';
20let indexObj = '';
21
22for (const locale of locales) {
23index += `import ${locale} from "./${locale}";\n`;
24indexObj += ` "${locale.replace('_', '-')}": ${locale},\n`;
25}
26
27return `${index}
28const resources = {
29${indexObj}} as const;
30export default resources;
31export const defaultResources = ${i18nConfig.entryLocale};
32export type Resources = typeof resources;
33export type DefaultResources = typeof defaultResources;
34export type Namespaces = keyof DefaultResources;
35export type Locales = keyof Resources;
36`;
37};
38
39export const genNamespaceList = (files: string[], locale: string) => {
40return files.map((file) => ({
41name: file.replace('.json', ''),
42path: resolve(i18nConfig.output, locale, file),
43}));
44};
45
46export const tagBlue = (text: string) => colors.bgBlueBright(colors.black(` ${text} `));
47export const tagYellow = (text: string) => colors.bgYellowBright(colors.black(` ${text} `));
48export const tagGreen = (text: string) => colors.bgGreenBright(colors.black(` ${text} `));
49export const tagWhite = (text: string) => colors.bgWhiteBright(colors.black(` ${text} `));
50
51export const split = (name: string) => {
52consola.log('');
53consola.log(colors.gray(`========================== ${name} ==============================`));
54};
55