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.
- package/dist/cjs/index.cjs +42 -54
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/middleware.cjs +11 -19
- package/dist/cjs/middleware.cjs.map +1 -1
- package/dist/esm/index.mjs +43 -55
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/middleware.mjs +12 -20
- package/dist/esm/middleware.mjs.map +1 -1
- package/dist/types/index.d.ts +25 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/middleware.d.ts.map +1 -1
- package/package.json +6 -8
package/dist/cjs/index.cjs
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
20
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return
|
|
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 = (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return
|
|
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
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["
|
|
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"}
|
package/dist/cjs/middleware.cjs
CHANGED
|
@@ -1,37 +1,29 @@
|
|
|
1
1
|
const require_index = require('./index.cjs');
|
|
2
|
-
let
|
|
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
|
|
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
|
-
|
|
24
|
-
|
|
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.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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":["
|
|
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"}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -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 {
|
|
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
|
-
|
|
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
|
-
|
|
20
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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 = (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return
|
|
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,
|
|
94
|
+
export { IntlayerMiddleware, getDictionary, getIntlayer, getLocale, getStorageLocale, intlayerStorage, t, translateFunction };
|
|
107
95
|
//# sourceMappingURL=index.mjs.map
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -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 {
|
|
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"}
|
package/dist/esm/middleware.mjs
CHANGED
|
@@ -1,37 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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
|
|
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
|
-
|
|
24
|
-
|
|
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
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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 {
|
|
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"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
*
|
|
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":"
|
|
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":";;;;;;
|
|
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.
|
|
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.
|
|
85
|
-
"@intlayer/core": "9.
|
|
86
|
-
"@intlayer/engine": "9.
|
|
87
|
-
"@intlayer/types": "9.
|
|
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.
|
|
97
|
+
"vitest": "5.0.3"
|
|
100
98
|
},
|
|
101
99
|
"peerDependencies": {
|
|
102
100
|
"@adonisjs/core": ">=6.0.0"
|