Need help with a piece of code

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:
Spoiler
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);
        }
    }
}
Very ugly stuff. If you can, please help me make it more flexible too ^^;
Thanks!
Does this need anything external to work for setting it up to test and debug?
Are you talling about this code? Just the npm package that yoy can install with:
npm install @vitalets/google-translate-api
and an extra txt file with the item descriptions (each one in a different line). I put it in the data folder as itemDesc.txt. That's about it :/

Though this thing works it's a bit too unstable. Having to hop on to multiple json and text files... I tried multiple times to just simply take the description object from the json file and translate it and update it but always gave me an error -_- The only reason why I'm taking this route is because I don't know many languages and I think machine translation will save a lot of time rather than me going to google translate and making separate files manually.
You could always use Google translate API directly.

As for the code, does it send all 50 requests? Or one at a time after hitting enter? Check the network panel in inspector
Can you explain in details please? Even if I do... won't the problem of translating things likes terms and item descriptions still persist? :0
Where are you running this from at the moment?
@Trihan Script call. I still haven't figured out the updating thing. So I have to reload the game after each test to see the items in the item menu.

Edit: ugh the more I think about this, the more incovenient it seems. I might just drop it. This was good practice though. Rip my brain cells.

@Tsukihime I'm pretty sure it's supposed to translate all descriptions with one press of the button. My laptop's out of battery. I'll see this in the morning.
Did you define $ somewhere in another plugin? It doesn't exist in the default engine.
Oh you can ignore that. I just followed a sample plugin for the structure since I've never written one before...
So how does the one you use in the engine itself look then?
This is the whole plugin contents:
Code:
translate = {};
(function($) {
    $.itemDesc = 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++) {
            itemtranslate(i);
        }
    }
})(translate);

And you can call this with translate.itemDesc();
-_- I realize it's giving me an error 429: too many requests. I tested this way too much huh? XD Anyways, see if it runs for you. I'm tossing this in to the garbage if it continues to be so problematic :")

@Trihan btw forgot to mention! Set the game variable 1 to a language code like hi,ko,ja,zh-CN. Or change it in the code directly for testing.
How do you have the text file set up?
Eh... it's just a simple text file:

Can be used to unlock the Main Entrance.
Can be used to unlock most classrooms.
A can of grape juice. Replenish health up to 25%
A piece of chalk.
A lighter. There's not much oil inside but it's better than nothing.
A pack of cigarettes. Has 'smoking can kill' written on it.
A long scale made of steel.
A lone key with no tags. I wonder where it fits...
A ¥100 Coin.
(etc. etc. many more)

Just these lines. Each is a description for an item.
I feel bad that you're going through so much trouble just to test this 🙁 You don't have to test it... I'm sorta over this... ._.
I hate to say this, but I would recommend just dropping this. Even aside from the issues you're having with it, it's going to produce a translation that sounds awkward and wrong to native speakers of that language, giving a bad impression of your game. There's no substitute for hiring a real person to translate things, and there probably never will be - translation is an art, not a science.
I agree. It's just that I really hate language barriers. I wasn't able to enjoy so many games in my life because I don't speak those languages. Even now I get mad at Japanese VNs because they're Japanese! I wished I could play them even if it meant reading bad translations... at least I'd have an idea about what's going on... Though, I don't think many people will play mine anyways. Yeah I should drop this. At least got some coding practice ^^
Yeah, it's nice to put out your game in many languages (why else would I write a plugin for that!). But automating the translation en masse is not going to work. If you have some understanding of another language, or know someone who does and is willing to help you, you might be able to get an almost-decent translation by manually passing everything through Google Translation and tweaking anything that sounds wrong or comes out incorrect (because yes, sometimes Google Translation is actually wrong).

Obviously the best approach would be to pay someone to translate it for you, but failing that, getting a volunteer who knows both the source and destination language is probably the only way to get a decent translation. Even if they're not a professional translator they might be able to do an okay job.

One thing you could do is take steps to make it as easy as possible for a volunteer to translate your game. As an example, someone using my localization plugin could distribute the POT file with their game, allowing anyone who likes the game and knows another language to grab it, make a translation, and send it to you. I'm sure there are other ways to make it easier to translate, mind you.
I agree. It's just that I really hate language barriers. I wasn't able to enjoy so many games in my life because I don't speak those languages. Even now I get mad at Japanese VNs because they're Japanese! I wished I could play them even if it meant reading bad translations... at least I'd have an idea about what's going on... Though, I don't think many people will play mine anyways. Yeah I should drop this. At least got some coding practice ^^


This might be of interest

Original thread: https://forums.rpgmakerweb.com/threads/need-help-with-a-piece-of-code.124509/