express-intlayer 5.5.11 → 5.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -33,7 +33,7 @@
33
33
  <a href="https://www.facebook.com/intlayer" target="blank"><img align="center"
34
34
  src="https://img.shields.io/badge/facebook-4267B2.svg?style=for-the-badge&logo=facebook&logoColor=white"
35
35
  alt="Intlayer Facebook" height="30"/></a>
36
- <a href="https://www.instagram.com/intlayer_org/" target="blank"><img align="center"
36
+ <a href="https://www.instagram.com/intlayer/" target="blank"><img align="center"
37
37
  src="https://img.shields.io/badge/instagram-%23E4405F.svg?style=for-the-badge&logo=Instagram&logoColor=white"
38
38
  alt="Intlayer Instagram" height="30"/></a>
39
39
  <a href="https://x.com/Intlayer183096" target="blank"><img align="center"
@@ -86,8 +86,22 @@ const intlayer = () => (req, res, next) => {
86
86
  });
87
87
  };
88
88
  const t = (content, locale) => appNamespace.get("t")(content, locale);
89
- const getIntlayer = (...args) => appNamespace.get("getIntlayer")(...args);
90
- const getDictionary = (...args) => appNamespace.get("getDictionary")(...args);
89
+ const getIntlayer = (...args) => {
90
+ if (typeof appNamespace === "undefined") {
91
+ throw new Error(
92
+ "Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function"
93
+ );
94
+ }
95
+ return appNamespace.get("getIntlayer")(...args);
96
+ };
97
+ const getDictionary = (...args) => {
98
+ if (typeof appNamespace === "undefined") {
99
+ throw new Error(
100
+ "Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function"
101
+ );
102
+ }
103
+ return appNamespace.get("getDictionary")(...args);
104
+ };
91
105
  // Annotate the CommonJS export names for ESM import in node:
