From 49b5cf428dbf70169fbc324676e87c13edf7b1fa Mon Sep 17 00:00:00 2001 From: Ash Monty Date: Wed, 22 Mar 2023 20:51:50 +0100 Subject: [PATCH] feat: generate locale list on the fly, minor style changes --- public/assets/css/dropdown.css | 4 +- src/middleware/render-data.js | 58 +++++++++++++ views/partials/header.handlebars | 143 ++----------------------------- 3 files changed, 68 insertions(+), 137 deletions(-) diff --git a/public/assets/css/dropdown.css b/public/assets/css/dropdown.css index 194d4bc..0fcd4f4 100644 --- a/public/assets/css/dropdown.css +++ b/public/assets/css/dropdown.css @@ -11,7 +11,7 @@ .select-box .options-container { max-height: 0; - width: fit-content; + width: min(90vw, 240px); opacity: 0; transition: all 0.4s; overflow: hidden; @@ -35,7 +35,7 @@ } .select-box .options-container.active { - max-height: 240px; + max-height: 320px; opacity: 1; overflow-y: auto; } diff --git a/src/middleware/render-data.js b/src/middleware/render-data.js index 075120d..6c46b83 100644 --- a/src/middleware/render-data.js +++ b/src/middleware/render-data.js @@ -1,5 +1,7 @@ const util = require('../util'); const database = require('../database'); +const fs = require('fs'); +const localeFileNames = fs.readdirSync(`${__dirname}/../../locales`); async function renderDataMiddleware(request, response, next) { if (request.path.startsWith('/assets')) { @@ -14,6 +16,62 @@ async function renderDataMiddleware(request, response, next) { const reqLocale = request.locale; const locale = util.getLocale(reqLocale.language, reqLocale.region); + let localeList = localeFileNames.map(locale => { + const code = locale.replace('.json', '').replace('_', '-'); + + // Check if it's a real language code, or a custom one + if (!code.includes('@')) { + const enDisp = new Intl.DisplayNames([code], { + type: 'language', + languageDisplay: 'standard' + }); + const languageName = enDisp.of(code); + + return { + code, + languageName + }; + } else { + switch (code) { + case 'en@uwu': + return { + code, + languageName: 'English (lolcat)' + }; + + default: + return { + code, + languageName: 'Unknown' + }; + } + } + }); + + // sort the array alphabetically by languageName while making sure that objects with language codes starting with 'en' are at the top + localeList = localeList.sort((a, b) => { + if (a.code.startsWith('en') && !b.code.startsWith('en')) { + return -1; + } else if (!a.code.startsWith('en') && b.code.startsWith('en')) { + return 1; + } else { + return a.languageName.localeCompare(b.languageName); + } + }); + + // move all the objects with language codes containing '@' to the end of the array + localeList = localeList.sort((a, b) => { + if (a.code.includes('@') && !b.code.includes('@')) { + return 1; + } else if (!a.code.includes('@') && b.code.includes('@')) { + return -1; + } else { + return 0; + } + }); + + response.locals.localeList = localeList; + response.locals.locale = locale; response.locals.localeString = reqLocale.toString(); diff --git a/views/partials/header.handlebars b/views/partials/header.handlebars index ddfec35..5c5a692 100644 --- a/views/partials/header.handlebars +++ b/views/partials/header.handlebars @@ -208,141 +208,14 @@
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- -
-
- -
-
- -
-
- - -
+ {{#each localeList}} +
+ + +
+ {{/each}}