-
Notifications
You must be signed in to change notification settings - Fork 2
/
i18n.config.ts
266 lines (239 loc) · 6.76 KB
/
i18n.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { Database } from 'bun:sqlite';
import fs from 'node:fs';
import path from 'node:path';
import OpenAI from 'openai';
const i18nIndex = `
/* eslint-disable @typescript-eslint/no-explicit-any */
import { i18nSource } from './i18n-source';
let lastLang = '';
const langs: Record<string, string> = { zh: 'zh', en: 'en', jp: 'jp', es: 'es', fr: 'fr' };
export const languageList = [
{ value: 'en', label: 'English' },
{ value: 'zh', label: '中文' },
{ value: 'jp', label: '日本語' },
{ value: 'es', label: 'Español' },
{ value: '', label: 'Français' },
];
const nofound = {
zh: ' ',
en: ' ',
jp: ' ',
es: ' ',
fr: ' ',
};
export const getLanguage = () => {
if (typeof window === 'undefined') {
return 'en';
}
if (lastLang) {
return lastLang;
}
let lng = localStorage.getItem('lang');
if (!lng) {
lng = (navigator.language || navigator.language).slice(0, 2).toLowerCase();
}
lastLang = langs[lng] || 'en';
return lastLang;
};
export function i18nKey(str: string): string {
return str;
}
function template(strings: any, ...values: any): string {
let result = '';
for (let i = 0; i < strings.length; i++) {
result += strings[i];
if (i < values.length) {
result += values[i];
}
}
return result;
}
export function i18n(strings: any, ...values: any): string {
const result = template(strings, values);
return ((i18nSource as any)[result] || nofound)[getLanguage()];
}
export function i18nFromKey(key: string) {
const obj = (i18nSource as any)[key];
if (!obj) {
return key;
}
return obj[getLanguage()];
}
export function i18nObj(strings: any, ...values: any): Record<string, string> {
const result = template(strings, values);
return (i18nSource as any)[result] || nofound;
}
export function setLanguage(lng: 'zh' | 'en' | 'jp' | 'es') {
if (typeof window === 'undefined') {
return;
}
lastLang = lng;
localStorage.setItem('lang', lng);
location.reload();
}
`;
const aiMessage = (text: string) => {
return `请把我接下来的文本翻译成中文、英文、日文、西班牙文, 法语 记得首字母要大写
比如:你好
你的输出格式为 JSON: {zh:"你好", en:"Hello", jp:"こんにちは", es:"Hola", fr:"Français"}
请不要有:好的,这是..., 或者:以下是... 这种回答的前缀,直接回答JSON即可
记住请直接回答上面的 JSON 格式
记住请直接回答上面的 JSON 格式
记住请直接回答上面的 JSON 格式
现在请你翻译:${text}`;
};
function parseJsonText(jsonString: string): Record<string, string> | null {
let openBraceIndex = -1;
let closeBraceIndex = -1;
for (let i = 0; i < jsonString.length; i++) {
if (jsonString[i] === '{' && openBraceIndex === -1) {
openBraceIndex = i;
} else if (jsonString[i] === '}') {
closeBraceIndex = i;
}
}
if (openBraceIndex !== -1 && closeBraceIndex !== -1) {
const extractedJsonString = jsonString.slice(openBraceIndex, closeBraceIndex + 1);
try {
const extractedJson = JSON.parse(extractedJsonString);
return extractedJson;
} catch (error) {
return null;
}
} else {
return null;
}
}
const openai = new OpenAI({
apiKey: process.env.OPENAI_CLIENT,
});
const tableName = 'i18n';
const outdir = './src/lib/i18n';
const outfile = outdir + '/i18n-source.ts';
const outIndexFile = outdir + '/index.ts';
const typeRegx = /(\.svelte|\.ts)/;
// 递归遍历目录并查找匹配的文本
function findI18nTextInDirectory(dirPath: string, resultArray: string[]) {
const files = fs.readdirSync(dirPath);
files.forEach((file) => {
const filePath = path.join(dirPath, file);
if (fs.statSync(filePath).isDirectory()) {
// 如果是目录,递归处理
findI18nTextInDirectory(filePath, resultArray);
} else {
if (!typeRegx.test(filePath)) {
return;
}
// 如果是文件,读取文件内容并查找匹配的文本
const fileContent = fs.readFileSync(filePath, 'utf8');
{
const matches = fileContent.match(/(i18n|i18nObj)`[^`]+`/g);
if (matches) {
matches.forEach((match) => {
// 提取匹配的文本,并去掉 i18n` 和 ` 符号
const i18nText = match.replace(/(i18n|i18nObj)`|`/g, '');
resultArray.push(i18nText);
});
}
}
{
const matches = fileContent.match(/i18nKey\('([^']+)'\)/g);
if (matches) {
matches.forEach((match) => {
const i18nText = match.replace(/i18nKey\('|'\)/g, '');
resultArray.push(i18nText);
});
}
}
{
const matches = fileContent.match(/i18nKey\("([^"]+)"\)/g);
if (matches) {
matches.forEach((match) => {
const i18nText = match.replace(/i18nKey\("|"\)/g, '');
resultArray.push(i18nText);
});
}
}
{
const matches = fileContent.match(/i18nKey\(`([^`]+)`\)/g);
if (matches) {
matches.forEach((match) => {
const i18nText = match.replace(/i18nKey\(`|`\)/g, '');
resultArray.push(i18nText);
});
}
}
}
});
}
async function insertKv(db: Database, k: string, v: string) {
return db.query(`insert into ${tableName} (k, v) values ($k, $v)`).values({ $k: k, $v: v });
}
async function loadI18nTexts() {
const texts: string[] = [];
findI18nTextInDirectory('./src', texts);
return Array.from(new Set(texts));
}
async function start(texts: string[]) {
const db = new Database('i18n.sqlite', { create: true });
await db
.query(
`
CREATE TABLE IF NOT Exists ${tableName} (
k TEXT PRIMARY KEY,
v TEXT
);
`,
)
.run();
const out: Record<string, { zh: string; en: string; jp: string; es: string }> = {};
for (const text of texts) {
const old = (await db
.query(`select * from ${tableName} where k = $text`)
.get({ $text: text })) as {
k: string;
v: string;
};
if (!old) {
console.log('tranlsate new:', text);
const completion = await openai.chat.completions.create({
messages: [{ role: 'user', content: aiMessage(text) }],
model: 'gpt-3.5-turbo-0613',
});
const msg = completion.choices[0].message.content;
try {
if (!msg) {
console.log('openai error, not msg:', text, msg);
continue;
}
const obj = parseJsonText(msg);
if (!obj) {
throw new Error('openai parse error:' + msg);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
out[text] = obj as any;
await insertKv(db, text, JSON.stringify(out[text]));
} catch (err) {
console.log('openai error, parse error:', text, msg, err);
}
} else {
// console.log('load by sqlite:', text);
out[text] = JSON.parse(old.v);
}
}
return out;
}
async function pipe() {
const texts = await loadI18nTexts();
const out = await start(texts);
fs.writeFileSync(
outfile,
`
export const i18nSource = ${JSON.stringify(out)}
`,
);
fs.writeFileSync(outIndexFile, i18nIndex);
console.log('Auto i18n sentences: ', Object.keys(out).length);
Bun.spawnSync(['bunx', 'prettier', outdir, '--write']);
}
pipe();