website/helpers/util.js
RedDucks 4c0f6d4290 Added more locale stuff
Added more entries to the US_en locale. Also added a default "ERROR" locale if the requested one cannot be found. Also changed some internals to fit semantics better and made views use some new locale entries.
2018-11-23 22:58:55 -05:00

65 lines
1.1 KiB
JavaScript

/*
util.js -
small commonly used utilities
*/
const fs = require('fs-extra');
// shows 404 template. takes express response object
function send404(res) {
res.status(404).send('404');
}
function templateReadyUser(req) {
// normal user logged in
const isLoggedIn = req.user ? req.user.role != 'admin' : false;
const user = req.user;
return {
isLoggedIn,
user
};
}
// Returns a list of possible locales:
/*
* [
* {
* region: 'US',
* language: 'en',
* display: 'American English',
* flag_id: 1 // I dunno maybe we display a local list that also displays flags?
* }
* ]
*
*/
function getLocales() {
return [];
}
// Returns a locale
function getLocale(region, language) {
const path = `${__dirname}/../locales/${region}_${language}.json`;
if (fs.pathExistsSync(path)) {
return require(path);
}
console.warn(`Could not find locale ${region}_${language}! Loading default`);
return getDefaultLocale();
}
// Returns the default locale
function getDefaultLocale(locale='default') {
return require(`${__dirname}/../locales/${locale}.json`);
}
module.exports = {
send404,
templateReadyUser,
getLocales,
getLocale,
getDefaultLocale
};