Implemented the translation.js module. Implemented the 'TRANSLATION_DEBUG' environment variable. Updated the README.md to include the Translations chapter. Moved en.json to en/email.json to better utilize Crowdin

This commit is contained in:
Glenn de Haan
2024-10-09 11:37:47 +02:00
parent 79b75cd56c
commit 1b1a934f87
6 changed files with 89 additions and 21 deletions

43
modules/translation.js Normal file
View File

@@ -0,0 +1,43 @@
/**
* Import base packages
*/
const fs = require('fs');
/**
* Import own modules
*/
const variables = require('./variables');
/**
* Translation returns translator function
*
* @param language
* @param module
* @return {(function(key: string): (string))}
*/
module.exports = (language = 'en', module) => {
// Check if translation file exists
if(!fs.existsSync(`${__dirname}/../locales/${language}/${module}.json`)) {
throw new Error(`[Translation] Missing translation file: ${__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') {
throw new Error(`[Translation][${language}] Missing for key: ${key}`);
}
// Check if debugging is enabled. If enabled only return key
return variables.translationDebug ? key : translations[key];
};
};

View File

@@ -41,6 +41,7 @@ module.exports = {
smtpUsername: config('smtp_username') || process.env.SMTP_USERNAME || '',
smtpPassword: config('smtp_password') || process.env.SMTP_PASSWORD || '',
logLevel: config('log_level') || process.env.LOG_LEVEL || 'info',
translationDebug: config('translation_debug') || (process.env.TRANSLATION_DEBUG === 'true') || false,
gitTag: process.env.GIT_TAG || 'master',
gitBuild: fs.existsSync('/etc/unifi_voucher_site_build') ? fs.readFileSync('/etc/unifi_voucher_site_build', 'utf-8') : 'Development'
};