我正在写一个字幕翻译应用程序。但我有一个问题。我将使用微软Translater文本API和这个API需要这种类型的请求体;
请求主体请求的主体是一个JSON数组。每个数组元素都是一个JSON对象,具有一个名为Text的字符串属性,表示要翻译的字符串。以下限制适用:
数组最多可以包含25个元素。请求中包含的整个文本不能超过5,000个字符(包括空格)。
[
{
"Text": "I would really like to drive your car around the block a few times."
},
{
"Text": "Tried something new."
}
]
字符串
我几乎什么都试过了。我的期权数据是这样的;
data: [
{ Text: 'Oh! What a nice day!\r' },
{ Text: "Keep going this way! We'll\rsee the navy base soon.\r" },
{ Text: 'Koby! You are good!\r' },
{ Text: 'We got here indeed!\r' },
{ Text: 'Sure...\r' },
{ Text: "That's the basic skill of a sailor!\r" },
{ Text: "You're still in a good mood, Luffy.\r" },
{ Text: 'I heard that he was caught there!\r' },
{ Text: 'The famous bounty hunter Roronoa Zoro!\r' },
{ Text: 'A bloody animal! Hunting down pirates!\r' },
{ Text: '<i>He cuts them into pieces!</i>\r' },
{ Text: `<i>He's a man, known as "the demon".</i>\r` },
{
Text: '(Episode 2) "The Great Swordsman!\rBounty Hunter Roronoa Zoro"\r'
},
{
Text: '(Episode 2) "Daikengou Arawaru!\rKaizoku-kari Roronoa Zoro"\r'
},
{
Text: '<i>(Based on Manga ch.3: "Enter Zoro Pirate\rHunter", ch.4: "The Great Captain Morgan"...)</i>\r'
},
{
Text: '<i>(...and ch.5: "The Pirate\rKing And The Master Swordsman")</i>\r'
},
{ Text: 'We are finally here!\r' },
{ Text: 'Hey Luffy,\r' },
{ Text: "Don't be stupid enough to see him!\r" },
{ Text: "I havn't decided yet\r" },
{ Text: 'depends on whether he\ris a good guy or not.\r' },
{ Text: "He is a bad guy! That's\rwhy he was caught!\r" },
{ Text: 'Uh, is Zoro captured at the base?\r' },
{ Text: "You can't mention that name here!\r" },
{
Text: "Let's go there first! Don't\ryou want to join the navy?\r"
}
]
型
首先,我需要使这个文本键用双引号。其次,我需要使值用双引号而不是单引号。这是我的完整代码。
const axios = require('axios');
const fs = require('fs/promises');
const { format } = require('path');
const { v4: uuidv4 } = require('uuid');
async function translateandsave(subtitleBatch) {
const key = "Text";
const formattedData = subtitleBatch.map(text => ({ [key]: `"${text}"` }));
const options = {
method: 'POST',
url: 'https://microsoft-translator-text.p.rapidapi.com/translate',
params: {
'to[0]': 'tr',
'api-version': '3.0',
profanityAction: 'NoAction',
textType: 'plain'
},
headers: {
'content-type': 'application/json',
'X-RapidAPI-Key': 'secret!',
'X-RapidAPI-Host': 'microsoft-translator-text.p.rapidapi.com'
},
data: subtitleBatch.map((text) => ({ "Text": text }))
};
console.log(options);
try {
const response = await axios.request(options);
const translatedSubtitles = response.data.map(entry => entry.translations[0].text);
const newSubtitleFilePath = `temp_${uuidv4()}\\translatedSubtitle.srt`;
await fs.appendFile(newSubtitleFilePath,{ flag: 'wx' }, translatedSubtitles.join('\n'));
} catch (error) {
console.error(`Batch translate error:`, error.message);
}
}
const originalSubtitleFilePath = 'temp_9985f505-1aa0-436b-bb36-025c77759de1\\subtitle_1.srt';
async function main() {
try {
const originalSubtitleContent = await fs.readFile(originalSubtitleFilePath, 'utf-8');
const lines = originalSubtitleContent.split('\n');
const batchSize = 25;
let subtitleBatch = [];
let subcounts = [];
let timecodes = [];
let texts = [];
let iscount = true;
let istimecode = false;
let istext = false;
let textcount = 0;
for (const line of lines) {
if(line.trim() === '') {
iscount = true;
istimecode = false;
istext = false;
textcount = 0;
subtitleBatch.push(texts[texts.length-1]);
if(subtitleBatch.length === batchSize){
await translateandsave(subtitleBatch)
subtitleBatch = [];
}
}
else if (iscount===true){
subcounts.push(line);
iscount = false;
istimecode = true;
}
else if (istimecode===true){
timecodes.push(line);
istimecode = false;
istext = true;
}
else if(istext === true){
if(textcount === 0){
texts.push(line);
}
else{
texts[texts.length-1] += line
}
textcount++;
}
}
} catch (error) {
console.error('Error:', error.message);
}
}
main();
型
我试过替换和其他东西。
1条答案
按热度按时间pw136qt21#
我已经解决了这样的问题;我想问题是在接受头。我已经添加了
'Accept-Encoding': 'zlib'
到我的头,它修复了它!任何人谁有问题,试试这个。字符串