wuchale
Version:
Protobuf-like i18n from normal code
29 lines • 917 B
JavaScript
import { Runtime } from '../runtime.js';
import { AsyncLocalStorage } from 'node:async_hooks';
const catalogs = {};
const catalogCtx = new AsyncLocalStorage();
export function currentRT(key, loadID) {
return new Runtime(catalogCtx.getStore()[key][loadID]);
}
export async function loadLocales(key, loadIDs, load, locales) {
if (loadIDs == null) {
loadIDs = [key];
}
for (const locale of locales) {
if (!(locale in catalogs)) {
catalogs[locale] = {};
}
const loaded = catalogs[locale];
if (!(key in loaded)) {
loaded[key] = {};
}
for (const id of loadIDs) {
loaded[key][id] = await load(id, locale);
}
}
return (loadID) => currentRT(key, loadID);
}
export async function runWithLocale(locale, func) {
return await catalogCtx.run(catalogs[locale], func);
}
//# sourceMappingURL=server.js.map