express-intlayer
Version:
Manage internationalization i18n in a simple way for express application.
93 lines (91 loc) • 4.69 kB
JavaScript
let __intlayer_chokidar = require("@intlayer/chokidar");
let __intlayer_config = require("@intlayer/config");
let __intlayer_core = require("@intlayer/core");
let cls_hooked = require("cls-hooked");
//#region src/index.ts
const configuration = (0, __intlayer_config.getConfiguration)();
const { internationalization } = configuration;
/**
* Retrieves the locale from storage (cookies, localStorage, sessionStorage).
*/
const getStorageLocale = (req) => (0, __intlayer_core.getLocaleFromStorage)({
getCookie: (name) => req.cookies?.[name],
getHeader: (name) => req.headers?.[name]
});
const appNamespace = (0, cls_hooked.createNamespace)("app");
(0, __intlayer_chokidar.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 (0, __intlayer_core.getTranslation)(content, defaultLocale);
return (0, __intlayer_core.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 = (0, __intlayer_core.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$1 = (key, localeArg = localeDetected, ...props) => (0, __intlayer_core.getIntlayer)(key, localeArg, ...props);
const getDictionary$1 = (key, localeArg = localeDetected, ...props) => (0, __intlayer_core.getDictionary)(key, localeArg, ...props);
res.locals.t = t$1;
res.locals.getIntlayer = getIntlayer$1;
res.locals.getDictionary = getDictionary$1;
appNamespace.run(() => {
appNamespace.set("t", t$1);
appNamespace.set("getIntlayer", getIntlayer$1);
appNamespace.set("getDictionary", getDictionary$1);
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) {
if (process.env.NODE_ENV === "development") console.error(error.message);
return (0, __intlayer_core.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) {
if (process.env.NODE_ENV === "development") console.error(error.message);
return (0, __intlayer_core.getIntlayer)(...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) {
if (process.env.NODE_ENV === "development") console.error(error.message);
return (0, __intlayer_core.getDictionary)(...args);
}
};
//#endregion
exports.getDictionary = getDictionary;
exports.getIntlayer = getIntlayer;
exports.intlayer = intlayer;
exports.t = t;
exports.translateFunction = translateFunction;
//# sourceMappingURL=index.cjs.map