OK, rewritten. Seems to do all that is required and is a lot tidier. I have added 3 more function nodes
- Simulates reading of original file
- Simulates reading of translation file (includes the language code (de) and an array of translated terms.
- Outputs a list of terms to translate with the English translation already supplied as an example
[{"id":"2b4937b94960567e","type":"inject","z":"8428164b1645b4b8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":740,"wires":[["9d5f907fd6f463aa"]]},{"id":"b9a3b4d32add88bb","type":"debug","z":"8428164b1645b4b8","name":"File Out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1220,"y":740,"wires":[]},{"id":"8ecf55a6ba007218","type":"function","z":"8428164b1645b4b8","name":"Creat File With \\n New Translation","func":"/**\n* Example of file input\n* /*\n* language.c\n*\n* Created: 01/02/2022 15:15:09\n* Author:\n* \n* #include < asf.h >\n*\n*\n* #include \"language.h\"\n*\n* //Divers\n* static const char lg_ok_fr[] = { \"Ok\"};\n* static const char lg_ok_en[] = { \"Ok\"};\n* const char * lg_ok[] = { lg_ok_fr, lg_ok_en };\n*\n* Note: Assumes that the language term translation array is in exactly the same order as the terms in the input file\n* msg.language {string} Example 'de'\n* msg.translation {Array} Example ['Ok', 'Nicht', 'Ja', 'L/h', 'L', 'cl', 'h', 's', 'min', '', 'pH+', 'clic 1 sec:\\nStornieren']\n*\n*/\n\n// Read in File \nlet oldFile = msg.payload\n\n// Read in term translations\nconst language = msg.language\nconst translation = msg.translation\n\n// Has to be done this way as some lines have embedded '\\n'\noldFile = oldFile.replace(/:\\n/g, \":\\\\n\")\nlet fileLines = oldFile.split(/(?<!\\\\)\\n/)\n\n// Create array with new language translations\nlet result = []\nlet termCount = 0\nfor (let i = 0; i < fileLines.length; i++) {\n if (fileLines[i].startsWith('const char *')) {\n\n let dataStart = (fileLines[i].indexOf('lg_'))\n let dataEnd = (fileLines[i].indexOf('['))\n let data = fileLines[i].slice(dataStart, dataEnd)\n\n result.push(`static const char ${data}_${language}[] = { \"${translation[termCount]}\" };`)\n result.push(`${fileLines[i].slice(0, -2).trim()}, ${data}_${language} };`)\n\n termCount++\n\n } else {\n result.push(fileLines[i])\n\n }\n\n}\n\n// Tidy output format\nfor (let i = 0; i < result.length; i++) {\n if (result[i].includes('{') && !result[i].includes('{ ')) {\n result[i] = result[i].replace('{', '{ ')\n\n }\n\n if (result[i].includes('}') && !result[i].includes(' }')) {\n result[i] = result[i].replace('}', ' }')\n\n }\n\n}\n\nnode.warn(result)\n\n// create new file\nmsg.result = result\nmsg.payload = result.join('\\n')\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":960,"y":740,"wires":[["b9a3b4d32add88bb"]]},{"id":"9d5f907fd6f463aa","type":"function","z":"8428164b1645b4b8","name":"Read In File To \\n Add Translation","func":"msg.payload = `\n/*\n * language.c\n *\n * Created: 01/02/2022 15:15:09\n * Author: \n */ \n\n#include <asf.h>\n\n#include \"language.h\"\n\n//Divers\nstatic const char lg_ok_fr[] = {\"Ok\"};\nstatic const char lg_ok_en[] = { \"Ok\"};\nconst char * lg_ok[] = { lg_ok_fr, lg_ok_en };\n\nstatic const char lg_no_fr[] = { \"Non\"};\nstatic const char lg_no_en[] = { \"No\"};\nconst char * lg_no[] = { lg_no_fr, lg_no_en };\n\nstatic const char lg_yes_fr[] = { \"Oui\"};\nstatic const char lg_yes_en[] = { \"Yes\"};\nconst char * lg_yes[] = { lg_yes_fr, lg_yes_en };\n\nstatic const char lg_unit_l_per_h_fr[] = {\"L/h\"};\nstatic const char lg_unit_l_per_h_en[] = {\"L/h\"};\nconst char * lg_unit_l_per_h[] = {lg_unit_l_per_h_fr, lg_unit_l_per_h_en};\n\nstatic const char lg_unit_liter_fr[] = {\"L\"};\nstatic const char lg_unit_liter_en[] = {\"L\"};\nconst char * lg_unit_liter[] = {lg_unit_liter_fr, lg_unit_liter_en};\n\t\nstatic const char lg_unit_centi_liter_fr[] = {\"cl\"};\nstatic const char lg_unit_centi_liter_en[] = {\"cl\"};\nconst char * lg_unit_centi_liter[] = {lg_unit_centi_liter_fr, lg_unit_centi_liter_en};\n\nstatic const char lg_unit_h_hour_fr[] = {\"h\"};\nstatic const char lg_unit_h_hour_en[] = {\"h\"};\nconst char * lg_unit_h_hour[] = {lg_unit_h_hour_fr, lg_unit_h_hour_en};\n\nstatic const char lg_unit_s_second_fr[] = {\"s\"};\nstatic const char lg_unit_s_second_en[] = {\"s\"};\nconst char * lg_unit_s_second[] = {lg_unit_s_second_fr, lg_unit_s_second_en};\n\t\nstatic const char lg_unit_minute_fr[] = {\"min\"};\nstatic const char lg_unit_minute_en[] = {\"min\"};\nconst char * lg_unit_minute[] = {lg_unit_minute_fr, lg_unit_minute_en};\t\n\nstatic const char lg_unit_j_jour_fr[] = {\"j\"};\nstatic const char lg_unit_j_jour_en[] = {\"d\"};\nconst char * lg_unit_j_jour[] = {lg_unit_j_jour_fr, lg_unit_j_jour_en};\n\nstatic const char lg_unit_ph_plus_fr[] = {\"pH+\"};\nstatic const char lg_unit_ph_plus_en[] = {\"pH+\"};\nconst char * lg_unit_ph_plus[] = {lg_unit_ph_plus_fr, lg_unit_ph_plus_en};\n\n//Annule \nstatic const char lg_clic1sec_cancel_fr[] = {\"Clic 1sec:\\nAnnule\"};\nstatic const char lg_clic1sec_cancel_en[] = {\"Click 1sec:\\nCancel\"};\nconst char * lg_clic1sec_cancel[] = {lg_clic1sec_cancel_fr, lg_clic1sec_cancel_en};\n`\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":740,"wires":[["ed7d1a55800d3102","22c7a91cb2a97e03"]]},{"id":"ed7d1a55800d3102","type":"function","z":"8428164b1645b4b8","name":"Read In \\n Translation","func":"msg.language = 'de'\nmsg.translation = ['Ok', 'Nicht', 'Ja', 'L/h', 'L', 'cl', 'h', 's', 'min', 't', 'pH+', 'Klicken 1sec:\\\\nStornieren']\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":740,"wires":[["8ecf55a6ba007218"]]},{"id":"22c7a91cb2a97e03","type":"function","z":"8428164b1645b4b8","name":"Get Terms to \\n Translate","func":"\n// Read in File \nlet file = msg.payload\n\n// Has to be done this way as some lines have embedded '\\n'\nfile = file.replace(/:\\n/g, \":\\\\n\")\nlet fileLines = file.split(/(?<!\\\\)\\n/)\n\n// Create array with terms to be translated\nlet result = []\nlet translation = ''\nfor (let i = 0; i < fileLines.length; i++) {\n if (fileLines[i].startsWith('static const char') && fileLines[i].includes('en')) { \n let translationStart = (fileLines[i].indexOf('{')) + 1\n let translationEnd = (fileLines[i].indexOf('}'))\n translation = fileLines[i].slice(translationStart, translationEnd).replaceAll('\"', '').trim()\n\n }\n\n if (fileLines[i].startsWith('const char *')) {\n let dataStart = (fileLines[i].indexOf('lg_')) + 3\n let dataEnd = (fileLines[i].indexOf('['))\n let term = fileLines[i].slice(dataStart, dataEnd)\n\n term = term.replaceAll('_', ' ')\n\n result.push(`Term: ${term}, English: ${translation}`)\n\n }\n\n}\n\nnode.warn(result)\n\nmsg = {}\nmsg.payload = result\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":660,"wires":[["afe32bafb2260001"]]},{"id":"afe32bafb2260001","type":"debug","z":"8428164b1645b4b8","name":"Term List","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":960,"y":660,"wires":[]}]
Edit. Updated to include splits taking embedded \n into account