2023-08-29 15:38:07 +08:00
|
|
|
import fs from 'fs';
|
2023-08-29 18:39:28 +08:00
|
|
|
import fsExtra from 'fs-extra';
|
2023-08-31 22:00:18 +08:00
|
|
|
// import translate from 'google-translate-api-x';
|
2023-08-30 19:46:37 +08:00
|
|
|
import { readFile, writeFile } from 'node:fs/promises';
|
2023-08-29 15:38:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A workaround
|
|
|
|
* because of `readdir` doesn't work with { recursive: true }
|
|
|
|
* see : https://github.com/nodejs/node/issues/48858
|
|
|
|
*/
|
2023-08-30 19:46:37 +08:00
|
|
|
var walk = (dir) => {
|
2023-08-29 15:38:07 +08:00
|
|
|
try {
|
2023-08-30 19:46:37 +08:00
|
|
|
var results = [];
|
|
|
|
var list = fs.readdirSync(dir);
|
|
|
|
list.forEach((file) => {
|
|
|
|
file = dir + '/' + file;
|
|
|
|
var stat = fs.statSync(file);
|
|
|
|
if (stat && stat.isDirectory()) {
|
|
|
|
/* Recurse into a subdirectory */
|
|
|
|
results = results.concat(walk(file));
|
2023-08-29 18:39:28 +08:00
|
|
|
} else {
|
2023-08-30 19:46:37 +08:00
|
|
|
/* Is a file */
|
|
|
|
results.push(file);
|
2023-08-29 18:39:28 +08:00
|
|
|
}
|
2023-08-29 15:38:07 +08:00
|
|
|
});
|
2023-08-30 19:46:37 +08:00
|
|
|
return results;
|
2023-08-29 15:38:07 +08:00
|
|
|
} catch (error) {
|
2023-08-29 18:39:28 +08:00
|
|
|
return [];
|
2023-08-29 15:38:07 +08:00
|
|
|
}
|
|
|
|
};
|
2023-08-29 18:39:28 +08:00
|
|
|
|
2023-08-30 19:46:37 +08:00
|
|
|
const TEMP_DOC_FILE_PATH = 'docs/_apis';
|
|
|
|
const DOC_FILE_PATH = 'docs/apis';
|
2023-08-29 18:39:28 +08:00
|
|
|
|
|
|
|
let errorMessage = ``;
|
|
|
|
|
2023-08-29 15:38:07 +08:00
|
|
|
const main = async () => {
|
2023-08-29 18:39:28 +08:00
|
|
|
// if node.js version not equal 18 ,throw error
|
|
|
|
|
2023-08-30 19:46:37 +08:00
|
|
|
const RawFiles = walk(TEMP_DOC_FILE_PATH); //Readdir(TEMP_DOC_FILE_PATH, { recursive: true });
|
2023-08-29 15:38:07 +08:00
|
|
|
// const files = await readdir(dirPath, { recursive: true }); // it is a bug: https://github.com/nodejs/node/issues/48858
|
2023-08-30 19:46:37 +08:00
|
|
|
const AllFiles = walk(DOC_FILE_PATH, { recursive: true });
|
2023-08-29 15:38:07 +08:00
|
|
|
|
2023-08-29 18:39:28 +08:00
|
|
|
for (let i = 0; i < RawFiles.length; i++) {
|
|
|
|
let file = AllFiles[i];
|
|
|
|
let rawFile = RawFiles[i];
|
|
|
|
|
|
|
|
const en_path = rawFile.replace('.md', '.en.md').replace(TEMP_DOC_FILE_PATH, DOC_FILE_PATH);
|
|
|
|
const zh_path = rawFile.replace('.md', '.zh.md').replace(TEMP_DOC_FILE_PATH, DOC_FILE_PATH);
|
|
|
|
let has_en_file = false;
|
|
|
|
let has_zh_file = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
has_en_file = fs.lstatSync(en_path).isFile();
|
|
|
|
has_zh_file = fs.lstatSync(zh_path).isFile();
|
|
|
|
} catch (error) {
|
|
|
|
errorMessage = errorMessage + '\n' + error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_en_file && has_zh_file) {
|
|
|
|
errorMessage = errorMessage + '\n' + `file: ${file} has been translated`;
|
|
|
|
console.log(`file: ${file} has been translated`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const content = await readFile(rawFile);
|
|
|
|
|
2023-08-30 19:46:37 +08:00
|
|
|
const zh_file_name = zh_path.split('/').pop().split('.')[0];
|
|
|
|
const en_file_name = en_path.split('/').pop().split('.')[0];
|
|
|
|
|
|
|
|
const zh_header = `---
|
|
|
|
title: ${zh_file_name}
|
|
|
|
---
|
|
|
|
|
2023-08-31 22:00:18 +08:00
|
|
|
> 📋 中文文档还在翻译中... 欢迎PR
|
|
|
|
|
2023-08-30 19:46:37 +08:00
|
|
|
`;
|
|
|
|
const en_header = `---
|
|
|
|
title: ${en_file_name}
|
|
|
|
---
|
|
|
|
|
|
|
|
`;
|
2023-08-29 18:39:28 +08:00
|
|
|
const en_content = content.toString().replaceAll('.md', '.en.md');
|
|
|
|
const zh_content = content.toString().replaceAll('.md', '.zh.md');
|
|
|
|
|
|
|
|
/** create en_US file */
|
|
|
|
|
|
|
|
await fsExtra.ensureFile(en_path);
|
2023-08-30 19:46:37 +08:00
|
|
|
await writeFile(en_path, en_header + en_content);
|
2023-08-29 15:38:07 +08:00
|
|
|
|
|
|
|
let hasTranslated = false;
|
|
|
|
try {
|
2023-08-29 18:39:28 +08:00
|
|
|
hasTranslated = fs.lstatSync(zh_path).isFile();
|
2023-08-29 15:38:07 +08:00
|
|
|
} catch (error) {}
|
|
|
|
|
|
|
|
if (!hasTranslated) {
|
2023-08-31 22:00:18 +08:00
|
|
|
// const res = await translate(zh_content, { from: 'en', to: 'zh-CN' }).catch((error) => {
|
|
|
|
// errorMessage = errorMessage + '\n' + `TRANSLATE ERROR: ${file}:${zh_path} \n `;
|
|
|
|
// return { text: '' };
|
|
|
|
// });
|
|
|
|
// writeFile(zh_path, zh_header + res.text);
|
|
|
|
writeFile(zh_path, zh_header + zh_content);
|
2023-08-29 15:38:07 +08:00
|
|
|
}
|
|
|
|
}
|
2023-08-29 18:39:28 +08:00
|
|
|
fs.writeFileSync('.translate-info.txt', errorMessage);
|
2023-08-30 19:46:37 +08:00
|
|
|
// rm('docs/apis/modules.en.md');
|
|
|
|
// rm('docs/apis/modules.zh.md');
|
2023-08-29 15:38:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
main();
|