diff --git a/mod/utils/languageTemplates.js b/mod/utils/languageTemplates.js index 4fc7751f5..1c4b6d3b5 100644 --- a/mod/utils/languageTemplates.js +++ b/mod/utils/languageTemplates.js @@ -1,31 +1,43 @@ const getFrom = require('../provider/getFrom') +const getTemplate = require('../workspace/getTemplate') + const workspaceCache = require('../workspace/cache') module.exports = async (params) => { - if (params.template === undefined) return; + if (params.template === undefined) return; + + // Set english as default template language. + params.language ??= 'en' + + const workspace = await workspaceCache() + + if (!Object.hasOwn(workspace.templates, params.template)) { - // Set english as default template language. - params.language ??= 'en' + console.warn(`Template ${params.template} not found.`) + return params.template; + } - const workspace = await workspaceCache() + // NOT a language template + if (workspace.templates[params.template].src) { - if (!Object.hasOwn(workspace.templates, params.template)) { + const nonLanguage = await getTemplate(workspace.templates[params.template]) - console.warn(`Template ${params.template} not found.`) - return params.template; - } + return nonLanguage.template + } - const allLanguages = workspace.templates[params.template] + const allLanguages = workspace.templates[params.template] - let template = Object.hasOwn(allLanguages, params.language)? allLanguages[params.language]: allLanguages.en; + let template = Object.hasOwn(allLanguages, params.language) + ? allLanguages[params.language] + : allLanguages.en; - if (typeof template === 'string' && Object.hasOwn(getFrom, template.split(':')[0])) { + if (typeof template === 'string' && Object.hasOwn(getFrom, template.split(':')[0])) { - // Get template from method. - template = await getFrom[template.split(':')[0]](template) - } + // Get template from method. + template = await getFrom[template.split(':')[0]](template) + } - return template + return template } diff --git a/mod/view.js b/mod/view.js index bd7a9797e..558ff4683 100644 --- a/mod/view.js +++ b/mod/view.js @@ -40,6 +40,11 @@ module.exports = async (req, res) => { const template = await languageTemplates(params) + if (!template) { + res.status(400).send(`Template undefined`) + return; + } + const view = template.replace(/{{2}([A-Za-z][A-Za-z0-9]*)}{2}/g, matched => { // regex matches {{ or }}