Make languages on frontend included individually as a language .js file. Fix issue with lib/build/render.js page. I **think** this is good now.

This commit is contained in:
Thomas Lynch
2023-01-16 22:19:15 +11:00
parent 5bf792649b
commit 670119148c
7 changed files with 28 additions and 17 deletions
+1 -2
View File
@@ -1,7 +1,6 @@
/* eslint-disable no-unused-vars */
/* globals LANG */
const CURRENTLANG = document.head.dataset.lang;
const __ = (key) => {
//TODO: we'll see if this needs to be more advanced in future
return LANG[CURRENTLANG][key];
return LANG[key];
};
+11 -4
View File
@@ -426,10 +426,10 @@ async function custompages() {
captchaOptions: config.get.captchaOptions,
commit,
version,
language: config.get.language,
__: i18n.__,
globalLanguage: config.get.language,
};
i18n.setLocale(locals, config.get.language);
i18n.init(locals);
locals.setLocale(config.get.language);
return gulp.src([
`${paths.pug.src}/custompages/*.pug`,
`${paths.pug.src}/pages/404.pug`,
@@ -463,10 +463,17 @@ const captchaOptions = ${JSON.stringify(reducedCaptchaOptions)};
const SERVER_TIMEZONE = '${Intl.DateTimeFormat().resolvedOptions().timeZone}';
const settings = ${JSON.stringify(config.get.frontendScriptDefault)};
const extraLocals = ${JSON.stringify({ meta: config.get.meta, reverseImageLinksURL: config.get.reverseImageLinksURL })};
const LANG = ${JSON.stringify(i18n.getCatalog())};
`;
fs.writeFileSync('gulp/res/js/locals.js', locals);
fs.mkdirSync(`${paths.scripts.dest}lang/`);
Object.entries(i18n.getCatalog())
.forEach(entry => {
const [lang, dict] = entry;
const langScript = `const LANG = ${JSON.stringify(dict)};`;
fs.writeFileSync(`${paths.scripts.dest}lang/${lang}.js`, langScript);
});
// const pugRuntimeFuncs = pugRuntime(['classes', 'style', 'attr', 'escape']);
// fs.writeFileSync('gulp/res/js/pugruntime.js', pugRuntimeFuncs);
+5 -4
View File
@@ -39,9 +39,10 @@ const updateLocals = () => {
hcaptchaSiteKey: hcaptcha.siteKey,
captchaOptions,
globalAnnouncement,
language,
__: i18n.__,
globalLanguage: language,
};
i18n.init(renderLocals);
renderLocals.setLocale(language);
};
updateLocals();
@@ -58,9 +59,9 @@ module.exports = async (htmlName=null, templateName=null, options=null, json=nul
};
//NOTE: will this cause issues with global locale?
if (options.board && options.board.settings) {
i18n.setLocale(mergedLocals, options.board.settings.language);
renderLocals.setLocale(options.board.settings.language);
} else {
i18n.setLocale(renderLocals, language);
renderLocals.setLocale(language);
}
html = pug.renderFile(`${templateDirectory}${templateName}`, mergedLocals);
}
+1
View File
@@ -6,6 +6,7 @@ const i18n = require('i18n')
i18n.configure({
directory: path.join(__dirname, '/../../locales'),
locales: ['en', 'pt'],
defaultLocale: 'en',
retryInDefaultLocale: false,
cookie: null,
+3 -3
View File
@@ -91,9 +91,9 @@ const config = require(__dirname+'/lib/misc/config.js')
app.locals.hcaptchaSiteKey = hcaptcha.siteKey;
app.locals.globalAnnouncement = globalAnnouncement;
app.locals.captchaOptions = captchaOptions;
app.locals.language = language
app.locals.__ = i18n.__;
i18n.setLocale(app.locals, language);
app.locals.globalLanguage = language;
i18n.init(app.locals);
app.locals.setLocale(language);
};
loadAppLocals();
redis.addCallback('config', loadAppLocals);
+6 -1
View File
@@ -5,6 +5,10 @@ meta(name='viewport' content='width=device-width initial-scale=1')
noscript
style .jsonly { display: none!important; } .user-id { cursor: auto!important; }
//- whether this page is rendered for a board
- const isBoard = board != null;
//- general meta and opengraph meta tags
if isBoard
if board.settings.description
@@ -29,7 +33,8 @@ if isBoard && board.settings.customCss
include ../../gulp/res/icons/html_code.html
//- language pack
//- TODO?
- const languageScript = isBoard ? board.settings.language : language;
script(src=`/js/lang/${languageScript}.js?v=${commit}`)
//- main script
script(src=`/js/all.js?v=${commit}&ct=${captchaOptions.type}`)
+1 -3
View File
@@ -1,8 +1,6 @@
doctype html
html
//- whether this page is rendered for a board
- const isBoard = board != null;
head(data-lang=(isBoard ? board.settings.language : language))
head
include includes/head.pug
block head
body#top