UNPKG

express-intlayer

Version:

Manage internationalization i18n in a simple way for express application.

113 lines 4.32 kB
"use strict"; 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, { getDictionary: () => getDictionary, getIntlayer: () => getIntlayer, 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)(); 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); const getIntlayer2 = (key, localeArg = localeDetected, ...props) => (0, import_core.getIntlayer)(key, localeArg, ...props); const getDictionary2 = (key, localeArg = localeDetected, ...props) => (0, import_core.getDictionary)(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); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getDictionary, getIntlayer, intlayer, t, translateFunction }); //# sourceMappingURL=index.cjs.map