change(rewrite): Loading morphemes.js with a hack

This commit is contained in:
tecc 2023-09-19 21:13:56 +02:00
parent cb72dd4289
commit 5a899ac0a5
No known key found for this signature in database
GPG Key ID: 622EEC5BAE5EBD3A

View File

@ -55,7 +55,7 @@ export async function loadLocaleDescriptions(): Promise<
)
).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<string>): Promise<string>;
private async readFile<T>(
parts: Array<string>,
parser: (value: string) => T,
parser: (value: string) => T
): Promise<T>;
private async readFile<T>(
pathParts: Array<string>,
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<string, number> = {};
private _examples: Array<PronounExample> = []; // TODO: Type these properly
// private _morphemes: Array<string>;
private _morphemes: Array<string> = [];
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<Pronoun> = { 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<PronounExample>,
examples: Array<PronounExample>
): Array<string> {
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}`)
)
);
}
}