Removed _locales.json implementation. Added language parameter to mail.js. Implemented regex check to translation.js. Added language dropdown to email component. Added languages.js

This commit is contained in:
Glenn de Haan
2024-10-10 19:36:07 +02:00
parent 1bc459de66
commit d5c19d021d
6 changed files with 29 additions and 12 deletions

View File

@@ -1,3 +0,0 @@
{
"en": "English"
}

View File

@@ -44,12 +44,13 @@ module.exports = {
*
* @param to
* @param voucher
* @param language
* @return {Promise<unknown>}
*/
send: (to, voucher) => {
send: (to, voucher, language) => {
return new Promise(async (resolve, reject) => {
// Create new translator
const t = translation('email');
const t = translation('email', language);
// Attempt to send mail via SMTP transport
const result = await transport.sendMail({

View File

@@ -18,6 +18,12 @@ const variables = require('./variables');
* @return {(function(key: string): (string))}
*/
module.exports = (module, language = 'en', fallback = 'en') => {
// Prevent users from escaping the filesystem
if(!new RegExp(/^[a-zA-Z]*$/).test(language)) {
log.error(`[Translation] Detected path escalation! Forcing fallback and skipping user input...`);
language = fallback;
}
// Check if translation file exists
if(!fs.existsSync(`${__dirname}/../locales/${language}/${module}.json`)) {
log.warn(`[Translation] Missing translation file: ${__dirname}/../locales/${language}/${module}.json`);
@@ -25,17 +31,11 @@ module.exports = (module, language = 'en', fallback = 'en') => {
log.warn(`[Translation] Using fallback: ${__dirname}/../locales/${language}/${module}.json`);
}
// Get locales mapping
const locales = JSON.parse(fs.readFileSync(`${__dirname}/../locales/_locales.json`, 'utf-8'));
// Get translation file
const translations = JSON.parse(fs.readFileSync(`${__dirname}/../locales/${language}/${module}.json`, 'utf-8'));
// Return translate function
return (key) => {
if(key === '_locales') {
return locales;
}
// Check if key exists within translation file
if(typeof translations[key] === 'undefined') {
log.warn(`[Translation][${language}] Missing for key: ${key}`);

View File

@@ -35,6 +35,7 @@ const types = require('./utils/types');
const time = require('./utils/time');
const bytes = require('./utils/bytes');
const status = require('./utils/status');
const languages = require('./utils/languages');
/**
* Setup Express app
@@ -298,6 +299,7 @@ if(variables.serviceWeb) {
baseUrl: req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : '',
timeConvert: time,
bytesConvert: bytes,
languages,
voucher,
updated: cache.updated
});
@@ -324,7 +326,7 @@ if(variables.serviceWeb) {
});
if(voucher) {
const emailResult = await mail.send(req.body.email, voucher).catch((e) => {
const emailResult = await mail.send(req.body.email, voucher, req.body.language).catch((e) => {
res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/vouchers`);
});

View File

@@ -24,6 +24,16 @@
<input type="email" id="email" name="email" required class="mt-2 block w-full rounded-md border-0 py-1.5 text-gray-900 dark:text-white dark:bg-white/5 ring-1 ring-inset ring-gray-300 dark:ring-white/10 focus:ring-2 focus:ring-sky-600 sm:text-sm sm:leading-6">
</div>
</div>
<div>
<label for="language" class="block text-sm font-medium leading-6 text-gray-900 dark:text-white">Language</label>
<div class="mt-2">
<select id="language" name="language" class="mt-2 block w-full rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-900 dark:text-white dark:bg-white/5 ring-1 ring-inset ring-gray-300 dark:ring-white/10 focus:ring-2 focus:ring-sky-600 sm:text-sm sm:leading-6 [&_*]:text-black">
<% Object.keys(languages).forEach((language) => { %>
<option value="<%= language %>"><%= languages[language] %> (<%= language %>)</option>
<% }); %>
</select>
</div>
</div>
</div>
</div>
</div>

7
utils/languages.js Normal file
View File

@@ -0,0 +1,7 @@
/**
* Exports all languages
*/
module.exports = {
en: 'English',
nl: 'Dutch'
};