mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-23 20:54:48 -04:00
change(rewrite): Loading morphemes.js with a hack
This commit is contained in:
parent
cb72dd4289
commit
5a899ac0a5
@ -50,12 +50,12 @@ export async function loadLocaleDescriptions(): Promise<
|
|||||||
if (loadedDescriptions == undefined) {
|
if (loadedDescriptions == undefined) {
|
||||||
loadedDescriptions = (
|
loadedDescriptions = (
|
||||||
await import(
|
await import(
|
||||||
"file://" +
|
"file://" +
|
||||||
path.resolve(getConfig().localeDataPath, "locales.js")
|
path.resolve(getConfig().localeDataPath, "locales.js")
|
||||||
)
|
)
|
||||||
).default;
|
).default;
|
||||||
indexedDescriptions = Object.fromEntries(
|
indexedDescriptions = Object.fromEntries(
|
||||||
loadedDescriptions.map((v) => [v.code, v]),
|
loadedDescriptions.map((v) => [v.code, v])
|
||||||
);
|
);
|
||||||
activeLocaleDescriptions = undefined;
|
activeLocaleDescriptions = undefined;
|
||||||
}
|
}
|
||||||
@ -123,11 +123,11 @@ export class Locale {
|
|||||||
private async readFile(parts: Array<string>): Promise<string>;
|
private async readFile(parts: Array<string>): Promise<string>;
|
||||||
private async readFile<T>(
|
private async readFile<T>(
|
||||||
parts: Array<string>,
|
parts: Array<string>,
|
||||||
parser: (value: string) => T,
|
parser: (value: string) => T
|
||||||
): Promise<T>;
|
): Promise<T>;
|
||||||
private async readFile<T>(
|
private async readFile<T>(
|
||||||
pathParts: Array<string>,
|
pathParts: Array<string>,
|
||||||
parser?: (value: string) => T,
|
parser?: (value: string) => T
|
||||||
) {
|
) {
|
||||||
// Some may complain about this
|
// Some may complain about this
|
||||||
const filePath = this.path(pathParts);
|
const filePath = this.path(pathParts);
|
||||||
@ -141,7 +141,7 @@ export class Locale {
|
|||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const err = new Error(
|
const err = new Error(
|
||||||
`[${this.code}] Could not load file ${filePath}: ${e}`,
|
`[${this.code}] Could not load file ${filePath}: ${e}`
|
||||||
);
|
);
|
||||||
err.cause = e;
|
err.cause = e;
|
||||||
throw err;
|
throw err;
|
||||||
@ -155,7 +155,7 @@ export class Locale {
|
|||||||
return await import(`file://${filePath}`);
|
return await import(`file://${filePath}`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const err = new Error(
|
const err = new Error(
|
||||||
`[${this.code}] Could not import file ${filePath}: ${e}`,
|
`[${this.code}] Could not import file ${filePath}: ${e}`
|
||||||
);
|
);
|
||||||
err.cause = e;
|
err.cause = e;
|
||||||
throw err;
|
throw err;
|
||||||
@ -168,7 +168,7 @@ export class Locale {
|
|||||||
private _pronounsByAlias: Record<string, number> = {};
|
private _pronounsByAlias: Record<string, number> = {};
|
||||||
|
|
||||||
private _examples: Array<PronounExample> = []; // TODO: Type these properly
|
private _examples: Array<PronounExample> = []; // TODO: Type these properly
|
||||||
// private _morphemes: Array<string>;
|
private _morphemes: Array<string> = [];
|
||||||
|
|
||||||
public async load() {
|
public async load() {
|
||||||
const tsvParse = (data: string) =>
|
const tsvParse = (data: string) =>
|
||||||
@ -182,17 +182,39 @@ export class Locale {
|
|||||||
this._config = await this.readFile(["config.suml"], suml.parse);
|
this._config = await this.readFile(["config.suml"], suml.parse);
|
||||||
this._translations = await this.readFile(
|
this._translations = await this.readFile(
|
||||||
["translations.suml"],
|
["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._pronouns = [];
|
||||||
this._pronounsByAlias = {};
|
this._pronounsByAlias = {};
|
||||||
const pronouns = await this.readFile(
|
const pronouns = await this.readFile(
|
||||||
["pronouns/pronouns.tsv"],
|
["pronouns/pronouns.tsv"],
|
||||||
tsvParse,
|
tsvParse
|
||||||
);
|
);
|
||||||
for (const pronoun of pronouns) {
|
for (const pronoun of pronouns) {
|
||||||
const partial: Partial<Pronoun> = { forms: {} };
|
const partial: Partial<Pronoun> = { forms: {} };
|
||||||
@ -223,6 +245,11 @@ export class Locale {
|
|||||||
partial.source = value;
|
partial.source = value;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
if (!this._morphemes.includes(key)) {
|
||||||
|
throw new Error(
|
||||||
|
`[${this.code}] Unknown key ${key}`
|
||||||
|
);
|
||||||
|
}
|
||||||
const [written, pronounced] = value.split("|");
|
const [written, pronounced] = value.split("|");
|
||||||
partial.forms![key] = { written, pronounced };
|
partial.forms![key] = { written, pronounced };
|
||||||
break;
|
break;
|
||||||
@ -237,7 +264,7 @@ export class Locale {
|
|||||||
}
|
}
|
||||||
const examples = await this.readFile(
|
const examples = await this.readFile(
|
||||||
["pronouns/examples.tsv"],
|
["pronouns/examples.tsv"],
|
||||||
tsvParse,
|
tsvParse
|
||||||
);
|
);
|
||||||
this._examples = [];
|
this._examples = [];
|
||||||
for (const example of examples) {
|
for (const example of examples) {
|
||||||
@ -282,7 +309,7 @@ export class Locale {
|
|||||||
|
|
||||||
export function examplesFor(
|
export function examplesFor(
|
||||||
pronoun: Pronoun,
|
pronoun: Pronoun,
|
||||||
examples: Array<PronounExample>,
|
examples: Array<PronounExample>
|
||||||
): Array<string> {
|
): Array<string> {
|
||||||
const finished = [];
|
const finished = [];
|
||||||
|
|
||||||
@ -320,7 +347,7 @@ export function getLocale(localeCode: string): Locale | null {
|
|||||||
if (locale == null) {
|
if (locale == null) {
|
||||||
locale = new Locale(
|
locale = new Locale(
|
||||||
localeCode,
|
localeCode,
|
||||||
path.resolve(getConfig().localeDataPath, localeCode),
|
path.resolve(getConfig().localeDataPath, localeCode)
|
||||||
);
|
);
|
||||||
loadedLocales[localeCode] = locale;
|
loadedLocales[localeCode] = locale;
|
||||||
}
|
}
|
||||||
@ -337,8 +364,8 @@ export async function loadAllLocales() {
|
|||||||
locale
|
locale
|
||||||
.load()
|
.load()
|
||||||
.then(() =>
|
.then(() =>
|
||||||
log.debug(`Successfully loaded locale ${locale.code}`),
|
log.debug(`Successfully loaded locale ${locale.code}`)
|
||||||
),
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user