fix email/sms/messaging customised templates #6994

Signed-off-by: si458 <simonsmith5521@gmail.com>

si458 committed May 24, 2025 at 12:47 UTC 5fcffcd608089d1a0f0f77e405e92be6eccaed5b
3 files changed +59 -2
meshmail.js
+21
@@ -116,6 +116,27 @@ module.exports.CreateMeshMail = function (parent, domain) {
116 }
117 }
118
119 + // If no email template found, use the default translated email template
120 + if (((htmlfile == null) || (txtfile == null)) && (lang != null) && (lang != 'en')) {
121 + var translationsPath = obj.parent.path.join(obj.parent.webEmailsPath, 'translations');
122 + var translationsPathHtml = obj.parent.path.join(obj.parent.webEmailsPath, 'translations', name + '_' + lang + '.html');
123 + var translationsPathTxt = obj.parent.path.join(obj.parent.webEmailsPath, 'translations', name + '_' + lang + '.txt');
124 + if (obj.parent.fs.existsSync(translationsPath) && obj.parent.fs.existsSync(translationsPathHtml) && obj.parent.fs.existsSync(translationsPathTxt)) {
125 + htmlfile = obj.parent.fs.readFileSync(translationsPathHtml).toString();
126 + txtfile = obj.parent.fs.readFileSync(translationsPathTxt).toString();
127 + }
128 + }
129 +
130 + // If no translated email template found, use the default email template
131 + if ((htmlfile == null) || (txtfile == null)) {
132 + var pathHtml = obj.parent.path.join(obj.parent.webEmailsPath, name + '.html');
133 + var pathTxt = obj.parent.path.join(obj.parent.webEmailsPath, name + '.txt');
134 + if (obj.parent.fs.existsSync(pathHtml) && obj.parent.fs.existsSync(pathTxt)) {
135 + htmlfile = obj.parent.fs.readFileSync(pathHtml).toString();
136 + txtfile = obj.parent.fs.readFileSync(pathTxt).toString();
137 + }
138 + }
139 +
140 // No email templates
141 if ((htmlfile == null) || (txtfile == null)) { return null; }
142
meshmessaging.js
+20 -2
@@ -397,6 +397,7 @@ module.exports.CreateServer = function (parent) {
397 function getTemplate(templateNumber, domain, lang) {
398 parent.debug('email', 'Getting SMS template #' + templateNumber + ', lang: ' + lang);
399 if (Array.isArray(lang)) { lang = lang[0]; } // TODO: For now, we only use the first language given.
400 + if (lang != null) { lang = lang.split('-')[0]; } // Take the first part of the language, "xx-xx"
401
402 var r = {}, emailsPath = null;
403 if ((domain != null) && (domain.webemailspath != null)) { emailsPath = domain.webemailspath; }
@@ -404,7 +405,7 @@ module.exports.CreateServer = function (parent) {
405 else if (obj.parent.webEmailsPath != null) { emailsPath = obj.parent.webEmailsPath; }
406 if ((emailsPath == null) || (obj.parent.fs.existsSync(emailsPath) == false)) { return null }
407
407 - // Get the non-english email if needed
408 + // Get the non-english sms if needed
409 var txtfile = null;
410 if ((lang != null) && (lang != 'en')) {
411 var translationsPath = obj.parent.path.join(emailsPath, 'translations');
@@ -414,7 +415,7 @@ module.exports.CreateServer = function (parent) {
415 }
416 }
417
417 - // Get the english email
418 + // Get the english sms
419 if (txtfile == null) {
420 var pathTxt = obj.parent.path.join(emailsPath, 'sms-messages.txt');
421 if (obj.parent.fs.existsSync(pathTxt)) {
@@ -422,6 +423,23 @@ module.exports.CreateServer = function (parent) {
423 }
424 }
425
426 + // If no english sms and a non-english language is requested, try to get the default translated sms
427 + if (txtfile == null && (lang != null) && (lang != 'en')) {
428 + var translationsPath = obj.parent.path.join(obj.parent.webEmailsPath, 'translations');
429 + var translationsPathTxt = obj.parent.path.join(obj.parent.webEmailsPath, 'translations', 'sms-messages_' + lang + '.txt');
430 + if (obj.parent.fs.existsSync(translationsPath) && obj.parent.fs.existsSync(translationsPathTxt)) {
431 + txtfile = obj.parent.fs.readFileSync(translationsPathTxt).toString();
432 + }
433 + }
434 +
435 + // If no default translated sms, try to get the default english sms
436 + if (txtfile == null) {
437 + var pathTxt = obj.parent.path.join(obj.parent.webEmailsPath, 'sms-messages.txt');
438 + if (obj.parent.fs.existsSync(pathTxt)) {
439 + txtfile = obj.parent.fs.readFileSync(pathTxt).toString();
440 + }
441 + }
442 +
443 // No email templates
444 if (txtfile == null) { return null; }
445
meshsms.js
+18
@@ -169,6 +169,7 @@ module.exports.CreateMeshSMS = function (parent) {
169 function getTemplate(templateNumber, domain, lang) {
170 parent.debug('email', 'Getting SMS template #' + templateNumber + ', lang: ' + lang);
171 if (Array.isArray(lang)) { lang = lang[0]; } // TODO: For now, we only use the first language given.
172 + if (lang != null) { lang = lang.split('-')[0]; } // Take the first part of the language, "xx-xx"
173
174 var r = {}, emailsPath = null;
175 if ((domain != null) && (domain.webemailspath != null)) { emailsPath = domain.webemailspath; }
@@ -194,6 +195,23 @@ module.exports.CreateMeshSMS = function (parent) {
195 }
196 }
197
198 + // If no english sms and a non-english language is requested, try to get the default translated sms
199 + if (txtfile == null && (lang != null) && (lang != 'en')) {
200 + var translationsPath = obj.parent.path.join(obj.parent.webEmailsPath, 'translations');
201 + var translationsPathTxt = obj.parent.path.join(obj.parent.webEmailsPath, 'translations', 'sms-messages_' + lang + '.txt');
202 + if (obj.parent.fs.existsSync(translationsPath) && obj.parent.fs.existsSync(translationsPathTxt)) {
203 + txtfile = obj.parent.fs.readFileSync(translationsPathTxt).toString();
204 + }
205 + }
206 +
207 + // If no default translated sms, try to get the default english sms
208 + if (txtfile == null) {
209 + var pathTxt = obj.parent.path.join(obj.parent.webEmailsPath, 'sms-messages.txt');
210 + if (obj.parent.fs.existsSync(pathTxt)) {
211 + txtfile = obj.parent.fs.readFileSync(pathTxt).toString();
212 + }
213 + }
214 +
215 // No email templates
216 if (txtfile == null) { return null; }
217