mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-11 22:06:17 -08:00
update deps
This commit is contained in:
1 parent
17ece4c0c8
commit
0aabb42db3
222 files changed
+2791
-2603
No files matched your search
+88
-40
@@ -1,13 +1,20 @@
|
||||
import fs from 'fs';
|
||||
import cpy from 'cpy';
|
||||
import path from 'path';
|
||||
import axios from 'axios';
|
||||
import { glob } from 'glob';
|
||||
import { removeDir } from './utils.js';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
// 配置常量
|
||||
const MAX_CHARS_PER_REQUEST = 512;
|
||||
const MAX_RETRIES = 3;
|
||||
const RETRY_DELAY = 1000; // 1秒
|
||||
const BASE_PATH = 'packages/artplayer-vitepress/docs';
|
||||
const EN_PATH = 'packages/artplayer-vitepress/docs/en';
|
||||
const COPY_DIRS = ['advanced', 'component', 'library', 'plugin', 'start'];
|
||||
|
||||
function splitMarkdown(text, maxChars) {
|
||||
const parts = [];
|
||||
let currentPart = '';
|
||||
@@ -46,42 +53,78 @@ function splitMarkdown(text, maxChars) {
|
||||
return parts;
|
||||
}
|
||||
|
||||
async function fetchWithRetry(url, options, retries = MAX_RETRIES) {
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
if (retries > 0) {
|
||||
console.log(`Retrying... (${MAX_RETRIES - retries + 1}/${MAX_RETRIES})`);
|
||||
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
|
||||
return fetchWithRetry(url, options, retries - 1);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
const translateContent = async (content, targetLanguage) => {
|
||||
try {
|
||||
const TRANSLATE_URL = process.env.TRANSLATE_URL;
|
||||
const response = await axios.post(
|
||||
TRANSLATE_URL,
|
||||
{
|
||||
if (!TRANSLATE_URL) {
|
||||
throw new Error('TRANSLATE_URL is not defined in environment variables');
|
||||
}
|
||||
|
||||
const response = await fetchWithRetry(TRANSLATE_URL, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
content: `The following text is written in VitePress's extended Markdown syntax, Please translate it into ${targetLanguage}, and keep the Markdown format and don't add your explanation:\n\n${content}`,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
timeout: 60000,
|
||||
},
|
||||
);
|
||||
return response.data.data;
|
||||
}),
|
||||
});
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(error.message);
|
||||
return content;
|
||||
console.error(`Translation failed: ${error.message}`);
|
||||
return content; // 返回原文作为回退
|
||||
}
|
||||
};
|
||||
|
||||
const translateMarkdownFiles = async (filePaths, targetLanguage, maxCharsPerRequest) => {
|
||||
let totalParts = 0;
|
||||
let translatedPartsCount = 0;
|
||||
|
||||
for (const filePath of filePaths) {
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
const parts = splitMarkdown(content, maxCharsPerRequest);
|
||||
totalParts += parts.length;
|
||||
class ProgressTracker {
|
||||
constructor(totalParts) {
|
||||
this.totalParts = totalParts;
|
||||
this.translatedPartsCount = 0;
|
||||
}
|
||||
|
||||
console.log(`开始翻译 ${filePaths.length} 个文件,总共 ${totalParts} 部分。`);
|
||||
update() {
|
||||
this.translatedPartsCount++;
|
||||
const progressPercentage = ((this.translatedPartsCount / this.totalParts) * 100).toFixed(2);
|
||||
console.log(`Overall progress: ${progressPercentage}% (${this.translatedPartsCount}/${this.totalParts})`);
|
||||
}
|
||||
}
|
||||
|
||||
const translateMarkdownFiles = async (filePaths, targetLanguage, maxCharsPerRequest) => {
|
||||
// 先计算总共有多少部分需要翻译
|
||||
let totalParts = 0;
|
||||
for (const filePath of filePaths) {
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
totalParts += splitMarkdown(content, maxCharsPerRequest).length;
|
||||
}
|
||||
|
||||
console.log(`Starting translation of ${filePaths.length} files, total ${totalParts} parts.`);
|
||||
const progressTracker = new ProgressTracker(totalParts);
|
||||
|
||||
for (const filePath of filePaths) {
|
||||
console.log(`翻译开始: ${filePath}`);
|
||||
console.log(`Translating: ${path.relative(EN_PATH, filePath)}`);
|
||||
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
const parts = splitMarkdown(content, maxCharsPerRequest);
|
||||
@@ -90,28 +133,33 @@ const translateMarkdownFiles = async (filePaths, targetLanguage, maxCharsPerRequ
|
||||
for (const part of parts) {
|
||||
const translatedPart = await translateContent(part, targetLanguage);
|
||||
translatedParts.push(translatedPart);
|
||||
translatedPartsCount++;
|
||||
const progressPercentage = ((translatedPartsCount / totalParts) * 100).toFixed(2);
|
||||
console.log(`总体翻译进度: ${progressPercentage}%`);
|
||||
progressTracker.update();
|
||||
}
|
||||
|
||||
const translatedContent = translatedParts.join('\n');
|
||||
fs.writeFileSync(filePath, translatedContent);
|
||||
console.log(`翻译结束: ${filePath}`);
|
||||
}
|
||||
};
|
||||
|
||||
async function setupDirectoryStructure() {
|
||||
// 清理并创建目录结构
|
||||
removeDir(EN_PATH);
|
||||
await cpy(path.resolve(BASE_PATH, 'index.md'), path.resolve(EN_PATH));
|
||||
|
||||
// 并行复制所有目录
|
||||
await Promise.all(
|
||||
COPY_DIRS.map((dir) => cpy(path.resolve(BASE_PATH, dir), path.resolve(EN_PATH, dir), { flat: true })),
|
||||
);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const basePath = 'packages/artplayer-vitepress/docs';
|
||||
const enPath = 'packages/artplayer-vitepress/docs/en';
|
||||
removeDir(enPath);
|
||||
cpy(path.resolve(basePath, 'index.md'), path.resolve(enPath));
|
||||
const dirs = ['advanced', 'component', 'library', 'plugin', 'start'];
|
||||
for (let index = 0; index < dirs.length; index++) {
|
||||
const dir = dirs[index];
|
||||
await cpy(path.resolve(basePath, dir), path.resolve(enPath, dir), { flat: true });
|
||||
try {
|
||||
await setupDirectoryStructure();
|
||||
const mdsPaths = glob.sync(path.resolve(EN_PATH, '**/*.md'));
|
||||
await translateMarkdownFiles(mdsPaths, 'English', MAX_CHARS_PER_REQUEST);
|
||||
console.log('Translation completed successfully!');
|
||||
} catch (error) {
|
||||
console.error('Translation failed:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
const mdsPaths = glob.sync(path.resolve(enPath, '**/*.md'));
|
||||
const maxCharsPerRequest = 512;
|
||||
await translateMarkdownFiles(mdsPaths, 'English', maxCharsPerRequest);
|
||||
})();
|
||||
Reference in new issue
Block a user