Add lines of text to a file

Great work! I learn a lot with your code. THANK YOU
And yes there are pitfalls that come out little by little :smile:... here's another one when I thought I was done with everything : I would like to display the comment lines.

Do you have an idea ?

... and if it tells you... While waiting for your answer I noticed a \n in the file I want to modify:
static const char lg_clic1sec_cancel_en[] = {"Click 1sec:\nCancel"};.
As a result, the lines are cut and the final text incomplete::
static const char lg_clic1sec_cancel_en[] = {"Click 1sec"};

[EDIT] line breaks have been resolved and included in the code below.

We are nearing the end ! Here is your last modified code to add comment lines to the newFile:

let file = `
/*
 * language.c
 *
 * Created: 01/02/2022 15:15:09
 *  Author: 
 */ 

#include <asf.h>

#include "language.h"

//Divers
static const char lg_ok_fr[] = {"Ok"};
static const char lg_ok_en[] = {"Ok"};
const char * lg_ok[] = {lg_ok_fr, lg_ok_en};

static const char lg_no_fr[] = {"Non"};
static const char lg_no_en[] = {"No"};
const char * lg_no[] = {lg_no_fr, lg_no_en};

static const char lg_yes_fr[] = {"Oui"};
static const char lg_yes_en[] = {"Yes"};
const char * lg_yes[] = {lg_yes_fr, lg_yes_en};

//Annule 
static const char lg_clic1sec_cancel_fr[] = {"Clic 1sec:\nAnnule"};
static const char lg_clic1sec_cancel_en[] = {"Click 1sec:\nCancel"};
const char * lg_clic1sec_cancel[] = {lg_clic1sec_cancel_fr, lg_clic1sec_cancel_en};	


`
//replace & split pour ne pas avoir de saut de ligne dans le resultat
file = file.replace(/:\n/g, ":\\n");//remplace les :\n par des :\\n
//node.warn(file)
let lines = file.split(/(?<!\\)\n/)//sƩpare ligne par ligne avec les \n en saut de ligne
//node.warn(lines)//affiche lignes par ligne originales


node.warn(lines)

let result = {}
let languageData = {}
for (let i = 0; i < lines.length; i++) {
    if (lines[i].startsWith('static const char')) {
        let translationStart = (lines[i].indexOf('{')) + 1
        let translationEnd = (lines[i].indexOf('}'))
        let translation = lines[i].slice(translationStart, translationEnd).replaceAll('"', '').trim()


        let dataStart = (lines[i].indexOf('lg_')) + 3
        let dataEnd = (lines[i].indexOf('['))
        let data = lines[i].slice(dataStart, dataEnd)

        let parts = data.split('_')

        let language = parts.pop()
        let word = parts.join('_')
 
        languageData = { [word]: translation }

        result[language] = { ...result[language], ...languageData }

    }

}

let deTranslation = ['Ok', 'Nicht', 'Ja']
let words = Object.keys(result.en)
let data = {}
for (let i = 0; i < deTranslation.length; i++) {
    data = { ...data, ...{[words[i]]: deTranslation[i] }}
    result = { ...result, ...{'de': data}}
}


let newFile = ''
let languages = Object.keys(result)
for (let i = 0; i < words.length; i++) {
    for (let j = 0; j < languages.length; j++) {
        newFile = newFile + '\n' + `static const char lg_${words[i]}_${languages[j]}[] = {"${result[languages[j]][words[i]]}"};`
    }
    newFile = newFile + '\n' + `const char * lg_${ words[i] } [] = {`

    let addition = ''
    for (let j = 0; j < languages.length - 1; j++) {
        addition = addition + `lg_${words[i]}_${languages[j]},`
    }
    newFile = newFile + addition + `lg_${words[i]}_${languages[languages.length - 1]}};`
    newFile = newFile + '\n'
}
//if replace (at first)
//newFile = file.replace( /:::/g, ":\n")

//node.warn(newFile)
msg.payload = newFile
return msg