Hi!
This is probably my first time posting in 'learning javascript'. I'm studying cs but I still don't know much about javascript. I'm basically still a fetus XD So please bear with me.
I'm using a npm package to translate my game via google translate. I'm done with the message text translating and terms part but I'm stuck with the item description translation part. As I said, I'm still a fetus XD But I managed to put together a little plugin to translate the item descriptions too.
Problem is, all the descriptions aren't translated when I call the function once (from the script call command. Just for testing). I have to mash the enter button like 20 times for all the text to get translated. So I guess there's some issues with the loops or maybe I have to wait for some time and then proceed... Anyways... here's the code, so it's easier for you to identify the issue:
Very ugly stuff. If you can, please help me make it more flexible too ^^;
Thanks!
This is probably my first time posting in 'learning javascript'. I'm studying cs but I still don't know much about javascript. I'm basically still a fetus XD So please bear with me.
I'm using a npm package to translate my game via google translate. I'm done with the message text translating and terms part but I'm stuck with the item description translation part. As I said, I'm still a fetus XD But I managed to put together a little plugin to translate the item descriptions too.
Problem is, all the descriptions aren't translated when I call the function once (from the script call command. Just for testing). I have to mash the enter button like 20 times for all the text to get translated. So I guess there's some issues with the loops or maybe I have to wait for some time and then proceed... Anyways... here's the code, so it's easier for you to identify the issue:
JavaScript:
$.renameItemDesc = function() {
const translate = require('@vitalets/google-translate-api');
var fs = require('fs');
if (fs.existsSync('data/itemsBackup.json') == false) {
var itemContentsB = fs.readFileSync('data/itemDesc.txt');
fs.writeFileSync('data/itemsBackup.json', itemContentsB, function(err, data) {
if (err) {
return console.log(err);
}
});
}
var itemContents = fs.readFileSync('data/itemsBackup.json');
itemContents = JSON.parse(itemContents);
itemContents = JSON.stringify(itemContents);
var desc = fs.readFileSync('data/itemDesc.txt').toString().split("\n");
itemtranslate = function(count) {
if (itemContents.includes(desc[count])) {
translate(desc[count], {
from: 'en',
to: $gameVariables.value(1)
}).then(res => {
itemContents = itemContents.replace(desc[count], res.text);
if (count == desc.length - 1) {
console.log(itemContents);
fs.writeFileSync('data/items.json', itemContents, function(err, data) {
if (err) {
return console.log(err);
}
});
}
}).catch(err => {
console.error(err);
});
}
}
for (i = 0; i < desc.length; i++) {
for (i = 0; i <= 50; i++) {
itemtranslate(i);
}
}
}
Thanks!