express-intlayer
Version:
Manage internationalization i18n in a simple way for express application.
89 lines (87 loc) • 4.33 kB
JavaScript
import { prepareIntlayer } from "@intlayer/chokidar";
import { getConfiguration } from "@intlayer/config";
import { getDictionary as getDictionary$1, getIntlayer as getIntlayer$1, getLocaleFromStorage, getTranslation, localeDetector } from "@intlayer/core";
import { createNamespace } from "cls-hooked";
//#region src/index.ts
const configuration = getConfiguration();
const { internationalization } = configuration;
/**
* Retrieves the locale from storage (cookies, localStorage, sessionStorage).
*/
const getStorageLocale = (req) => getLocaleFromStorage({
getCookie: (name) => req.cookies?.[name],
getHeader: (name) => req.headers?.[name]
});
const appNamespace = createNamespace("app");
prepareIntlayer(configuration);
const translateFunction = (_req, res, _next) => (content, locale) => {
const { locale: currentLocale, defaultLocale } = res.locals;
const targetLocale = locale ?? currentLocale;
if (typeof content === "undefined") return "";
if (typeof content === "string") return content;
if (typeof content?.[targetLocale] === "undefined") if (typeof content?.[defaultLocale] === "undefined") return content;
else return getTranslation(content, defaultLocale);
return getTranslation(content, targetLocale);
};
/**
* Detect locale used by the user and load it into res locale storage
*
* @returns
*/
const intlayer = () => async (req, res, next) => {
const localeFromStorage = getStorageLocale(req);
const negotiatorHeaders = {};
if (req && typeof req.headers === "object") {
for (const key in req.headers) if (typeof req.headers[key] === "string") negotiatorHeaders[key] = req.headers[key];
}
const localeDetected = localeDetector(negotiatorHeaders, internationalization.locales, internationalization.defaultLocale);
res.locals.locale_storage = localeFromStorage;
res.locals.locale_detected = localeDetected;
res.locals.locale = localeFromStorage ?? localeDetected;
res.locals.defaultLocale = internationalization.defaultLocale;
const t$1 = translateFunction(req, res, next);
const getIntlayer$2 = (key, localeArg = localeDetected, ...props) => getIntlayer$1(key, localeArg, ...props);
const getDictionary$2 = (key, localeArg = localeDetected, ...props) => getDictionary$1(key, localeArg, ...props);
res.locals.t = t$1;
res.locals.getIntlayer = getIntlayer$2;
res.locals.getDictionary = getDictionary$2;
appNamespace.run(() => {
appNamespace.set("t", t$1);
appNamespace.set("getIntlayer", getIntlayer$2);
appNamespace.set("getDictionary", getDictionary$2);
next();
});
};
const t = (content, locale) => {
try {
if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function.");
if (typeof appNamespace.get("t") !== "function") throw new Error("Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the res.locals.t syntax instead.");
return appNamespace.get("t")(content, locale);
} catch (error) {
console.error(error.message);
return getTranslation(content, locale ?? internationalization.defaultLocale);
}
};
const getIntlayer = (...args) => {
try {
if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function.");
if (typeof appNamespace.get("getIntlayer") !== "function") throw new Error("Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the res.locals.t syntax instead.");
return appNamespace.get("getIntlayer")(...args);
} catch (error) {
console.error(error.message);
return getIntlayer$1(...args);
}
};
const getDictionary = (...args) => {
try {
if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function.");
if (typeof appNamespace.get("getDictionary") !== "function") throw new Error("Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the res.locals.t syntax instead.");
return appNamespace.get("getDictionary")(...args);
} catch (error) {
console.error(error.message);
return getDictionary$1(...args);
}
};
//#endregion
export { getDictionary, getIntlayer, intlayer, t, translateFunction };
//# sourceMappingURL=index.mjs.map