express-intlayer
Version:
Manage internationalization i18n in a simple way for express application.
89 lines • 3.24 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var index_exports = {};
__export(index_exports, {
intlayer: () => intlayer,
t: () => t,
translateFunction: () => translateFunction
});
module.exports = __toCommonJS(index_exports);
var import_chokidar = require("@intlayer/chokidar");
var import_config = require("@intlayer/config");
var import_core = require("@intlayer/core");
var import_cls_hooked = require("cls-hooked");
const { middleware, internationalization } = (0, import_config.getConfiguration)({
verbose: true
});
const { headerName, cookieName } = middleware;
const appNamespace = (0, import_cls_hooked.createNamespace)("app");
(0, import_chokidar.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 (0, import_core.getTranslation)(content, defaultLocale);
}
}
return (0, import_core.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 = (0, import_core.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);
res.locals.t = t2;
appNamespace.run(() => {
appNamespace.set("t", t2);
next();
});
};
const t = (content, locale) => appNamespace.get("t")(content, locale);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
intlayer,
t,
translateFunction
});
//# sourceMappingURL=index.cjs.map
;