express-intlayer
Version:
Manage internationalization i18n in a simple way for express application.
90 lines • 3.17 kB
JavaScript
import { createModuleAugmentation } from "@intlayer/chokidar";
import { getConfiguration } from "@intlayer/config";
import {
getDictionary as getDictionaryFunction,
getIntlayer as getIntlayerFunction,
getTranslation,
localeDetector
} from "@intlayer/core";
import { createNamespace } from "cls-hooked";
const { middleware, internationalization } = getConfiguration();
const { headerName, cookieName } = middleware;
const appNamespace = createNamespace("app");
createModuleAugmentation();
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);
};
const intlayer = () => (req, res, next) => {
const localeCookie = req.cookies?.[cookieName];
const localeHeader = req.headers?.[headerName];
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_header = localeHeader;
res.locals.locale_cookie = localeCookie;
res.locals.locale_detected = localeDetected;
res.locals.locale = localeCookie ?? localeHeader ?? localeDetected;
res.locals.defaultLocale = internationalization.defaultLocale;
const t2 = translateFunction(req, res, next);
const getIntlayer2 = (key, localeArg = localeDetected, ...props) => getIntlayerFunction(key, localeArg, ...props);
const getDictionary2 = (key, localeArg = localeDetected, ...props) => getDictionaryFunction(key, localeArg, ...props);
res.locals.t = t2;
res.locals.getIntlayer = getIntlayer2;
res.locals.getDictionary = getDictionary2;
appNamespace.run(() => {
appNamespace.set("t", t2);
appNamespace.set("getIntlayer", getIntlayer2);
appNamespace.set("getDictionary", getDictionary2);
next();
});
};
const t = (content, locale) => appNamespace.get("t")(content, locale);
const getIntlayer = (...args) => {
if (typeof appNamespace === "undefined") {
throw new Error(
"Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function"
);
}
return appNamespace.get("getIntlayer")(...args);
};
const getDictionary = (...args) => {
if (typeof appNamespace === "undefined") {
throw new Error(
"Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function"
);
}
return appNamespace.get("getDictionary")(...args);
};
export {
getDictionary,
getIntlayer,
intlayer,
t,
translateFunction
};
//# sourceMappingURL=index.mjs.map