-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtranslate.mjs
206 lines (177 loc) Β· 5.5 KB
/
translate.mjs
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
import fs from 'node:fs/promises';
import path from 'node:path';
import arg from 'arg';
import dotenv from 'dotenv';
import { Listr } from 'listr2';
import picocolors from 'picocolors';
import { log, OpenAiTranslate } from './translate.openai.mjs';
import { sampleTranslations } from './translate.samples.mjs';
import {
walk,
exit,
docsBaseDir,
i18nBaseDir,
validExtensions,
translateDir,
filterFiles,
} from './translate.shared.mjs';
dotenv.config();
const args = arg({
'--file': [String],
'--all': Boolean,
'--sync': Boolean,
'--check': Boolean,
'--locale': String,
'--silent': Boolean,
});
/**
* The list of files to translate from the `--file` argument. File paths are relative to the
* `docs` directory. It can be a single file or a directory.
*
* - This option is mutually exclusive with `--all`.
*
* @type {string[]}
*/
const inputFiles = args['--file'];
/**
* Whether to translate all files in the `docs` directory.
*
* - This option is mutually exclusive with `--file`.
*
* @type {boolean}
*/
const all = args['--all'];
/**
* Whether to filter out files that are already translated in the target locale. This option
* uses Git commit timestamps to compare the source and target files.
*
* - This option should be used in conjunction with `--file` or `--all`.
* - This option is mutually exclusive with `--check`.
*
* @type {boolean}
*/
const sync = args['--sync'];
/**
* Whether to check if files are outdated and need to be translated. If any file is outdated, the
* script will exit with a non-zero status code; otherwise, it will exit with a zero status code.
*
* - This option should be used in conjunction with `--file` or `--all`.
* - This option is mutually exclusive with `--sync`.
*
* Note: This option does not translate any files.
*
* @type {boolean}
*/
const check = args['--check'];
/**
* The target locale to translate the files to. Note that the locale must exist in the `i18n`
* directory. It's recommended to run the Docusaurus write translation command before running this
* script.
*
* @type {string}
*/
const locale = args['--locale'];
/**
* Whether to suppress user confirmations.
*
* @type {boolean}
*/
const silent = args['--silent'];
if (sync && check) {
exit('Cannot use --sync and --check together.');
}
if (all && inputFiles?.length) {
exit('Cannot use --all and --file together.');
}
if (locale) {
await fs.readdir(path.join(i18nBaseDir, locale)).catch(() => {
exit(
`Locale ${locale} does not exist. Did you forget to run the Docusaurus write translation command?`
);
});
}
const getFiles = async () => {
if (inputFiles?.length) {
const result = await Promise.all(
inputFiles.map(async (file) => {
const filePath = path.join(docsBaseDir, file);
const stats = await fs.stat(filePath);
if (stats.isDirectory()) {
return walk(filePath);
}
if (!validExtensions.has(path.extname(filePath))) {
exit(`Invalid file extension: ${file}. Only ${validExtensions.join(', ')} allowed.`);
}
return filePath;
})
);
return result.flat();
}
if (all) {
return walk(docsBaseDir);
}
return [];
};
const confirm = async () =>
new Promise((resolve) => {
process.stdin.once('data', (data) => resolve(data.toString().trim()));
});
const translate = async (locale, files) => {
const filteredFiles = await filterFiles(files, locale, sync, check);
if (filteredFiles.length === 0) {
log(
'No files found to translate. You can provide a list of files with --file or use --all to force translate all files.'
);
exit();
}
const sortedFiles = filteredFiles.slice().sort();
log(`The following files will be translated:`);
for (const slug of sortedFiles) {
log(` - ${picocolors.blue(slug)}`);
}
if (filteredFiles.length > 1 && !silent) {
log(`${filteredFiles.length} files will be translated. Enter "y" to confirm.`);
const confirmation = await confirm();
if (confirmation.toLowerCase() !== 'y') {
exit('Translation cancelled.');
}
}
if (!sampleTranslations[locale] && !silent) {
log(
picocolors.yellow(
`No sample translation found for locale "${locale}", the translation quality may vary. Enter "y" to confirm.`
)
);
const confirmation = await confirm();
if (confirmation.toLowerCase() !== 'y') {
exit('Translation cancelled.');
}
}
const openAiTranslate = new OpenAiTranslate(locale);
const listr = new Listr([], { concurrent: 8 });
for (const file of filteredFiles) {
listr.add({
async task(_, task) {
// eslint-disable-next-line @silverhand/fp/no-mutation
task.title = `Translating ${file}...`;
const content = await fs.readFile(file, 'utf8');
const translated = await openAiTranslate.translate(content, locale, task);
const targetFile = file.replace(docsBaseDir, path.join(i18nBaseDir, locale, translateDir));
await fs.mkdir(path.dirname(targetFile), { recursive: true });
await fs.writeFile(targetFile, translated, 'utf8');
// eslint-disable-next-line @silverhand/fp/no-mutation
task.title = `Done: ${targetFile}`;
},
retry: 1,
});
}
await listr.run();
log(picocolors.green(`β
Completed translation for "${locale}".`));
};
const locales = locale ? [locale] : await fs.readdir(path.join(i18nBaseDir));
const files = await getFiles();
for (const locale of locales) {
// eslint-disable-next-line no-await-in-loop
await translate(locale, files);
}
exit();