mirror of
https://github.com/glenndehaan/unifi-voucher-site.git
synced 2026-03-31 06:24:00 -04:00
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:
43
modules/translation.js
Normal file
43
modules/translation.js
Normal 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];
|
||||
};
|
||||
};
|
||||
@@ -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'
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user