mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-24 05:05:20 -04:00
104 lines
4.5 KiB
TypeScript
104 lines
4.5 KiB
TypeScript
import type { H3Event } from 'h3';
|
|
|
|
import { parsePronounGroups, parsePronouns } from '#shared/buildPronoun.ts';
|
|
import { buildCalendar } from '#shared/calendar/calendar.ts';
|
|
import type { Calendar } from '#shared/calendar/helpers.ts';
|
|
import { PronounLibrary } from '#shared/classes.ts';
|
|
import type { Pronoun, PronounGroup } from '#shared/classes.ts';
|
|
import type { NounsData } from '#shared/nouns.ts';
|
|
import { Translator } from '#shared/translator.ts';
|
|
import type { Config } from '~~/locale/config.ts';
|
|
import type { PronounData, PronounExamplesData } from '~~/locale/data.ts';
|
|
import type { LocaleCode } from '~~/locale/locales.ts';
|
|
import { loadSuml, loadTsv } from '~~/server/loader.ts';
|
|
import { getLocaleForUrl, getUrlForLocale } from '~~/server/src/domain.ts';
|
|
|
|
const setDefault = async <K, V>(map: Map<K, V>, key: K, supplier: () => Promise<V>): Promise<V> => {
|
|
const value = map.get(key);
|
|
if (value !== undefined) {
|
|
return value;
|
|
}
|
|
const computedValue = await supplier();
|
|
map.set(key, computedValue);
|
|
return computedValue;
|
|
};
|
|
|
|
export const getLocale = (event: H3Event): LocaleCode => {
|
|
const query = getQuery(event);
|
|
if (typeof query.locale === 'string') {
|
|
return query.locale as LocaleCode;
|
|
}
|
|
return getLocaleForUrl(getRequestURL(event)) ?? '_';
|
|
};
|
|
|
|
const configByLocale: Map<LocaleCode, Config> = new Map();
|
|
export const loadConfig = async (locale: LocaleCode): Promise<Config> => {
|
|
return setDefault(configByLocale, locale, async () => {
|
|
return loadSuml<Config>(`/locale/${locale}/config.suml`);
|
|
});
|
|
};
|
|
|
|
const baseTranslations = await loadSuml('locale/_base/translations.suml');
|
|
const translatorByLocale: Map<LocaleCode, Translator> = new Map();
|
|
export const loadTranslator = async (locale: LocaleCode): Promise<Translator> => {
|
|
return setDefault(translatorByLocale, locale, async () => {
|
|
const [config, translations] =
|
|
await Promise.all([loadConfig(locale), loadSuml(`locale/${locale}/translations.suml`)]);
|
|
return new Translator(translations, baseTranslations, config);
|
|
});
|
|
};
|
|
|
|
const pronounsByLocale: Map<LocaleCode, Record<LocaleCode, Pronoun>> = new Map();
|
|
export const loadPronouns = async (locale: LocaleCode): Promise<Record<string, Pronoun>> => {
|
|
return setDefault(pronounsByLocale, locale, async () => {
|
|
const [config, pronounsRaw] = await Promise.all([
|
|
loadConfig(locale),
|
|
loadTsv<PronounData<string>>(`locale/${locale}/pronouns/pronouns.tsv`),
|
|
]);
|
|
return parsePronouns(config, pronounsRaw);
|
|
});
|
|
};
|
|
|
|
const pronounGroupsByLocale: Map<LocaleCode, PronounGroup[]> = new Map();
|
|
export const loadPronounGroups = async (locale: LocaleCode): Promise<PronounGroup[]> => {
|
|
return setDefault(pronounGroupsByLocale, locale, async () => {
|
|
return parsePronounGroups(await loadTsv(`locale/${locale}/pronouns/pronounGroups.tsv`));
|
|
});
|
|
};
|
|
|
|
const pronounLibraryByLocale: Map<LocaleCode, PronounLibrary> = new Map();
|
|
export const loadPronounLibrary = async (locale: LocaleCode): Promise<PronounLibrary> => {
|
|
return setDefault(pronounLibraryByLocale, locale, async () => {
|
|
const [config, pronounGroups, pronouns] =
|
|
await Promise.all([loadConfig(locale), loadPronounGroups(locale), loadPronouns(locale)]);
|
|
return new PronounLibrary(config, pronounGroups, pronouns);
|
|
});
|
|
};
|
|
|
|
const pronounExamplesByLocale: Map<LocaleCode, PronounExamplesData[]> = new Map();
|
|
export const loadPronounExamples = async (locale: LocaleCode): Promise<PronounExamplesData[]> => {
|
|
return setDefault(pronounExamplesByLocale, locale, async () => {
|
|
return loadTsv<PronounExamplesData>(`locale/${locale}/pronouns/examples.tsv`);
|
|
});
|
|
};
|
|
|
|
const nounsDataByLocale: Map<LocaleCode, NounsData> = new Map();
|
|
export const loadNounsData = async (locale: LocaleCode): Promise<NounsData> => {
|
|
return setDefault(nounsDataByLocale, locale, async () => {
|
|
try {
|
|
return await loadSuml<NounsData>(`locale/${locale}/nouns/nounsData.suml`);
|
|
} catch (error) {
|
|
return {};
|
|
}
|
|
});
|
|
};
|
|
|
|
const calendarByLocale: Map<LocaleCode, Calendar> = new Map();
|
|
export const loadCalendar = async (locale: LocaleCode): Promise<Calendar> => {
|
|
return setDefault(calendarByLocale, locale, async () => {
|
|
const localeEventImports = (await import('#virtual/calendar/events.ts')).default;
|
|
const localEvents = await localeEventImports[locale]();
|
|
return buildCalendar(localEvents, getUrlForLocale(locale));
|
|
});
|
|
};
|