From cc3a6e32b957c60aa29027fa575e4b3ca0c05c64 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 1 Nov 2023 09:12:23 -0300 Subject: data: more language loading refactoring --- src/data/language.js | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/data/language.js b/src/data/language.js index 34de8779..b71e55a2 100644 --- a/src/data/language.js +++ b/src/data/language.js @@ -1,10 +1,13 @@ import {readFile} from 'node:fs/promises'; -// It stands for "HTML Entities", apparently. Cursed. -import he from 'he'; +import chokidar from 'chokidar'; +import he from 'he'; // It stands for "HTML Entities", apparently. Cursed. +import {withAggregate} from '#sugar'; import T from '#things'; +const {Language} = T; + export function processLanguageSpec(spec) { const { 'meta.languageCode': code, @@ -16,23 +19,21 @@ export function processLanguageSpec(spec) { ...strings } = spec; - if (!code) { - throw new Error(`Missing language code (file: ${file})`); - } + withAggregate({message: `Errors validating language spec`}, ({push}) => { + if (!code) { + push(new Error(`Missing language code (file: ${file})`)); + } - if (!name) { - throw new Error(`Missing language name (${code})`); - } + if (!name) { + push(new Error(`Missing language name (${code})`)); + } + }); - const language = new T.Language(); + return {code, intlCode, name, hidden, strings}; +} - Object.assign(language, { - code, - intlCode, - name, - hidden, - strings, - }); +export function initializeLanguageObject() { + const language = new Language(); language.escapeHTML = string => he.encode(string, {useNamedReferences: true}); @@ -43,5 +44,10 @@ export function processLanguageSpec(spec) { export async function processLanguageFile(file) { const contents = await readFile(file, 'utf-8'); const spec = JSON.parse(contents); - return processLanguageSpec(spec); + + const language = initializeLanguageObject(); + const properties = processLanguageSpec(spec); + Object.assign(language, properties); + + return language; } -- cgit 1.3.0-6-gf8a5