92
106
  0 && (module.exports = {
93
107
  getDictionary,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { createModuleAugmentation } from '@intlayer/chokidar';\nimport { getConfiguration, type Locales } from '@intlayer/config';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n localeDetector,\n type LanguageContent,\n} from '@intlayer/core';\nimport { createNamespace } from 'cls-hooked';\nimport type { NextFunction, Request, RequestHandler, Response } from 'express';\n\nconst { middleware, internationalization } = getConfiguration();\nconst { headerName, cookieName } = middleware;\n\nconst appNamespace = createNamespace('app');\n\ncreateModuleAugmentation();\n\nexport const translateFunction =\n (_req: Request, res: Response, _next?: NextFunction) =>\n <T extends string>(\n content: LanguageContent<T> | string,\n locale?: Locales\n ): T => {\n const { locale: currentLocale, defaultLocale } = res.locals as {\n locale: Locales;\n defaultLocale: Locales;\n };\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[targetLocale as unknown as keyof LanguageContent<T>] ===\n 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof LanguageContent<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Detect locale used by the user and load it into res locale storage\n *\n * @returns\n */\nexport const intlayer = (): RequestHandler => (req, res, next) => {\n // Detect if locale is set by intlayer frontend lib in the cookies\n const localeCookie = req.cookies?.[cookieName];\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeHeader = req.headers?.[headerName];\n // Interpret browser locale\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // // Check if req.headers exists and is an object\n if (req && typeof req.headers === 'object') {\n // Copy all headers from the request to negotiatorHeaders\n for (const key in req.headers) {\n if (typeof req.headers[key] === 'string') {\n negotiatorHeaders[key] = req.headers[key];\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n res.locals.locale_header = localeHeader;\n res.locals.locale_cookie = localeCookie;\n res.locals.locale_detected = localeDetected;\n res.locals.locale = localeCookie ?? localeHeader ?? localeDetected;\n res.locals.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(req, res, next);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n res.locals.t = t;\n res.locals.getIntlayer = getIntlayer;\n res.locals.getDictionary = getDictionary;\n\n appNamespace.run(() => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n next();\n });\n};\n\nexport const t = <Content = string>(\n content: LanguageContent<Content>,\n locale?: Locales\n): Content => appNamespace.get('t')(content, locale);\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) =>\n appNamespace.get('getIntlayer')(...args);\nexport const getDictionary: typeof getDictionaryFunction = (...args) =>\n appNamespace.get('getDictionary')(...args);\n\nexport { LanguageContent };\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAyC;AACzC,oBAA+C;AAC/C,kBAMO;AACP,wBAAgC;AAGhC,MAAM,EAAE,YAAY,qBAAqB,QAAI,gCAAiB;AAC9D,MAAM,EAAE,YAAY,WAAW,IAAI;AAEnC,MAAM,mBAAe,mCAAgB,KAAK;AAAA,IAE1C,0CAAyB;AAElB,MAAM,oBACX,CAAC,MAAe,KAAe,UAC/B,CACE,SACA,WACM;AACN,QAAM,EAAE,QAAQ,eAAe,cAAc,IAAI,IAAI;AAKrD,QAAM,eAAe,UAAU;AAE/B,MAAI,OAAO,YAAY,aAAa;AAClC,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,YAAY,UAAU;AAC/B,WAAO;AAAA,EACT;AAEA,MACE,OAAO,UAAU,YAAm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AACH;AAEO,MAAM,IAAI,CACf,SACA,WACY,aAAa,IAAI,GAAG,EAAE,SAAS,MAAM;AAE5C,MAAM,cAA0C,IAAI,SACzD,aAAa,IAAI,aAAa,EAAE,GAAG,IAAI;AAClC,MAAM,gBAA8C,IAAI,SAC7D,aAAa,IAAI,eAAe,EAAE,GAAG,IAAI;","names":["t","getIntlayer","getIntlayerFunction","getDictionary","getDictionaryFunction"]}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { createModuleAugmentation } from '@intlayer/chokidar';\nimport { getConfiguration, type Locales } from '@intlayer/config';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n localeDetector,\n type LanguageContent,\n} from '@intlayer/core';\nimport { createNamespace } from 'cls-hooked';\nimport type { NextFunction, Request, RequestHandler, Response } from 'express';\n\nconst { middleware, internationalization } = getConfiguration();\nconst { headerName, cookieName } = middleware;\n\nconst appNamespace = createNamespace('app');\n\ncreateModuleAugmentation();\n\nexport const translateFunction =\n (_req: Request, res: Response, _next?: NextFunction) =>\n <T extends string>(\n content: LanguageContent<T> | string,\n locale?: Locales\n ): T => {\n const { locale: currentLocale, defaultLocale } = res.locals as {\n locale: Locales;\n defaultLocale: Locales;\n };\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[targetLocale as unknown as keyof LanguageContent<T>] ===\n 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof LanguageContent<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Detect locale used by the user and load it into res locale storage\n *\n * @returns\n */\nexport const intlayer = (): RequestHandler => (req, res, next) => {\n // Detect if locale is set by intlayer frontend lib in the cookies\n const localeCookie = req.cookies?.[cookieName];\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeHeader = req.headers?.[headerName];\n // Interpret browser locale\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // // Check if req.headers exists and is an object\n if (req && typeof req.headers === 'object') {\n // Copy all headers from the request to negotiatorHeaders\n for (const key in req.headers) {\n if (typeof req.headers[key] === 'string') {\n negotiatorHeaders[key] = req.headers[key];\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n res.locals.locale_header = localeHeader;\n res.locals.locale_cookie = localeCookie;\n res.locals.locale_detected = localeDetected;\n res.locals.locale = localeCookie ?? localeHeader ?? localeDetected;\n res.locals.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(req, res, next);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n res.locals.t = t;\n res.locals.getIntlayer = getIntlayer;\n res.locals.getDictionary = getDictionary;\n\n appNamespace.run(() => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n next();\n });\n};\n\nexport const t = <Content = string>(\n content: LanguageContent<Content>,\n locale?: Locales\n): Content => appNamespace.get('t')(content, locale);\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) => {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function'\n );\n }\n\n return appNamespace.get('getIntlayer')(...args);\n};\n\nexport const getDictionary: typeof getDictionaryFunction = (...args) => {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function'\n );\n }\n return appNamespace.get('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AAI,OAAO,gBAAgB;AAC3B,MAAI,OAAO,kBAAkB;AAC7B,MAAI,OAAO,SAAS,gBAAgB,gBAAgB;AACpD,MAAI,OAAO,gBAAgB,qBAAqB;AAEhD,QAAMA,KAAI,kBAAkB,KAAK,KAAK,IAAI;AAE1C,QAAMC,eAA0C,CAC9C,KACA,YAAY,mBACT,cACA,YAAAC,aAAoB,KAAK,WAAW,GAAG,KAAK;AAEjD,QAAMC,iBAA8C,CAClD,KACA,YAAY,mBACT,cACA,YAAAC,eAAsB,KAAK,WAAW,GAAG,KAAK;AAEnD,MAAI,OAAO,IAAIJ;AACf,MAAI,OAAO,cAAcC;AACzB,MAAI,OAAO,gBAAgBE;AAE3B,eAAa,IAAI,MAAM;AACrB,iBAAa,IAAI,KAAKH,EAAC;AACvB,iBAAa,IAAI,eAAeC,YAAW;AAC3C,iBAAa,IAAI,iBAAiBE,cAAa;AAE/C,SAAK;AAAA,EACP,CAAC;AACH;AAEO,MAAM,IAAI,CACf,SACA,WACY,aAAa,IAAI,GAAG,EAAE,SAAS,MAAM;AAE5C,MAAM,cAA0C,IAAI,SAAS;AAClE,MAAI,OAAO,iBAAiB,aAAa;AACvC,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,SAAO,aAAa,IAAI,aAAa,EAAE,GAAG,IAAI;AAChD;AAEO,MAAM,gBAA8C,IAAI,SAAS;AACtE,MAAI,OAAO,iBAAiB,aAAa;AACvC,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO,aAAa,IAAI,eAAe,EAAE,GAAG,IAAI;AAClD;","names":["t","getIntlayer","getIntlayerFunction","getDictionary","getDictionaryFunction"]}
@@ -64,8 +64,22 @@ const intlayer = () => (req, res, next) => {
64
64
  });
65
65
  };
66
66
  const t = (content, locale) => appNamespace.get("t")(content, locale);
67
- const getIntlayer = (...args) => appNamespace.get("getIntlayer")(...args);
68
- const getDictionary = (...args) => appNamespace.get("getDictionary")(...args);
67
+ const getIntlayer = (...args) => {
68
+ if (typeof appNamespace === "undefined") {
69
+ throw new Error(
70
+ "Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function"
71
+ );
72
+ }
73
+ return appNamespace.get("getIntlayer")(...args);
74
+ };
75
+ const getDictionary = (...args) => {
76
+ if (typeof appNamespace === "undefined") {
77
+ throw new Error(
78
+ "Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function"
79
+ );
80
+ }
81
+ return appNamespace.get("getDictionary")(...args);
82
+ };
69
83
  export {
70
84
  getDictionary,
71
85
  getIntlayer,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { createModuleAugmentation } from '@intlayer/chokidar';\nimport { getConfiguration, type Locales } from '@intlayer/config';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n localeDetector,\n type LanguageContent,\n} from '@intlayer/core';\nimport { createNamespace } from 'cls-hooked';\nimport type { NextFunction, Request, RequestHandler, Response } from 'express';\n\nconst { middleware, internationalization } = getConfiguration();\nconst { headerName, cookieName } = middleware;\n\nconst appNamespace = createNamespace('app');\n\ncreateModuleAugmentation();\n\nexport const translateFunction =\n (_req: Request, res: Response, _next?: NextFunction) =>\n <T extends string>(\n content: LanguageContent<T> | string,\n locale?: Locales\n ): T => {\n const { locale: currentLocale, defaultLocale } = res.locals as {\n locale: Locales;\n defaultLocale: Locales;\n };\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[targetLocale as unknown as keyof LanguageContent<T>] ===\n 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof LanguageContent<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Detect locale used by the user and load it into res locale storage\n *\n * @returns\n */\nexport const intlayer = (): RequestHandler => (req, res, next) => {\n // Detect if locale is set by intlayer frontend lib in the cookies\n const localeCookie = req.cookies?.[cookieName];\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeHeader = req.headers?.[headerName];\n // Interpret browser locale\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // // Check if req.headers exists and is an object\n if (req && typeof req.headers === 'object') {\n // Copy all headers from the request to negotiatorHeaders\n for (const key in req.headers) {\n if (typeof req.headers[key] === 'string') {\n negotiatorHeaders[key] = req.headers[key];\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n res.locals.locale_header = localeHeader;\n res.locals.locale_cookie = localeCookie;\n res.locals.locale_detected = localeDetected;\n res.locals.locale = localeCookie ?? localeHeader ?? localeDetected;\n res.locals.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(req, res, next);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n res.locals.t = t;\n res.locals.getIntlayer = getIntlayer;\n res.locals.getDictionary = getDictionary;\n\n appNamespace.run(() => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n next();\n });\n};\n\nexport const t = <Content = string>(\n content: LanguageContent<Content>,\n locale?: Locales\n): Content => appNamespace.get('t')(content, locale);\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) =>\n appNamespace.get('getIntlayer')(...args);\nexport const getDictionary: typeof getDictionaryFunction = (...args) =>\n appNamespace.get('getDictionary')(...args);\n\nexport { LanguageContent };\n"],"mappings":"AAAA,SAAS,gCAAgC;AACzC,SAAS,wB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AAgB;AACpD,MAAI,OAAO,gBAAgB,qBAAqB;AAEhD,QAAMA,KAAI,kBAAkB,KAAK,KAAK,IAAI;AAE1C,QAAMC,eAA0C,CAC9C,KACA,YAAY,mBACT,UACA,oBAAoB,KAAK,WAAW,GAAG,KAAK;AAEjD,QAAMC,iBAA8C,CAClD,KACA,YAAY,mBACT,UACA,sBAAsB,KAAK,WAAW,GAAG,KAAK;AAEnD,MAAI,OAAO,IAAIF;AACf,MAAI,OAAO,cAAcC;AACzB,MAAI,OAAO,gBAAgBC;AAE3B,eAAa,IAAI,MAAM;AACrB,iBAAa,IAAI,KAAKF,EAAC;AACvB,iBAAa,IAAI,eAAeC,YAAW;AAC3C,iBAAa,IAAI,iBAAiBC,cAAa;AAE/C,SAAK;AAAA,EACP,CAAC;AACH;AAEO,MAAM,IAAI,CACf,SACA,WACY,aAAa,IAAI,GAAG,EAAE,SAAS,MAAM;AAE5C,MAAM,cAA0C,IAAI,SACzD,aAAa,IAAI,aAAa,EAAE,GAAG,IAAI;AAClC,MAAM,gBAA8C,IAAI,SAC7D,aAAa,IAAI,eAAe,EAAE,GAAG,IAAI;","names":["t","getIntlayer","getDictionary"]}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { createModuleAugmentation } from '@intlayer/chokidar';\nimport { getConfiguration, type Locales } from '@intlayer/config';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n localeDetector,\n type LanguageContent,\n} from '@intlayer/core';\nimport { createNamespace } from 'cls-hooked';\nimport type { NextFunction, Request, RequestHandler, Response } from 'express';\n\nconst { middleware, internationalization } = getConfiguration();\nconst { headerName, cookieName } = middleware;\n\nconst appNamespace = createNamespace('app');\n\ncreateModuleAugmentation();\n\nexport const translateFunction =\n (_req: Request, res: Response, _next?: NextFunction) =>\n <T extends string>(\n content: LanguageContent<T> | string,\n locale?: Locales\n ): T => {\n const { locale: currentLocale, defaultLocale } = res.locals as {\n locale: Locales;\n defaultLocale: Locales;\n };\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[targetLocale as unknown as keyof LanguageContent<T>] ===\n 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof LanguageContent<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Detect locale used by the user and load it into res locale storage\n *\n * @returns\n */\nexport const intlayer = (): RequestHandler => (req, res, next) => {\n // Detect if locale is set by intlayer frontend lib in the cookies\n const localeCookie = req.cookies?.[cookieName];\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeHeader = req.headers?.[headerName];\n // Interpret browser locale\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // // Check if req.headers exists and is an object\n if (req && typeof req.headers === 'object') {\n // Copy all headers from the request to negotiatorHeaders\n for (const key in req.headers) {\n if (typeof req.headers[key] === 'string') {\n negotiatorHeaders[key] = req.headers[key];\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n res.locals.locale_header = localeHeader;\n res.locals.locale_cookie = localeCookie;\n res.locals.locale_detected = localeDetected;\n res.locals.locale = localeCookie ?? localeHeader ?? localeDetected;\n res.locals.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(req, res, next);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n res.locals.t = t;\n res.locals.getIntlayer = getIntlayer;\n res.locals.getDictionary = getDictionary;\n\n appNamespace.run(() => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n next();\n });\n};\n\nexport const t = <Content = string>(\n content: LanguageContent<Content>,\n locale?: Locales\n): Content => appNamespace.get('t')(content, locale);\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) => {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function'\n );\n }\n\n return appNamespace.get('getIntlayer')(...args);\n};\n\nexport const getDictionary: typeof getDictionaryFunction = (...args) => {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `app.use(intlayer());` middleware before using this function'\n );\n }\n return appNamespace.get('getDictionary')(...args);\n};\n\nexport { LanguageContent };\n"],"mappings":"AAAA,SAAS,gCAAgC;AACzC,SAAS,wBAAsC;AAC/C;AAAA,EACE,iBAAiB;AAAA,EACjB,eAAe;AAAA,EACf;AAAA,EACA;AAAA,OAEK;AACP,SAAS,uBAAuB;AAGhC,MAAM,EAAE,YAAY,qBAAqB,IAAI,iBAAiB;AAC9D,MAAM,EAAE,YAAY,WAAW,IAAI;AAEnC,MAAM,eAAe,gBAAgB,KAAK;AAE1C,yBAAyB;AAElB,MAAM,oBACX,CAAC,MAAe,KAAe,UAC/B,CACE,SACA,WACM;AACN,QAAM,EAAE,QAAQ,eAAe,cAAc,IAAI,IAAI;AAKrD,QAAM,eAAe,UAAU;AAE/B,MAAI,OAAO,YAAY,aAAa;AAClC,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,YAAY,UAAU;AAC/B,WAAO;AAAA,EACT;AAEA,MACE,OAAO,UAAU,YAAmD,MACpE,aACA;AACA,QACE,OAAO,UACL,aACF,MAAM,aACN;AACA,aAAO;AAAA,IACT,OAAO;AACL,aAAO,eAAe,SAAS,aAAa;AAAA,IAC9C;AAAA,EACF;AAEA,SAAO,eAAe,SAAS,YAAY;AAC7C;AAOK,MAAM,WAA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,aAAa,IAAI,aAAa,EAAE,GAAG,IAAI;AAChD;AAEO,MAAM,gBAA8C,IAAI,SAAS;AACtE,MAAI,OAAO,iBAAiB,aAAa;AACvC,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO,aAAa,IAAI,eAAe,EAAE,GAAG,IAAI;AAClD;","names":["t","getIntlayer","getDictionary"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,KAAK,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,EACL,aAAa,IAAI,qBAAqB,EACtC,WAAW,IAAI,mBAAmB,EAGlC,KAAK,eAAe,EACrB,MAAM,gBAAgB,CAAC;AAExB,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAS/E,eAAO,MAAM,iBAAiB,GAC3B,MAAM,OAAO,EAAE,KAAK,QAAQ,EAAE,QAAQ,YAAY,MAClD,CAAC,SAAS,MAAM,EACf,SAAS,eAAe,CAAC,CAAC,CAAC,GAAG,MAAM,EACpC,SAAS,OAAO,KACf,CAgCF,CAAC;AAEJ;;;;GAIG;AACH,eAAO,MAAM,QAAQ,QAAO,cAwD3B,CAAC;AAEF,eAAO,MAAM,CAAC,GAAI,OAAO,GAAG,MAAM,EAChC,SAAS,eAAe,CAAC,OAAO,CAAC,EACjC,SAAS,OAAO,KACf,OAAiD,CAAC;AAErD,eAAO,MAAM,WAAW,EAAE,OAAO,mBACS,CAAC;AAC3C,eAAO,MAAM,aAAa,EAAE,OAAO,qBACS,CAAC;AAE7C,OAAO,EAAE,eAAe,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,KAAK,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,EACL,aAAa,IAAI,qBAAqB,EACtC,WAAW,IAAI,mBAAmB,EAGlC,KAAK,eAAe,EACrB,MAAM,gBAAgB,CAAC;AAExB,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAS/E,eAAO,MAAM,iBAAiB,GAC3B,MAAM,OAAO,EAAE,KAAK,QAAQ,EAAE,QAAQ,YAAY,MAClD,CAAC,SAAS,MAAM,EACf,SAAS,eAAe,CAAC,CAAC,CAAC,GAAG,MAAM,EACpC,SAAS,OAAO,KACf,CAgCF,CAAC;AAEJ;;;;GAIG;AACH,eAAO,MAAM,QAAQ,QAAO,cAwD3B,CAAC;AAEF,eAAO,MAAM,CAAC,GAAI,OAAO,GAAG,MAAM,EAChC,SAAS,eAAe,CAAC,OAAO,CAAC,EACjC,SAAS,OAAO,KACf,OAAiD,CAAC;AAErD,eAAO,MAAM,WAAW,EAAE,OAAO,mBAQhC,CAAC;AAEF,eAAO,MAAM,aAAa,EAAE,OAAO,qBAOlC,CAAC;AAEF,OAAO,EAAE,eAAe,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-intlayer",
3
- "version": "5.5.11",
3
+ "version": "5.7.0",
4
4
  "private": false,
5
5
  "description": "Manage internationalization i18n in a simple way for express application.",
6
6
  "keywords": [
@@ -62,10 +62,10 @@
62
62
  ],
63
63
  "dependencies": {
64
64
  "cls-hooked": "^4.2.2",
65
- "@intlayer/config": "5.5.11",
66
- "@intlayer/chokidar": "5.5.11",
67
- "@intlayer/core": "5.5.11",
68
- "intlayer": "5.5.11"
65
+ "@intlayer/config": "5.7.0",
66
+ "@intlayer/core": "5.7.0",
67
+ "intlayer": "5.7.0",
68
+ "@intlayer/chokidar": "5.7.0"
69
69
  },
70
70
  "devDependencies": {
71
71
  "@changesets/changelog-github": "0.5.1",
@@ -83,16 +83,16 @@
83
83
  "tsup": "^8.5.0",
84
84
  "typescript": "^5.8.3",
85
85
  "vitest": "^3.2.2",
86
+ "@utils/ts-config": "1.0.4",
86
87
  "@utils/eslint-config": "1.0.4",
87
88
  "@utils/tsup-config": "1.0.4",
88
- "@utils/ts-config-types": "1.0.4",
89
- "@utils/ts-config": "1.0.4"
89
+ "@utils/ts-config-types": "1.0.4"
90
90
  },
91
91
  "peerDependencies": {
92
- "@intlayer/chokidar": "5.5.11",
93
- "@intlayer/config": "5.5.11",
94
- "@intlayer/core": "5.5.11",
95
- "intlayer": "5.5.11"
92
+ "@intlayer/chokidar": "5.7.0",
93
+ "@intlayer/config": "5.7.0",
94
+ "@intlayer/core": "5.7.0",
95
+ "intlayer": "5.7.0"
96
96
  },
97
97
  "engines": {
98
98
  "node": ">=14.18"