I am writing an app for subtitle translate. But i have a problem. I will use Microsoft Translater Text Api and this api requires this type of request body;
Request body The body of the request is a JSON array. Each array element is a JSON object with a string property named Text, which represents the string to translate. The following limitations apply:
The array can have at most 25 elements. The entire text included in the request cannot exceed 5,000 characters including spaces.
[
{
"Text": "I would really like to drive your car around the block a few times."
},
{
"Text": "Tried something new."
}
]
I tried almost everything. My options data is this;
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"
}
]
First, i need to make this Text key with double quoted. Second, i need to make the value with double quoted not with single quote.This is my full code.
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();
I tried text.replace and all other stuff.
I have solved my issue like this; i suppose the issue was in the accept headers.I have added the
'Accept-Encoding': 'zlib'
to my headers and it fixed it! Anyone who has problem,try this.