From 5a899ac0a53850341f7b23c9f51f61ad91358d40 Mon Sep 17 00:00:00 2001 From: tecc Date: Tue, 19 Sep 2023 21:13:56 +0200 Subject: [PATCH] change(rewrite): Loading morphemes.js with a hack --- new/backend/src/locales.ts | 67 ++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/new/backend/src/locales.ts b/new/backend/src/locales.ts index 928cc0381..a000058d6 100644 --- a/new/backend/src/locales.ts +++ b/new/backend/src/locales.ts @@ -50,12 +50,12 @@ export async function loadLocaleDescriptions(): Promise< if (loadedDescriptions == undefined) { loadedDescriptions = ( await import( - "file://" + - path.resolve(getConfig().localeDataPath, "locales.js") - ) + "file://" + + path.resolve(getConfig().localeDataPath, "locales.js") + ) ).default; indexedDescriptions = Object.fromEntries( - loadedDescriptions.map((v) => [v.code, v]), + loadedDescriptions.map((v) => [v.code, v]) ); activeLocaleDescriptions = undefined; } @@ -123,11 +123,11 @@ export class Locale { private async readFile(parts: Array): Promise; private async readFile( parts: Array, - parser: (value: string) => T, + parser: (value: string) => T ): Promise; private async readFile( pathParts: Array, - parser?: (value: string) => T, + parser?: (value: string) => T ) { // Some may complain about this const filePath = this.path(pathParts); @@ -141,7 +141,7 @@ export class Locale { } } catch (e) { const err = new Error( - `[${this.code}] Could not load file ${filePath}: ${e}`, + `[${this.code}] Could not load file ${filePath}: ${e}` ); err.cause = e; throw err; @@ -155,7 +155,7 @@ export class Locale { return await import(`file://${filePath}`); } catch (e) { const err = new Error( - `[${this.code}] Could not import file ${filePath}: ${e}`, + `[${this.code}] Could not import file ${filePath}: ${e}` ); err.cause = e; throw err; @@ -168,7 +168,7 @@ export class Locale { private _pronounsByAlias: Record = {}; private _examples: Array = []; // TODO: Type these properly - // private _morphemes: Array; + private _morphemes: Array = []; public async load() { const tsvParse = (data: string) => @@ -182,17 +182,39 @@ export class Locale { this._config = await this.readFile(["config.suml"], suml.parse); this._translations = await this.readFile( ["translations.suml"], - suml.parse, + suml.parse + ); + + // NOTE(tecc): This is a hack. A bad hack. + // But it's far less painful than converting every morphemes.js + // file to some other format. + this._morphemes = await this.readFile( + ["pronouns/morphemes.js"], + (value) => { + let all = []; + let capture = null; + for (const char of value) { + if (capture == null) { + if (char === '"' || char === "'") { + capture = ""; + } + } else { + if (char === '"' || char === "'") { + all.push(capture); + capture = null; + continue; + } + capture += char; + } + } + return all; + } ); - // NOTE(tecc): This currently doesn't work because the morphemes.js files (as well as many others) rely on ESM syntax. - // This is both inconsistent with the locales.js and expectedTranslations.js files, - // and causes Node to throw an error. - // this._morphemes = (await this.importFile("pronouns/morphemes.js")).default; this._pronouns = []; this._pronounsByAlias = {}; const pronouns = await this.readFile( ["pronouns/pronouns.tsv"], - tsvParse, + tsvParse ); for (const pronoun of pronouns) { const partial: Partial = { forms: {} }; @@ -223,6 +245,11 @@ export class Locale { partial.source = value; break; default: + if (!this._morphemes.includes(key)) { + throw new Error( + `[${this.code}] Unknown key ${key}` + ); + } const [written, pronounced] = value.split("|"); partial.forms![key] = { written, pronounced }; break; @@ -237,7 +264,7 @@ export class Locale { } const examples = await this.readFile( ["pronouns/examples.tsv"], - tsvParse, + tsvParse ); this._examples = []; for (const example of examples) { @@ -282,7 +309,7 @@ export class Locale { export function examplesFor( pronoun: Pronoun, - examples: Array, + examples: Array ): Array { const finished = []; @@ -320,7 +347,7 @@ export function getLocale(localeCode: string): Locale | null { if (locale == null) { locale = new Locale( localeCode, - path.resolve(getConfig().localeDataPath, localeCode), + path.resolve(getConfig().localeDataPath, localeCode) ); loadedLocales[localeCode] = locale; } @@ -337,8 +364,8 @@ export async function loadAllLocales() { locale .load() .then(() => - log.debug(`Successfully loaded locale ${locale.code}`), - ), + log.debug(`Successfully loaded locale ${locale.code}`) + ) ); } }