adonis-intlayer 9.5.11 → 9.6.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.
@@ -1,44 +1,44 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_middleware = require('./middleware.cjs');
3
+ let node_async_hooks = require("node:async_hooks");
4
+ let _intlayer_config_built = require("@intlayer/config/built");
3
5
  let _intlayer_config_node = require("@intlayer/config/node");
4
6
  let _intlayer_core_interpreter = require("@intlayer/core/interpreter");
5
7
  let _intlayer_core_utils = require("@intlayer/core/utils");
6
8
  let _intlayer_engine_build = require("@intlayer/engine/build");
7
- let cls_hooked = require("cls-hooked");
8
9
 
9
10
  //#region src/index.ts
10
11
  let debug = () => {};
11
- const configuration = (0, _intlayer_config_node.getConfiguration)();
12
- const { internationalization } = configuration;
13
- if (process.env["NODE_ENV"] === "development") try {
12
+ if (process.env.NODE_ENV === "development") try {
14
13
  const logger = require("@adonisjs/core/services/logger").default;
15
14
  debug = (msg) => logger.debug(msg);
16
15
  } catch {
17
16
  debug = (msg) => console.debug(msg);
18
17
  }
19
- const appNamespace = (0, cls_hooked.createNamespace)("app");
20
- (0, _intlayer_engine_build.prepareIntlayer)(configuration);
18
+ /**
19
+ * Request-scoped Intlayer context, entered by `IntlayerMiddleware`.
20
+ *
21
+ * `AsyncLocalStorage` rather than `cls-hooked`: the latter relies on
22
+ * `async_hooks.createHook`, which Bun does not implement, and is several times
23
+ * slower on Node.
24
+ */
25
+ const intlayerStorage = new node_async_hooks.AsyncLocalStorage();
26
+ (0, _intlayer_core_interpreter.registerAmbientLocaleResolver)(() => intlayerStorage.getStore()?.locale);
27
+ (0, _intlayer_engine_build.prepareIntlayerServer)((0, _intlayer_config_node.getConfiguration)(), { label: "adonis-intlayer" });
21
28
  /**
22
29
  * Retrieves the locale from storage (cookies, headers).
30
+ *
31
+ * `request.cookie` only reads signed cookies: the locale cookie set by the
32
+ * Intlayer client libraries is a plain one.
23
33
  */
24
34
  const getStorageLocale = (ctx) => (0, _intlayer_core_utils.getLocaleFromStorageServer)({
25
- getCookie: (name) => ctx.request.cookie(name),
35
+ getCookie: (name) => ctx.request.cookie(name) ?? ctx.request.plainCookie(name, { encoded: false }),
26
36
  getHeader: (name) => ctx.request.header(name)
27
37
  });
28
38
  /**
29
- * Translation function for context
39
+ * Builds the translation function bound to the locale stored on the HTTP context.
30
40
  */
31
- const translateFunction = (ctx) => (content, locale) => {
32
- const { locale: currentLocale, defaultLocale } = ctx;
33
- const targetLocale = locale ?? currentLocale;
34
- if (typeof content === "undefined") return "";
35
- if (typeof content === "string") return content;
36
- if (typeof content?.[targetLocale] === "undefined") {
37
- if (typeof content?.[defaultLocale] === "undefined") return content;
38
- else return (0, _intlayer_core_interpreter.getTranslation)(content, defaultLocale);
39
- }
40
- return (0, _intlayer_core_interpreter.getTranslation)(content, targetLocale);
41
- };
41
+ const translateFunction = (ctx) => (content, locale) => typeof content === "string" ? content : (0, _intlayer_core_interpreter.getTranslation)(content, locale ?? ctx.locale, _intlayer_config_built.internationalization.defaultLocale);
42
42
  /**
43
43
  * Translation function to retrieve content for the current locale.
44
44
  *
@@ -62,53 +62,41 @@ const translateFunction = (ctx) => (content, locale) => {
62
62
  * ```
63
63
  */
64
64
  const t = (content, locale) => {
65
- try {
66
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.");
67
- if (typeof appNamespace.get("t") !== "function") throw new Error("Using the import { t } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.");
68
- return appNamespace.get("t")(content, locale);
69
- } catch (error) {
70
- debug(error.message);
71
- return (0, _intlayer_core_interpreter.getTranslation)(content, locale ?? internationalization.defaultLocale);
65
+ const context = intlayerStorage.getStore();
66
+ if (!context) {
67
+ debug("Using the import { t } from \"adonis-intlayer\" outside of a request handled by the `intlayer` middleware. Ensure you are within a request context.");
68
+ return (0, _intlayer_core_interpreter.getTranslation)(content, locale ?? _intlayer_config_built.internationalization.defaultLocale);
72
69
  }
70
+ return context.t(content, locale);
73
71
  };
74
- const getIntlayer = ((key, localeArg, ...props) => {
75
- try {
76
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.");
77
- const getIntlayerWrapped = appNamespace.get("getIntlayer");
78
- if (typeof getIntlayerWrapped !== "function") throw new Error("Using the import { getIntlayer } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.");
79
- return getIntlayerWrapped(key, localeArg, ...props);
80
- } catch (error) {
81
- debug(error.message);
82
- return (0, _intlayer_core_interpreter.getIntlayer)(key, localeArg, ...props);
72
+ const getIntlayer = (...args) => {
73
+ const context = intlayerStorage.getStore();
74
+ if (!context) {
75
+ debug("Using the import { getIntlayer } from \"adonis-intlayer\" outside of a request handled by the `intlayer` middleware. Ensure you are within a request context.");
76
+ return (0, _intlayer_core_interpreter.getIntlayer)(...args);
83
77
  }
84
- });
85
- const getDictionary = ((key, localeArg, ...props) => {
86
- try {
87
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.");
88
- const getDictionaryWrapped = appNamespace.get("getDictionary");
89
- if (typeof getDictionaryWrapped !== "function") throw new Error("Using the import { getDictionary } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.");
90
- return getDictionaryWrapped(key, localeArg, ...props);
91
- } catch (error) {
92
- debug(error.message);
93
- return (0, _intlayer_core_interpreter.getDictionary)(key, localeArg, ...props);
94
- }
95
- });
96
- const getLocale = (locale) => {
97
- try {
98
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.");
99
- return appNamespace.get("locale") ?? locale ?? internationalization.defaultLocale;
100
- } catch (_error) {
101
- return locale ?? internationalization.defaultLocale;
78
+ return context.getIntlayer(...args);
79
+ };
80
+ const getDictionary = (...args) => {
81
+ const context = intlayerStorage.getStore();
82
+ if (!context) {
83
+ debug("Using the import { getDictionary } from \"adonis-intlayer\" outside of a request handled by the `intlayer` middleware. Ensure you are within a request context.");
84
+ return (0, _intlayer_core_interpreter.getDictionary)(...args);
102
85
  }
86
+ return context.getDictionary(...args);
103
87
  };
88
+ /**
89
+ * Returns the locale of the request being handled, else `locale`, else the default locale.
90
+ */
91
+ const getLocale = (locale) => intlayerStorage.getStore()?.locale ?? locale ?? _intlayer_config_built.internationalization.defaultLocale;
104
92
 
105
93
  //#endregion
106
94
  exports.IntlayerMiddleware = require_middleware;
107
- exports.appNamespace = appNamespace;
108
95
  exports.getDictionary = getDictionary;
109
96
  exports.getIntlayer = getIntlayer;
110
97
  exports.getLocale = getLocale;
111
98
  exports.getStorageLocale = getStorageLocale;
99
+ exports.intlayerStorage = intlayerStorage;
112
100
  exports.t = t;
113
101
  exports.translateFunction = translateFunction;
114
102
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["getConfiguration","createNamespace","prepareIntlayer","getLocaleFromStorageServer","getTranslation","getIntlayerFunction","getDictionaryFunction"],"sources":["../../src/index.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport { getConfiguration } from '@intlayer/config/node';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n} from '@intlayer/core/interpreter';\nimport { getLocaleFromStorageServer } from '@intlayer/core/utils';\nimport { prepareIntlayer } from '@intlayer/engine/build';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\nimport { createNamespace } from 'cls-hooked';\n\n// Zero-cost fallback, will be updated with AdonisJS logger or console in dev mode\nlet debug: (message: string) => void = () => {};\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\nif (process.env['NODE_ENV'] === 'development') {\n try {\n const logger = require('@adonisjs/core/services/logger').default;\n debug = (msg: string) => logger.debug(msg);\n } catch {\n debug = (msg: string) => console.debug(msg);\n }\n}\n\nexport const appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n */\nexport const getStorageLocale = (ctx: HttpContext): Locale | undefined =>\n getLocaleFromStorageServer({\n getCookie: (name: string) => ctx.request.cookie(name),\n getHeader: (name: string) => ctx.request.header(name),\n });\n\n/**\n * Translation function for context\n */\nexport const translateFunction =\n (ctx: HttpContext) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n const { locale: currentLocale, defaultLocale } = ctx as unknown as {\n locale: Locale;\n defaultLocale: Locale;\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?.[\n targetLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof StrictModeLocaleMap<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 * Translation function to retrieve content for the current locale.\n *\n * This function works within the request lifecycle managed by the `intlayer` middleware.\n *\n * @param content - A map of locales to content.\n * @param locale - Optional locale override.\n * @returns The translated content.\n *\n * @example\n * ```ts\n * import { t } from 'adonis-intlayer';\n *\n * router.get('/', async () => {\n * const greeting = t({\n * en: 'Hello',\n * fr: 'Bonjour',\n * });\n * return greeting;\n * });\n * ```\n */\nexport const t = <Content = string>(\n content: StrictModeLocaleMap<Content>,\n locale?: Locale\n): Content => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return appNamespace.get('t')(content, locale);\n } catch (error) {\n debug((error as Error).message);\n\n return getTranslation(\n content,\n locale ?? internationalization.defaultLocale\n );\n }\n};\n\nexport const getIntlayer: typeof getIntlayerFunction = ((\n key: any,\n localeArg?: any,\n ...props: any[]\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n const getIntlayerWrapped = appNamespace.get('getIntlayer');\n\n if (typeof getIntlayerWrapped !== 'function') {\n throw new Error(\n 'Using the import { getIntlayer } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return getIntlayerWrapped(key, localeArg, ...props);\n } catch (error) {\n debug((error as Error).message);\n\n return getIntlayerFunction(key, localeArg, ...props);\n }\n}) as typeof getIntlayerFunction;\n\nexport const getDictionary: typeof getDictionaryFunction = ((\n key: any,\n localeArg?: any,\n ...props: any[]\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n const getDictionaryWrapped = appNamespace.get('getDictionary');\n\n if (typeof getDictionaryWrapped !== 'function') {\n throw new Error(\n 'Using the import { getDictionary } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return getDictionaryWrapped(key, localeArg, ...props);\n } catch (error) {\n debug((error as Error).message);\n\n return getDictionaryFunction(key, localeArg, ...props);\n }\n}) as typeof getDictionaryFunction;\n\nexport const getLocale = (locale?: Locale): Locale => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n return (\n appNamespace.get('locale') ?? locale ?? internationalization.defaultLocale\n );\n } catch (_error) {\n return locale ?? internationalization.defaultLocale;\n }\n};\n\nexport { default as IntlayerMiddleware } from './middleware';\n"],"mappings":";;;;;;;;;AAcA,IAAI,cAAyC,CAAC;AAE9C,MAAM,oBAAgBA,wCAAiB;AACvC,MAAM,EAAE,yBAAyB;AAEjC,IAAI,QAAQ,IAAI,gBAAgB,eAC9B,IAAI;CACF,MAAM,SAAS,QAAQ,gCAAgC,CAAC,CAAC;CACzD,SAAS,QAAgB,OAAO,MAAM,GAAG;AAC3C,QAAQ;CACN,SAAS,QAAgB,QAAQ,MAAM,GAAG;AAC5C;AAGF,MAAa,mBAAeC,4BAAgB,KAAK;IAEjDC,wCAAgB,aAAa;;;;AAK7B,MAAa,oBAAoB,YAC/BC,iDAA2B;CACzB,YAAY,SAAiB,IAAI,QAAQ,OAAO,IAAI;CACpD,YAAY,SAAiB,IAAI,QAAQ,OAAO,IAAI;AACtD,CAAC;;;;AAKH,MAAa,qBACV,SAEC,SACA,WACM;CACN,MAAM,EAAE,QAAQ,eAAe,kBAAkB;CAKjD,MAAM,eAAe,UAAU;CAE/B,IAAI,OAAO,YAAY,aACrB,OAAO;CAGT,IAAI,OAAO,YAAY,UACrB,OAAO;CAGT,IACE,OAAO,UACL,kBACI,aACN;EACA,IACE,OAAO,UACL,mBACI,aAEN,OAAO;OAEP,WAAOC,2CAAe,SAAS,aAAa;CAEhD;CAEA,WAAOA,2CAAe,SAAS,YAAY;AAC7C;;;;;;;;;;;;;;;;;;;;;;;AAwBF,MAAa,KACX,SACA,WACY;CACZ,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,uGACF;EAGF,IAAI,OAAO,aAAa,IAAI,GAAG,MAAM,YACnC,MAAM,IAAI,MACR,gIACF;EAGF,OAAO,aAAa,IAAI,GAAG,CAAC,CAAC,SAAS,MAAM;CAC9C,SAAS,OAAO;EACd,MAAO,MAAgB,OAAO;EAE9B,WAAOA,2CACL,SACA,UAAU,qBAAqB,aACjC;CACF;AACF;AAEA,MAAa,gBACX,KACA,WACA,GAAG,UACA;CACH,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,uGACF;EAGF,MAAM,qBAAqB,aAAa,IAAI,aAAa;EAEzD,IAAI,OAAO,uBAAuB,YAChC,MAAM,IAAI,MACR,0IACF;EAGF,OAAO,mBAAmB,KAAK,WAAW,GAAG,KAAK;CACpD,SAAS,OAAO;EACd,MAAO,MAAgB,OAAO;EAE9B,WAAOC,wCAAoB,KAAK,WAAW,GAAG,KAAK;CACrD;AACF;AAEA,MAAa,kBACX,KACA,WACA,GAAG,UACA;CACH,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,uGACF;EAGF,MAAM,uBAAuB,aAAa,IAAI,eAAe;EAE7D,IAAI,OAAO,yBAAyB,YAClC,MAAM,IAAI,MACR,4IACF;EAGF,OAAO,qBAAqB,KAAK,WAAW,GAAG,KAAK;CACtD,SAAS,OAAO;EACd,MAAO,MAAgB,OAAO;EAE9B,WAAOC,0CAAsB,KAAK,WAAW,GAAG,KAAK;CACvD;AACF;AAEA,MAAa,aAAa,WAA4B;CACpD,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,uGACF;EAGF,OACE,aAAa,IAAI,QAAQ,KAAK,UAAU,qBAAqB;CAEjE,SAAS,QAAQ;EACf,OAAO,UAAU,qBAAqB;CACxC;AACF"}
1
+ {"version":3,"file":"index.cjs","names":["AsyncLocalStorage","registerAmbientLocaleResolver","prepareIntlayerServer","getConfiguration","getLocaleFromStorageServer","getTranslation","internationalization","getIntlayerFunction","getDictionaryFunction"],"sources":["../../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { HttpContext } from '@adonisjs/core/http';\nimport { internationalization } from '@intlayer/config/built';\nimport { getConfiguration } from '@intlayer/config/node';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n registerAmbientLocaleResolver,\n} from '@intlayer/core/interpreter';\nimport { getLocaleFromStorageServer } from '@intlayer/core/utils';\nimport { prepareIntlayerServer } from '@intlayer/engine/build';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\n\n// Zero-cost fallback, will be updated with AdonisJS logger or console in dev mode\nlet debug: (message: string) => void = () => {};\n\nif (process.env['NODE_ENV'] === 'development') {\n try {\n const logger = require('@adonisjs/core/services/logger').default;\n debug = (msg: string) => logger.debug(msg);\n } catch {\n debug = (msg: string) => console.debug(msg);\n }\n}\n\n/**\n * Intlayer helpers bound to the request being handled.\n */\nexport type IntlayerRequestContext = {\n locale: Locale;\n t: ReturnType<typeof translateFunction>;\n getIntlayer: typeof getIntlayerFunction;\n getDictionary: typeof getDictionaryFunction;\n};\n\n/**\n * Request-scoped Intlayer context, entered by `IntlayerMiddleware`.\n *\n * `AsyncLocalStorage` rather than `cls-hooked`: the latter relies on\n * `async_hooks.createHook`, which Bun does not implement, and is several times\n * slower on Node.\n */\nexport const intlayerStorage = new AsyncLocalStorage<IntlayerRequestContext>();\n\n// Lets a bare `getIntlayer` / `getDictionary` from any Intlayer package resolve\n// to the locale of the request being handled.\nregisterAmbientLocaleResolver(() => intlayerStorage.getStore()?.locale);\n\nprepareIntlayerServer(getConfiguration(), { label: 'adonis-intlayer' });\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n *\n * `request.cookie` only reads signed cookies: the locale cookie set by the\n * Intlayer client libraries is a plain one.\n */\nexport const getStorageLocale = (ctx: HttpContext): Locale | undefined =>\n getLocaleFromStorageServer({\n getCookie: (name: string) =>\n ctx.request.cookie(name) ??\n ctx.request.plainCookie(name, { encoded: false }),\n getHeader: (name: string) => ctx.request.header(name),\n });\n\n/**\n * Builds the translation function bound to the locale stored on the HTTP context.\n */\nexport const translateFunction =\n (ctx: HttpContext) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T =>\n typeof content === 'string'\n ? (content as T)\n : getTranslation(\n content,\n locale ?? (ctx as HttpContext & { locale: Locale }).locale,\n internationalization.defaultLocale\n );\n\n/**\n * Translation function to retrieve content for the current locale.\n *\n * This function works within the request lifecycle managed by the `intlayer` middleware.\n *\n * @param content - A map of locales to content.\n * @param locale - Optional locale override.\n * @returns The translated content.\n *\n * @example\n * ```ts\n * import { t } from 'adonis-intlayer';\n *\n * router.get('/', async () => {\n * const greeting = t({\n * en: 'Hello',\n * fr: 'Bonjour',\n * });\n * return greeting;\n * });\n * ```\n */\nexport const t = <Content = string>(\n content: StrictModeLocaleMap<Content>,\n locale?: Locale\n): Content => {\n const context = intlayerStorage.getStore();\n\n if (!context) {\n debug(\n 'Using the import { t } from \"adonis-intlayer\" outside of a request handled by the `intlayer` middleware. Ensure you are within a request context.'\n );\n\n return getTranslation(\n content,\n locale ?? internationalization.defaultLocale\n );\n }\n\n return context.t(content as StrictModeLocaleMap<string>, locale) as Content;\n};\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) => {\n const context = intlayerStorage.getStore();\n\n if (!context) {\n debug(\n 'Using the import { getIntlayer } from \"adonis-intlayer\" outside of a request handled by the `intlayer` middleware. Ensure you are within a request context.'\n );\n\n return getIntlayerFunction(...args);\n }\n\n return context.getIntlayer(...args);\n};\n\nexport const getDictionary: typeof getDictionaryFunction = (...args) => {\n const context = intlayerStorage.getStore();\n\n if (!context) {\n debug(\n 'Using the import { getDictionary } from \"adonis-intlayer\" outside of a request handled by the `intlayer` middleware. Ensure you are within a request context.'\n );\n\n return getDictionaryFunction(...args);\n }\n\n return context.getDictionary(...args);\n};\n\n/**\n * Returns the locale of the request being handled, else `locale`, else the default locale.\n */\nexport const getLocale = (locale?: Locale): Locale =>\n intlayerStorage.getStore()?.locale ??\n locale ??\n internationalization.defaultLocale;\n\nexport { default as IntlayerMiddleware } from './middleware';\n"],"mappings":";;;;;;;;;;AAgBA,IAAI,cAAyC,CAAC;AAE9C,6BAAgC,eAC9B,IAAI;CACF,MAAM,SAAS,QAAQ,gCAAgC,CAAC,CAAC;CACzD,SAAS,QAAgB,OAAO,MAAM,GAAG;AAC3C,QAAQ;CACN,SAAS,QAAgB,QAAQ,MAAM,GAAG;AAC5C;;;;;;;;AAoBF,MAAa,kBAAkB,IAAIA,mCAA0C;IAI7EC,gEAAoC,gBAAgB,SAAS,CAAC,EAAE,MAAM;IAEtEC,kDAAsBC,wCAAiB,GAAG,EAAE,OAAO,kBAAkB,CAAC;;;;;;;AAQtE,MAAa,oBAAoB,YAC/BC,iDAA2B;CACzB,YAAY,SACV,IAAI,QAAQ,OAAO,IAAI,KACvB,IAAI,QAAQ,YAAY,MAAM,EAAE,SAAS,MAAM,CAAC;CAClD,YAAY,SAAiB,IAAI,QAAQ,OAAO,IAAI;AACtD,CAAC;;;;AAKH,MAAa,qBACV,SAEC,SACA,WAEA,OAAO,YAAY,WACd,cACDC,2CACE,SACA,UAAW,IAAyC,QACpDC,4CAAqB,aACvB;;;;;;;;;;;;;;;;;;;;;;;AAwBR,MAAa,KACX,SACA,WACY;CACZ,MAAM,UAAU,gBAAgB,SAAS;CAEzC,IAAI,CAAC,SAAS;EACZ,MACE,qJACF;EAEA,WAAOD,2CACL,SACA,UAAUC,4CAAqB,aACjC;CACF;CAEA,OAAO,QAAQ,EAAE,SAAwC,MAAM;AACjE;AAEA,MAAa,eAA2C,GAAG,SAAS;CAClE,MAAM,UAAU,gBAAgB,SAAS;CAEzC,IAAI,CAAC,SAAS;EACZ,MACE,+JACF;EAEA,WAAOC,wCAAoB,GAAG,IAAI;CACpC;CAEA,OAAO,QAAQ,YAAY,GAAG,IAAI;AACpC;AAEA,MAAa,iBAA+C,GAAG,SAAS;CACtE,MAAM,UAAU,gBAAgB,SAAS;CAEzC,IAAI,CAAC,SAAS;EACZ,MACE,iKACF;EAEA,WAAOC,0CAAsB,GAAG,IAAI;CACtC;CAEA,OAAO,QAAQ,cAAc,GAAG,IAAI;AACtC;;;;AAKA,MAAa,aAAa,WACxB,gBAAgB,SAAS,CAAC,EAAE,UAC5B,UACAF,4CAAqB"}
@@ -1,37 +1,29 @@
1
1
  const require_index = require('./index.cjs');
2
- let _intlayer_config_node = require("@intlayer/config/node");
2
+ let _intlayer_config_built = require("@intlayer/config/built");
3
3
  let _intlayer_core_interpreter = require("@intlayer/core/interpreter");
4
4
  let _intlayer_core_localization = require("@intlayer/core/localization");
5
5
 
6
6
  //#region src/middleware.ts
7
- const { internationalization } = (0, _intlayer_config_node.getConfiguration)();
8
7
  /**
9
8
  * AdonisJS middleware that detects the user's locale and populates the context with Intlayer data.
10
9
  */
11
10
  var IntlayerMiddleware = class {
12
11
  async handle(ctx, next) {
13
12
  const localeFromStorage = require_index.getStorageLocale(ctx);
14
- const negotiatorHeaders = {};
15
- const headers = ctx.request.headers();
16
- for (const key in headers) {
17
- const val = headers[key];
18
- if (typeof val === "string") negotiatorHeaders[key] = val;
19
- else if (Array.isArray(val)) negotiatorHeaders[key] = val.join(",");
20
- }
21
- const localeDetected = (0, _intlayer_core_localization.localeDetector)(negotiatorHeaders, internationalization.locales, internationalization.defaultLocale);
13
+ const localeDetected = (0, _intlayer_core_localization.localeDetector)({ "accept-language": ctx.request.header("accept-language") }, _intlayer_config_built.internationalization.locales, _intlayer_config_built.internationalization.defaultLocale);
22
14
  const locale = localeFromStorage ?? localeDetected;
23
- ctx.locale = locale;
24
- ctx.defaultLocale = internationalization.defaultLocale;
15
+ const decoratedContext = ctx;
16
+ decoratedContext.locale = locale;
17
+ decoratedContext.defaultLocale = _intlayer_config_built.internationalization.defaultLocale;
25
18
  const t = require_index.translateFunction(ctx);
26
19
  const getIntlayer = (key, localeArg, ...props) => (0, _intlayer_core_interpreter.getIntlayer)(key, localeArg ?? locale, ...props);
27
20
  const getDictionary = (key, localeArg, ...props) => (0, _intlayer_core_interpreter.getDictionary)(key, localeArg ?? locale, ...props);
28
- await require_index.appNamespace.runPromise(async () => {
29
- require_index.appNamespace.set("locale", locale);
30
- require_index.appNamespace.set("t", t);
31
- require_index.appNamespace.set("getIntlayer", getIntlayer);
32
- require_index.appNamespace.set("getDictionary", getDictionary);
33
- await next();
34
- });
21
+ await require_index.intlayerStorage.run({
22
+ locale,
23
+ t,
24
+ getIntlayer,
25
+ getDictionary
26
+ }, next);
35
27
  }
36
28
  };
37
29
 
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.cjs","names":["getConfiguration","getStorageLocale","localeDetector","translateFunction","getIntlayerFunction","getDictionaryFunction","appNamespace"],"sources":["../../src/middleware.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport type { NextFn } from '@adonisjs/core/types/http';\nimport { getConfiguration } from '@intlayer/config/node';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n} from '@intlayer/core/interpreter';\nimport { localeDetector } from '@intlayer/core/localization';\nimport { appNamespace, getStorageLocale, translateFunction } from './index';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * AdonisJS middleware that detects the user's locale and populates the context with Intlayer data.\n */\nexport default class IntlayerMiddleware {\n async handle(ctx: HttpContext, next: NextFn) {\n // Detect if locale is set by intlayer frontend lib in the headers or cookies\n const localeFromStorage = getStorageLocale(ctx);\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // Copy all headers from the request to negotiatorHeaders\n const headers = ctx.request.headers();\n for (const key in headers) {\n const val = headers[key];\n if (typeof val === 'string') {\n negotiatorHeaders[key] = val;\n } else if (Array.isArray(val)) {\n negotiatorHeaders[key] = val.join(',');\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n\n // Decorate context\n (ctx as any).locale = locale;\n (ctx as any).defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(ctx);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg,\n ...props\n ) =>\n getIntlayerFunction(\n key,\n (localeArg ?? locale) as typeof localeArg,\n ...props\n );\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg,\n ...props\n ) =>\n getDictionaryFunction(\n key,\n (localeArg ?? locale) as typeof localeArg,\n ...props\n );\n\n // Make functions available via CLS\n await appNamespace.runPromise(async () => {\n appNamespace.set('locale', locale);\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n await next();\n });\n }\n}\n"],"mappings":";;;;;;AAWA,MAAM,EAAE,6BADcA,wCACuB;;;;AAK7C,IAAqB,qBAArB,MAAwC;CACtC,MAAM,OAAO,KAAkB,MAAc;EAE3C,MAAM,oBAAoBC,+BAAiB,GAAG;EAE9C,MAAM,oBAA4C,CAAC;EAGnD,MAAM,UAAU,IAAI,QAAQ,QAAQ;EACpC,KAAK,MAAM,OAAO,SAAS;GACzB,MAAM,MAAM,QAAQ;GACpB,IAAI,OAAO,QAAQ,UACjB,kBAAkB,OAAO;QACpB,IAAI,MAAM,QAAQ,GAAG,GAC1B,kBAAkB,OAAO,IAAI,KAAK,GAAG;EAEzC;EAEA,MAAM,qBAAiBC,4CACrB,mBACA,qBAAqB,SACrB,qBAAqB,aACvB;EAEA,MAAM,SAAS,qBAAqB;EAGpC,AAAC,IAAY,SAAS;EACtB,AAAC,IAAY,gBAAgB,qBAAqB;EAElD,MAAM,IAAIC,gCAAkB,GAAG;EAE/B,MAAM,eACJ,KACA,WACA,GAAG,cAEHC,wCACE,KACC,aAAa,QACd,GAAG,KACL;EAEF,MAAM,iBACJ,KACA,WACA,GAAG,cAEHC,0CACE,KACC,aAAa,QACd,GAAG,KACL;EAGF,MAAMC,2BAAa,WAAW,YAAY;GACxC,2BAAa,IAAI,UAAU,MAAM;GACjC,2BAAa,IAAI,KAAK,CAAC;GACvB,2BAAa,IAAI,eAAe,WAAW;GAC3C,2BAAa,IAAI,iBAAiB,aAAa;GAE/C,MAAM,KAAK;EACb,CAAC;CACH;AACF"}
1
+ {"version":3,"file":"middleware.cjs","names":["getStorageLocale","localeDetector","internationalization","translateFunction","getIntlayerFunction","getDictionaryFunction","intlayerStorage"],"sources":["../../src/middleware.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport type { NextFn } from '@adonisjs/core/types/http';\nimport { internationalization } from '@intlayer/config/built';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n} from '@intlayer/core/interpreter';\nimport { localeDetector } from '@intlayer/core/localization';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport { getStorageLocale, intlayerStorage, translateFunction } from './index';\n\n/**\n * AdonisJS middleware that detects the user's locale and populates the context with Intlayer data.\n */\nexport default class IntlayerMiddleware {\n async handle(ctx: HttpContext, next: NextFn) {\n // Detect if locale is set by intlayer frontend lib in the headers or cookies\n const localeFromStorage = getStorageLocale(ctx);\n\n const localeDetected = localeDetector(\n { 'accept-language': ctx.request.header('accept-language') },\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n\n // Decorate context\n const decoratedContext = ctx as HttpContext & {\n locale: Locale;\n defaultLocale: Locale;\n };\n decoratedContext.locale = locale;\n decoratedContext.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(ctx);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg,\n ...props\n ) =>\n getIntlayerFunction(\n key,\n (localeArg ?? locale) as typeof localeArg,\n ...props\n );\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg,\n ...props\n ) =>\n getDictionaryFunction(\n key,\n (localeArg ?? locale) as typeof localeArg,\n ...props\n );\n\n // Make functions available to the standalone exports for this request\n await intlayerStorage.run({ locale, t, getIntlayer, getDictionary }, next);\n }\n}\n"],"mappings":";;;;;;;;;AAcA,IAAqB,qBAArB,MAAwC;CACtC,MAAM,OAAO,KAAkB,MAAc;EAE3C,MAAM,oBAAoBA,+BAAiB,GAAG;EAE9C,MAAM,qBAAiBC,4CACrB,EAAE,mBAAmB,IAAI,QAAQ,OAAO,iBAAiB,EAAE,GAC3DC,4CAAqB,SACrBA,4CAAqB,aACvB;EAEA,MAAM,SAAS,qBAAqB;EAGpC,MAAM,mBAAmB;EAIzB,iBAAiB,SAAS;EAC1B,iBAAiB,gBAAgBA,4CAAqB;EAEtD,MAAM,IAAIC,gCAAkB,GAAG;EAE/B,MAAM,eACJ,KACA,WACA,GAAG,cAEHC,wCACE,KACC,aAAa,QACd,GAAG,KACL;EAEF,MAAM,iBACJ,KACA,WACA,GAAG,cAEHC,0CACE,KACC,aAAa,QACd,GAAG,KACL;EAGF,MAAMC,8BAAgB,IAAI;GAAE;GAAQ;GAAG;GAAa;EAAc,GAAG,IAAI;CAC3E;AACF"}
@@ -1,44 +1,44 @@
1
1
  import { __require } from "./_virtual/_rolldown/runtime.mjs";
2
2
  import IntlayerMiddleware from "./middleware.mjs";
3
+ import { AsyncLocalStorage } from "node:async_hooks";
4
+ import { internationalization } from "@intlayer/config/built";
3
5
  import { getConfiguration } from "@intlayer/config/node";
4
- import { getDictionary as getDictionary$1, getIntlayer as getIntlayer$1, getTranslation } from "@intlayer/core/interpreter";
6
+ import { getDictionary as getDictionary$1, getIntlayer as getIntlayer$1, getTranslation, registerAmbientLocaleResolver } from "@intlayer/core/interpreter";
5
7
  import { getLocaleFromStorageServer } from "@intlayer/core/utils";
6
- import { prepareIntlayer } from "@intlayer/engine/build";
7
- import { createNamespace } from "cls-hooked";
8
+ import { prepareIntlayerServer } from "@intlayer/engine/build";
8
9
 
9
10
  //#region src/index.ts
10
11
  let debug = () => {};
11
- const configuration = getConfiguration();
12
- const { internationalization } = configuration;
13
- try {
12
+ if (process.env.NODE_ENV === "development") try {
14
13
  const logger = __require("@adonisjs/core/services/logger").default;
15
14
  debug = (msg) => logger.debug(msg);
16
15
  } catch {
17
16
  debug = (msg) => console.debug(msg);
18
17
  }
19
- const appNamespace = createNamespace("app");
20
- prepareIntlayer(configuration);
18
+ /**
19
+ * Request-scoped Intlayer context, entered by `IntlayerMiddleware`.
20
+ *
21
+ * `AsyncLocalStorage` rather than `cls-hooked`: the latter relies on
22
+ * `async_hooks.createHook`, which Bun does not implement, and is several times
23
+ * slower on Node.
24
+ */
25
+ const intlayerStorage = new AsyncLocalStorage();
26
+ registerAmbientLocaleResolver(() => intlayerStorage.getStore()?.locale);
27
+ prepareIntlayerServer(getConfiguration(), { label: "adonis-intlayer" });
21
28
  /**
22
29
  * Retrieves the locale from storage (cookies, headers).
30
+ *
31
+ * `request.cookie` only reads signed cookies: the locale cookie set by the
32
+ * Intlayer client libraries is a plain one.
23
33
  */
24
34
  const getStorageLocale = (ctx) => getLocaleFromStorageServer({
25
- getCookie: (name) => ctx.request.cookie(name),
35
+ getCookie: (name) => ctx.request.cookie(name) ?? ctx.request.plainCookie(name, { encoded: false }),
26
36
  getHeader: (name) => ctx.request.header(name)
27
37
  });
28
38
  /**
29
- * Translation function for context
39
+ * Builds the translation function bound to the locale stored on the HTTP context.
30
40
  */
31
- const translateFunction = (ctx) => (content, locale) => {
32
- const { locale: currentLocale, defaultLocale } = ctx;
33
- const targetLocale = locale ?? currentLocale;
34
- if (typeof content === "undefined") return "";
35
- if (typeof content === "string") return content;
36
- if (typeof content?.[targetLocale] === "undefined") {
37
- if (typeof content?.[defaultLocale] === "undefined") return content;
38
- else return getTranslation(content, defaultLocale);
39
- }
40
- return getTranslation(content, targetLocale);
41
- };
41
+ const translateFunction = (ctx) => (content, locale) => typeof content === "string" ? content : getTranslation(content, locale ?? ctx.locale, internationalization.defaultLocale);
42
42
  /**
43
43
  * Translation function to retrieve content for the current locale.
44
44
  *
@@ -62,46 +62,34 @@ const translateFunction = (ctx) => (content, locale) => {
62
62
  * ```
63
63
  */
64
64
  const t = (content, locale) => {
65
- try {
66
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.");
67
- if (typeof appNamespace.get("t") !== "function") throw new Error("Using the import { t } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.");
68
- return appNamespace.get("t")(content, locale);
69
- } catch (error) {
70
- debug(error.message);
65
+ const context = intlayerStorage.getStore();
66
+ if (!context) {
67
+ debug("Using the import { t } from \"adonis-intlayer\" outside of a request handled by the `intlayer` middleware. Ensure you are within a request context.");
71
68
  return getTranslation(content, locale ?? internationalization.defaultLocale);
72
69
  }
70
+ return context.t(content, locale);
73
71
  };
74
- const getIntlayer = ((key, localeArg, ...props) => {
75
- try {
76
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.");
77
- const getIntlayerWrapped = appNamespace.get("getIntlayer");
78
- if (typeof getIntlayerWrapped !== "function") throw new Error("Using the import { getIntlayer } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.");
79
- return getIntlayerWrapped(key, localeArg, ...props);
80
- } catch (error) {
81
- debug(error.message);
82
- return getIntlayer$1(key, localeArg, ...props);
72
+ const getIntlayer = (...args) => {
73
+ const context = intlayerStorage.getStore();
74
+ if (!context) {
75
+ debug("Using the import { getIntlayer } from \"adonis-intlayer\" outside of a request handled by the `intlayer` middleware. Ensure you are within a request context.");
76
+ return getIntlayer$1(...args);
83
77
  }
84
- });
85
- const getDictionary = ((key, localeArg, ...props) => {
86
- try {
87
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.");
88
- const getDictionaryWrapped = appNamespace.get("getDictionary");
89
- if (typeof getDictionaryWrapped !== "function") throw new Error("Using the import { getDictionary } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.");
90
- return getDictionaryWrapped(key, localeArg, ...props);
91
- } catch (error) {
92
- debug(error.message);
93
- return getDictionary$1(key, localeArg, ...props);
94
- }
95
- });
96
- const getLocale = (locale) => {
97
- try {
98
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.");
99
- return appNamespace.get("locale") ?? locale ?? internationalization.defaultLocale;
100
- } catch (_error) {
101
- return locale ?? internationalization.defaultLocale;
78
+ return context.getIntlayer(...args);
79
+ };
80
+ const getDictionary = (...args) => {
81
+ const context = intlayerStorage.getStore();
82
+ if (!context) {
83
+ debug("Using the import { getDictionary } from \"adonis-intlayer\" outside of a request handled by the `intlayer` middleware. Ensure you are within a request context.");
84
+ return getDictionary$1(...args);
102
85
  }
86
+ return context.getDictionary(...args);
103
87
  };
88
+ /**
89
+ * Returns the locale of the request being handled, else `locale`, else the default locale.
90
+ */
91
+ const getLocale = (locale) => intlayerStorage.getStore()?.locale ?? locale ?? internationalization.defaultLocale;
104
92
 
105
93
  //#endregion
106
- export { IntlayerMiddleware, appNamespace, getDictionary, getIntlayer, getLocale, getStorageLocale, t, translateFunction };
94
+ export { IntlayerMiddleware, getDictionary, getIntlayer, getLocale, getStorageLocale, intlayerStorage, t, translateFunction };
107
95
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["getIntlayerFunction","getDictionaryFunction"],"sources":["../../src/index.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport { getConfiguration } from '@intlayer/config/node';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n} from '@intlayer/core/interpreter';\nimport { getLocaleFromStorageServer } from '@intlayer/core/utils';\nimport { prepareIntlayer } from '@intlayer/engine/build';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\nimport { createNamespace } from 'cls-hooked';\n\n// Zero-cost fallback, will be updated with AdonisJS logger or console in dev mode\nlet debug: (message: string) => void = () => {};\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\nif (process.env['NODE_ENV'] === 'development') {\n try {\n const logger = require('@adonisjs/core/services/logger').default;\n debug = (msg: string) => logger.debug(msg);\n } catch {\n debug = (msg: string) => console.debug(msg);\n }\n}\n\nexport const appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n */\nexport const getStorageLocale = (ctx: HttpContext): Locale | undefined =>\n getLocaleFromStorageServer({\n getCookie: (name: string) => ctx.request.cookie(name),\n getHeader: (name: string) => ctx.request.header(name),\n });\n\n/**\n * Translation function for context\n */\nexport const translateFunction =\n (ctx: HttpContext) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n const { locale: currentLocale, defaultLocale } = ctx as unknown as {\n locale: Locale;\n defaultLocale: Locale;\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?.[\n targetLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof StrictModeLocaleMap<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 * Translation function to retrieve content for the current locale.\n *\n * This function works within the request lifecycle managed by the `intlayer` middleware.\n *\n * @param content - A map of locales to content.\n * @param locale - Optional locale override.\n * @returns The translated content.\n *\n * @example\n * ```ts\n * import { t } from 'adonis-intlayer';\n *\n * router.get('/', async () => {\n * const greeting = t({\n * en: 'Hello',\n * fr: 'Bonjour',\n * });\n * return greeting;\n * });\n * ```\n */\nexport const t = <Content = string>(\n content: StrictModeLocaleMap<Content>,\n locale?: Locale\n): Content => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return appNamespace.get('t')(content, locale);\n } catch (error) {\n debug((error as Error).message);\n\n return getTranslation(\n content,\n locale ?? internationalization.defaultLocale\n );\n }\n};\n\nexport const getIntlayer: typeof getIntlayerFunction = ((\n key: any,\n localeArg?: any,\n ...props: any[]\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n const getIntlayerWrapped = appNamespace.get('getIntlayer');\n\n if (typeof getIntlayerWrapped !== 'function') {\n throw new Error(\n 'Using the import { getIntlayer } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return getIntlayerWrapped(key, localeArg, ...props);\n } catch (error) {\n debug((error as Error).message);\n\n return getIntlayerFunction(key, localeArg, ...props);\n }\n}) as typeof getIntlayerFunction;\n\nexport const getDictionary: typeof getDictionaryFunction = ((\n key: any,\n localeArg?: any,\n ...props: any[]\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n const getDictionaryWrapped = appNamespace.get('getDictionary');\n\n if (typeof getDictionaryWrapped !== 'function') {\n throw new Error(\n 'Using the import { getDictionary } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return getDictionaryWrapped(key, localeArg, ...props);\n } catch (error) {\n debug((error as Error).message);\n\n return getDictionaryFunction(key, localeArg, ...props);\n }\n}) as typeof getDictionaryFunction;\n\nexport const getLocale = (locale?: Locale): Locale => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n return (\n appNamespace.get('locale') ?? locale ?? internationalization.defaultLocale\n );\n } catch (_error) {\n return locale ?? internationalization.defaultLocale;\n }\n};\n\nexport { default as IntlayerMiddleware } from './middleware';\n"],"mappings":";;;;;;;;;AAcA,IAAI,cAAyC,CAAC;AAE9C,MAAM,gBAAgB,iBAAiB;AACvC,MAAM,EAAE,yBAAyB;AAG/B,IAAI;CACF,MAAM,mBAAiB,gCAAgC,CAAC,CAAC;CACzD,SAAS,QAAgB,OAAO,MAAM,GAAG;AAC3C,QAAQ;CACN,SAAS,QAAgB,QAAQ,MAAM,GAAG;AAC5C;AAGF,MAAa,eAAe,gBAAgB,KAAK;AAEjD,gBAAgB,aAAa;;;;AAK7B,MAAa,oBAAoB,QAC/B,2BAA2B;CACzB,YAAY,SAAiB,IAAI,QAAQ,OAAO,IAAI;CACpD,YAAY,SAAiB,IAAI,QAAQ,OAAO,IAAI;AACtD,CAAC;;;;AAKH,MAAa,qBACV,SAEC,SACA,WACM;CACN,MAAM,EAAE,QAAQ,eAAe,kBAAkB;CAKjD,MAAM,eAAe,UAAU;CAE/B,IAAI,OAAO,YAAY,aACrB,OAAO;CAGT,IAAI,OAAO,YAAY,UACrB,OAAO;CAGT,IACE,OAAO,UACL,kBACI,aACN;EACA,IACE,OAAO,UACL,mBACI,aAEN,OAAO;OAEP,OAAO,eAAe,SAAS,aAAa;CAEhD;CAEA,OAAO,eAAe,SAAS,YAAY;AAC7C;;;;;;;;;;;;;;;;;;;;;;;AAwBF,MAAa,KACX,SACA,WACY;CACZ,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,uGACF;EAGF,IAAI,OAAO,aAAa,IAAI,GAAG,MAAM,YACnC,MAAM,IAAI,MACR,gIACF;EAGF,OAAO,aAAa,IAAI,GAAG,CAAC,CAAC,SAAS,MAAM;CAC9C,SAAS,OAAO;EACd,MAAO,MAAgB,OAAO;EAE9B,OAAO,eACL,SACA,UAAU,qBAAqB,aACjC;CACF;AACF;AAEA,MAAa,gBACX,KACA,WACA,GAAG,UACA;CACH,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,uGACF;EAGF,MAAM,qBAAqB,aAAa,IAAI,aAAa;EAEzD,IAAI,OAAO,uBAAuB,YAChC,MAAM,IAAI,MACR,0IACF;EAGF,OAAO,mBAAmB,KAAK,WAAW,GAAG,KAAK;CACpD,SAAS,OAAO;EACd,MAAO,MAAgB,OAAO;EAE9B,OAAOA,cAAoB,KAAK,WAAW,GAAG,KAAK;CACrD;AACF;AAEA,MAAa,kBACX,KACA,WACA,GAAG,UACA;CACH,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,uGACF;EAGF,MAAM,uBAAuB,aAAa,IAAI,eAAe;EAE7D,IAAI,OAAO,yBAAyB,YAClC,MAAM,IAAI,MACR,4IACF;EAGF,OAAO,qBAAqB,KAAK,WAAW,GAAG,KAAK;CACtD,SAAS,OAAO;EACd,MAAO,MAAgB,OAAO;EAE9B,OAAOC,gBAAsB,KAAK,WAAW,GAAG,KAAK;CACvD;AACF;AAEA,MAAa,aAAa,WAA4B;CACpD,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,uGACF;EAGF,OACE,aAAa,IAAI,QAAQ,KAAK,UAAU,qBAAqB;CAEjE,SAAS,QAAQ;EACf,OAAO,UAAU,qBAAqB;CACxC;AACF"}
1
+ {"version":3,"file":"index.mjs","names":["getIntlayerFunction","getDictionaryFunction"],"sources":["../../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { HttpContext } from '@adonisjs/core/http';\nimport { internationalization } from '@intlayer/config/built';\nimport { getConfiguration } from '@intlayer/config/node';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n registerAmbientLocaleResolver,\n} from '@intlayer/core/interpreter';\nimport { getLocaleFromStorageServer } from '@intlayer/core/utils';\nimport { prepareIntlayerServer } from '@intlayer/engine/build';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\n\n// Zero-cost fallback, will be updated with AdonisJS logger or console in dev mode\nlet debug: (message: string) => void = () => {};\n\nif (process.env['NODE_ENV'] === 'development') {\n try {\n const logger = require('@adonisjs/core/services/logger').default;\n debug = (msg: string) => logger.debug(msg);\n } catch {\n debug = (msg: string) => console.debug(msg);\n }\n}\n\n/**\n * Intlayer helpers bound to the request being handled.\n */\nexport type IntlayerRequestContext = {\n locale: Locale;\n t: ReturnType<typeof translateFunction>;\n getIntlayer: typeof getIntlayerFunction;\n getDictionary: typeof getDictionaryFunction;\n};\n\n/**\n * Request-scoped Intlayer context, entered by `IntlayerMiddleware`.\n *\n * `AsyncLocalStorage` rather than `cls-hooked`: the latter relies on\n * `async_hooks.createHook`, which Bun does not implement, and is several times\n * slower on Node.\n */\nexport const intlayerStorage = new AsyncLocalStorage<IntlayerRequestContext>();\n\n// Lets a bare `getIntlayer` / `getDictionary` from any Intlayer package resolve\n// to the locale of the request being handled.\nregisterAmbientLocaleResolver(() => intlayerStorage.getStore()?.locale);\n\nprepareIntlayerServer(getConfiguration(), { label: 'adonis-intlayer' });\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n *\n * `request.cookie` only reads signed cookies: the locale cookie set by the\n * Intlayer client libraries is a plain one.\n */\nexport const getStorageLocale = (ctx: HttpContext): Locale | undefined =>\n getLocaleFromStorageServer({\n getCookie: (name: string) =>\n ctx.request.cookie(name) ??\n ctx.request.plainCookie(name, { encoded: false }),\n getHeader: (name: string) => ctx.request.header(name),\n });\n\n/**\n * Builds the translation function bound to the locale stored on the HTTP context.\n */\nexport const translateFunction =\n (ctx: HttpContext) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T =>\n typeof content === 'string'\n ? (content as T)\n : getTranslation(\n content,\n locale ?? (ctx as HttpContext & { locale: Locale }).locale,\n internationalization.defaultLocale\n );\n\n/**\n * Translation function to retrieve content for the current locale.\n *\n * This function works within the request lifecycle managed by the `intlayer` middleware.\n *\n * @param content - A map of locales to content.\n * @param locale - Optional locale override.\n * @returns The translated content.\n *\n * @example\n * ```ts\n * import { t } from 'adonis-intlayer';\n *\n * router.get('/', async () => {\n * const greeting = t({\n * en: 'Hello',\n * fr: 'Bonjour',\n * });\n * return greeting;\n * });\n * ```\n */\nexport const t = <Content = string>(\n content: StrictModeLocaleMap<Content>,\n locale?: Locale\n): Content => {\n const context = intlayerStorage.getStore();\n\n if (!context) {\n debug(\n 'Using the import { t } from \"adonis-intlayer\" outside of a request handled by the `intlayer` middleware. Ensure you are within a request context.'\n );\n\n return getTranslation(\n content,\n locale ?? internationalization.defaultLocale\n );\n }\n\n return context.t(content as StrictModeLocaleMap<string>, locale) as Content;\n};\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) => {\n const context = intlayerStorage.getStore();\n\n if (!context) {\n debug(\n 'Using the import { getIntlayer } from \"adonis-intlayer\" outside of a request handled by the `intlayer` middleware. Ensure you are within a request context.'\n );\n\n return getIntlayerFunction(...args);\n }\n\n return context.getIntlayer(...args);\n};\n\nexport const getDictionary: typeof getDictionaryFunction = (...args) => {\n const context = intlayerStorage.getStore();\n\n if (!context) {\n debug(\n 'Using the import { getDictionary } from \"adonis-intlayer\" outside of a request handled by the `intlayer` middleware. Ensure you are within a request context.'\n );\n\n return getDictionaryFunction(...args);\n }\n\n return context.getDictionary(...args);\n};\n\n/**\n * Returns the locale of the request being handled, else `locale`, else the default locale.\n */\nexport const getLocale = (locale?: Locale): Locale =>\n intlayerStorage.getStore()?.locale ??\n locale ??\n internationalization.defaultLocale;\n\nexport { default as IntlayerMiddleware } from './middleware';\n"],"mappings":";;;;;;;;;;AAgBA,IAAI,cAAyC,CAAC;AAE9C,6BAAgC,eAC9B,IAAI;CACF,MAAM,mBAAiB,gCAAgC,CAAC,CAAC;CACzD,SAAS,QAAgB,OAAO,MAAM,GAAG;AAC3C,QAAQ;CACN,SAAS,QAAgB,QAAQ,MAAM,GAAG;AAC5C;;;;;;;;AAoBF,MAAa,kBAAkB,IAAI,kBAA0C;AAI7E,oCAAoC,gBAAgB,SAAS,CAAC,EAAE,MAAM;AAEtE,sBAAsB,iBAAiB,GAAG,EAAE,OAAO,kBAAkB,CAAC;;;;;;;AAQtE,MAAa,oBAAoB,QAC/B,2BAA2B;CACzB,YAAY,SACV,IAAI,QAAQ,OAAO,IAAI,KACvB,IAAI,QAAQ,YAAY,MAAM,EAAE,SAAS,MAAM,CAAC;CAClD,YAAY,SAAiB,IAAI,QAAQ,OAAO,IAAI;AACtD,CAAC;;;;AAKH,MAAa,qBACV,SAEC,SACA,WAEA,OAAO,YAAY,WACd,UACD,eACE,SACA,UAAW,IAAyC,QACpD,qBAAqB,aACvB;;;;;;;;;;;;;;;;;;;;;;;AAwBR,MAAa,KACX,SACA,WACY;CACZ,MAAM,UAAU,gBAAgB,SAAS;CAEzC,IAAI,CAAC,SAAS;EACZ,MACE,qJACF;EAEA,OAAO,eACL,SACA,UAAU,qBAAqB,aACjC;CACF;CAEA,OAAO,QAAQ,EAAE,SAAwC,MAAM;AACjE;AAEA,MAAa,eAA2C,GAAG,SAAS;CAClE,MAAM,UAAU,gBAAgB,SAAS;CAEzC,IAAI,CAAC,SAAS;EACZ,MACE,+JACF;EAEA,OAAOA,cAAoB,GAAG,IAAI;CACpC;CAEA,OAAO,QAAQ,YAAY,GAAG,IAAI;AACpC;AAEA,MAAa,iBAA+C,GAAG,SAAS;CACtE,MAAM,UAAU,gBAAgB,SAAS;CAEzC,IAAI,CAAC,SAAS;EACZ,MACE,iKACF;EAEA,OAAOC,gBAAsB,GAAG,IAAI;CACtC;CAEA,OAAO,QAAQ,cAAc,GAAG,IAAI;AACtC;;;;AAKA,MAAa,aAAa,WACxB,gBAAgB,SAAS,CAAC,EAAE,UAC5B,UACA,qBAAqB"}
@@ -1,37 +1,29 @@
1
- import { appNamespace, getStorageLocale, translateFunction } from "./index.mjs";
2
- import { getConfiguration } from "@intlayer/config/node";
1
+ import { getStorageLocale, intlayerStorage, translateFunction } from "./index.mjs";
2
+ import { internationalization } from "@intlayer/config/built";
3
3
  import { getDictionary, getIntlayer } from "@intlayer/core/interpreter";
4
4
  import { localeDetector } from "@intlayer/core/localization";
5
5
 
6
6
  //#region src/middleware.ts
7
- const { internationalization } = getConfiguration();
8
7
  /**
9
8
  * AdonisJS middleware that detects the user's locale and populates the context with Intlayer data.
10
9
  */
11
10
  var IntlayerMiddleware = class {
12
11
  async handle(ctx, next) {
13
12
  const localeFromStorage = getStorageLocale(ctx);
14
- const negotiatorHeaders = {};
15
- const headers = ctx.request.headers();
16
- for (const key in headers) {
17
- const val = headers[key];
18
- if (typeof val === "string") negotiatorHeaders[key] = val;
19
- else if (Array.isArray(val)) negotiatorHeaders[key] = val.join(",");
20
- }
21
- const localeDetected = localeDetector(negotiatorHeaders, internationalization.locales, internationalization.defaultLocale);
13
+ const localeDetected = localeDetector({ "accept-language": ctx.request.header("accept-language") }, internationalization.locales, internationalization.defaultLocale);
22
14
  const locale = localeFromStorage ?? localeDetected;
23
- ctx.locale = locale;
24
- ctx.defaultLocale = internationalization.defaultLocale;
15
+ const decoratedContext = ctx;
16
+ decoratedContext.locale = locale;
17
+ decoratedContext.defaultLocale = internationalization.defaultLocale;
25
18
  const t = translateFunction(ctx);
26
19
  const getIntlayer$1 = (key, localeArg, ...props) => getIntlayer(key, localeArg ?? locale, ...props);
27
20
  const getDictionary$1 = (key, localeArg, ...props) => getDictionary(key, localeArg ?? locale, ...props);
28
- await appNamespace.runPromise(async () => {
29
- appNamespace.set("locale", locale);
30
- appNamespace.set("t", t);
31
- appNamespace.set("getIntlayer", getIntlayer$1);
32
- appNamespace.set("getDictionary", getDictionary$1);
33
- await next();
34
- });
21
+ await intlayerStorage.run({
22
+ locale,
23
+ t,
24
+ getIntlayer: getIntlayer$1,
25
+ getDictionary: getDictionary$1
26
+ }, next);
35
27
  }
36
28
  };
37
29
 
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.mjs","names":["getIntlayer","getIntlayerFunction","getDictionary","getDictionaryFunction"],"sources":["../../src/middleware.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport type { NextFn } from '@adonisjs/core/types/http';\nimport { getConfiguration } from '@intlayer/config/node';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n} from '@intlayer/core/interpreter';\nimport { localeDetector } from '@intlayer/core/localization';\nimport { appNamespace, getStorageLocale, translateFunction } from './index';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * AdonisJS middleware that detects the user's locale and populates the context with Intlayer data.\n */\nexport default class IntlayerMiddleware {\n async handle(ctx: HttpContext, next: NextFn) {\n // Detect if locale is set by intlayer frontend lib in the headers or cookies\n const localeFromStorage = getStorageLocale(ctx);\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // Copy all headers from the request to negotiatorHeaders\n const headers = ctx.request.headers();\n for (const key in headers) {\n const val = headers[key];\n if (typeof val === 'string') {\n negotiatorHeaders[key] = val;\n } else if (Array.isArray(val)) {\n negotiatorHeaders[key] = val.join(',');\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n\n // Decorate context\n (ctx as any).locale = locale;\n (ctx as any).defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(ctx);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg,\n ...props\n ) =>\n getIntlayerFunction(\n key,\n (localeArg ?? locale) as typeof localeArg,\n ...props\n );\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg,\n ...props\n ) =>\n getDictionaryFunction(\n key,\n (localeArg ?? locale) as typeof localeArg,\n ...props\n );\n\n // Make functions available via CLS\n await appNamespace.runPromise(async () => {\n appNamespace.set('locale', locale);\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n await next();\n });\n }\n}\n"],"mappings":";;;;;;AAWA,MAAM,EAAE,yBADc,iBACuB;;;;AAK7C,IAAqB,qBAArB,MAAwC;CACtC,MAAM,OAAO,KAAkB,MAAc;EAE3C,MAAM,oBAAoB,iBAAiB,GAAG;EAE9C,MAAM,oBAA4C,CAAC;EAGnD,MAAM,UAAU,IAAI,QAAQ,QAAQ;EACpC,KAAK,MAAM,OAAO,SAAS;GACzB,MAAM,MAAM,QAAQ;GACpB,IAAI,OAAO,QAAQ,UACjB,kBAAkB,OAAO;QACpB,IAAI,MAAM,QAAQ,GAAG,GAC1B,kBAAkB,OAAO,IAAI,KAAK,GAAG;EAEzC;EAEA,MAAM,iBAAiB,eACrB,mBACA,qBAAqB,SACrB,qBAAqB,aACvB;EAEA,MAAM,SAAS,qBAAqB;EAGpC,AAAC,IAAY,SAAS;EACtB,AAAC,IAAY,gBAAgB,qBAAqB;EAElD,MAAM,IAAI,kBAAkB,GAAG;EAE/B,MAAMA,iBACJ,KACA,WACA,GAAG,UAEHC,YACE,KACC,aAAa,QACd,GAAG,KACL;EAEF,MAAMC,mBACJ,KACA,WACA,GAAG,UAEHC,cACE,KACC,aAAa,QACd,GAAG,KACL;EAGF,MAAM,aAAa,WAAW,YAAY;GACxC,aAAa,IAAI,UAAU,MAAM;GACjC,aAAa,IAAI,KAAK,CAAC;GACvB,aAAa,IAAI,eAAeH,aAAW;GAC3C,aAAa,IAAI,iBAAiBE,eAAa;GAE/C,MAAM,KAAK;EACb,CAAC;CACH;AACF"}
1
+ {"version":3,"file":"middleware.mjs","names":["getIntlayer","getIntlayerFunction","getDictionary","getDictionaryFunction"],"sources":["../../src/middleware.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport type { NextFn } from '@adonisjs/core/types/http';\nimport { internationalization } from '@intlayer/config/built';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n} from '@intlayer/core/interpreter';\nimport { localeDetector } from '@intlayer/core/localization';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport { getStorageLocale, intlayerStorage, translateFunction } from './index';\n\n/**\n * AdonisJS middleware that detects the user's locale and populates the context with Intlayer data.\n */\nexport default class IntlayerMiddleware {\n async handle(ctx: HttpContext, next: NextFn) {\n // Detect if locale is set by intlayer frontend lib in the headers or cookies\n const localeFromStorage = getStorageLocale(ctx);\n\n const localeDetected = localeDetector(\n { 'accept-language': ctx.request.header('accept-language') },\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n\n // Decorate context\n const decoratedContext = ctx as HttpContext & {\n locale: Locale;\n defaultLocale: Locale;\n };\n decoratedContext.locale = locale;\n decoratedContext.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(ctx);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg,\n ...props\n ) =>\n getIntlayerFunction(\n key,\n (localeArg ?? locale) as typeof localeArg,\n ...props\n );\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg,\n ...props\n ) =>\n getDictionaryFunction(\n key,\n (localeArg ?? locale) as typeof localeArg,\n ...props\n );\n\n // Make functions available to the standalone exports for this request\n await intlayerStorage.run({ locale, t, getIntlayer, getDictionary }, next);\n }\n}\n"],"mappings":";;;;;;;;;AAcA,IAAqB,qBAArB,MAAwC;CACtC,MAAM,OAAO,KAAkB,MAAc;EAE3C,MAAM,oBAAoB,iBAAiB,GAAG;EAE9C,MAAM,iBAAiB,eACrB,EAAE,mBAAmB,IAAI,QAAQ,OAAO,iBAAiB,EAAE,GAC3D,qBAAqB,SACrB,qBAAqB,aACvB;EAEA,MAAM,SAAS,qBAAqB;EAGpC,MAAM,mBAAmB;EAIzB,iBAAiB,SAAS;EAC1B,iBAAiB,gBAAgB,qBAAqB;EAEtD,MAAM,IAAI,kBAAkB,GAAG;EAE/B,MAAMA,iBACJ,KACA,WACA,GAAG,UAEHC,YACE,KACC,aAAa,QACd,GAAG,KACL;EAEF,MAAMC,mBACJ,KACA,WACA,GAAG,UAEHC,cACE,KACC,aAAa,QACd,GAAG,KACL;EAGF,MAAM,gBAAgB,IAAI;GAAE;GAAQ;GAAG;GAAa;EAAc,GAAG,IAAI;CAC3E;AACF"}
@@ -1,16 +1,36 @@
1
1
  import IntlayerMiddleware from "./middleware.js";
2
+ import { AsyncLocalStorage } from "node:async_hooks";
2
3
  import { HttpContext } from "@adonisjs/core/http";
3
4
  import { getDictionary as getDictionary$1, getIntlayer as getIntlayer$1 } from "@intlayer/core/interpreter";
4
5
  import { Locale } from "@intlayer/types/allLocales";
5
6
  import { StrictModeLocaleMap } from "@intlayer/types/module_augmentation";
6
7
  //#region src/index.d.ts
7
- export declare const appNamespace: import("cls-hooked").Namespace<Record<string, any>>;
8
+ /**
9
+ * Intlayer helpers bound to the request being handled.
10
+ */
11
+ export type IntlayerRequestContext = {
12
+ locale: Locale;
13
+ t: ReturnType<typeof translateFunction>;
14
+ getIntlayer: typeof getIntlayer$1;
15
+ getDictionary: typeof getDictionary$1;
16
+ };
17
+ /**
18
+ * Request-scoped Intlayer context, entered by `IntlayerMiddleware`.
19
+ *
20
+ * `AsyncLocalStorage` rather than `cls-hooked`: the latter relies on
21
+ * `async_hooks.createHook`, which Bun does not implement, and is several times
22
+ * slower on Node.
23
+ */
24
+ export declare const intlayerStorage: AsyncLocalStorage<IntlayerRequestContext>;
8
25
  /**
9
26
  * Retrieves the locale from storage (cookies, headers).
27
+ *
28
+ * `request.cookie` only reads signed cookies: the locale cookie set by the
29
+ * Intlayer client libraries is a plain one.
10
30
  */
11
31
  export declare const getStorageLocale: (ctx: HttpContext) => Locale | undefined;
12
32
  /**
13
- * Translation function for context
33
+ * Builds the translation function bound to the locale stored on the HTTP context.
14
34
  */
15
35
  export declare const translateFunction: (ctx: HttpContext) => <T extends string>(content: StrictModeLocaleMap<T> | string, locale?: Locale) => T;
16
36
  /**
@@ -38,6 +58,9 @@ export declare const translateFunction: (ctx: HttpContext) => <T extends string>
38
58
  export declare const t: <Content = string>(content: StrictModeLocaleMap<Content>, locale?: Locale) => Content;
39
59
  export declare const getIntlayer: typeof getIntlayer$1;
40
60
  export declare const getDictionary: typeof getDictionary$1;
61
+ /**
62
+ * Returns the locale of the request being handled, else `locale`, else the default locale.
63
+ */
41
64
  export declare const getLocale: (locale?: Locale) => Locale;
42
65
  //#endregion
43
66
  export { IntlayerMiddleware };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;qBA4Ba,mCAAY,UAAA;;;;qBAOZ,mBAAgB,KAAS,gBAAc;;;;qBASvC,oBAAiB,KACtB,iBACL,kBAAgB,SACN,oBAAoB,aAAW,SAC/B,WACR;;;;;;;;;;;;;;;;;;;;;;;qBAyDQ,IAAK,kBAAgB,SACvB,oBAAoB,UAAQ,SAC5B,WACR;qBAyBU,oBAAoB;qBA4BpB,sBAAsB;qBA4BtB,YAAS,SAAa,WAAS"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/index.ts"],"mappings":";;;;;;;;;;YA8BY;EACV,QAAQ;EACR,GAAG,kBAAkB;EACrB,oBAAoB;EACpB,sBAAsB;;;;;;;;;qBAUX,iBAAe,kBAAA;;;;;;;qBAcf,mBAAgB,KAAS,gBAAc;;;;qBAWvC,oBAAiB,KACtB,iBACL,kBAAgB,SACN,oBAAoB,aAAW,SAC/B,WACR;;;;;;;;;;;;;;;;;;;;;;;qBA+BQ,IAAK,kBAAgB,SACvB,oBAAoB,UAAQ,SAC5B,WACR;qBAiBU,oBAAoB;qBAcpB,sBAAsB;;;;qBAiBtB,YAAS,SAAa,WAAS"}
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.d.ts","names":[],"sources":["../../src/middleware.ts"],"mappings":";;;;;;qBAgBqB;EACb,OAAO,KAAK,aAAa,MAAM,SAAM"}
1
+ {"version":3,"file":"middleware.d.ts","names":[],"sources":["../../src/middleware.ts"],"mappings":";;;;;;qBAcqB;EACb,OAAO,KAAK,aAAa,MAAM,SAAM"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adonis-intlayer",
3
- "version": "9.5.11",
3
+ "version": "9.6.0",
4
4
  "private": false,
5
5
  "description": "Manage internationalization i18n in a simple way for AdonisJS application.",
6
6
  "keywords": [
@@ -81,14 +81,12 @@
81
81
  "typecheck": "tsc --noEmit --project tsconfig.types.json"
82
82
  },
83
83
  "dependencies": {
84
- "@intlayer/config": "9.5.11",
85
- "@intlayer/core": "9.5.11",
86
- "@intlayer/engine": "9.5.11",
87
- "@intlayer/types": "9.5.11",
88
- "cls-hooked": "4.2.2"
84
+ "@intlayer/config": "9.6.0",
85
+ "@intlayer/core": "9.6.0",
86
+ "@intlayer/engine": "9.6.0",
87
+ "@intlayer/types": "9.6.0"
89
88
  },
90
89
  "devDependencies": {
91
- "@types/cls-hooked": "4.3.9",
92
90
  "@types/node": "^22",
93
91
  "@utils/ts-config": "1.0.4",
94
92
  "@utils/ts-config-types": "1.0.4",
@@ -96,7 +94,7 @@
96
94
  "rimraf": "6.1.3",
97
95
  "tsdown": "0.23.0",
98
96
  "typescript": "7.0.2",
99
- "vitest": "5.0.2"
97
+ "vitest": "5.0.3"
100
98
  },
101
99
  "peerDependencies": {
102
100
  "@adonisjs/core": ">=6.0.0"