express-intlayer 7.5.7 → 7.5.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-intlayer",
3
- "version": "7.5.7",
3
+ "version": "7.5.8",
4
4
  "private": false,
5
5
  "description": "Manage internationalization i18n in a simple way for express application.",
6
6
  "keywords": [
@@ -76,12 +76,12 @@
76
76
  "typecheck": "tsc --noEmit --project tsconfig.types.json"
77
77
  },
78
78
  "dependencies": {
79
- "@intlayer/chokidar": "7.5.7",
80
- "@intlayer/config": "7.5.7",
81
- "@intlayer/core": "7.5.7",
82
- "@intlayer/types": "7.5.7",
79
+ "@intlayer/chokidar": "7.5.8",
80
+ "@intlayer/config": "7.5.8",
81
+ "@intlayer/core": "7.5.8",
82
+ "@intlayer/types": "7.5.8",
83
83
  "cls-hooked": "4.2.2",
84
- "intlayer": "7.5.7"
84
+ "intlayer": "7.5.8"
85
85
  },
86
86
  "devDependencies": {
87
87
  "@types/cls-hooked": "4.3.9",
@@ -1,93 +0,0 @@
1
- let _intlayer_chokidar = require("@intlayer/chokidar");
2
- let _intlayer_config = require("@intlayer/config");
3
- let _intlayer_core = require("@intlayer/core");
4
- let cls_hooked = require("cls-hooked");
5
-
6
- //#region src/fastify.ts
7
- const configuration = (0, _intlayer_config.getConfiguration)();
8
- const { internationalization } = configuration;
9
- /**
10
- * Retrieves the locale from storage (cookies, localStorage, sessionStorage).
11
- */
12
- const getStorageLocale = (req) => (0, _intlayer_core.getLocaleFromStorage)({
13
- getCookie: (name) => req.cookies?.[name],
14
- getHeader: (name) => req.headers[name]
15
- });
16
- const appNamespace = (0, cls_hooked.createNamespace)("app");
17
- (0, _intlayer_chokidar.prepareIntlayer)(configuration);
18
- const translateFunction = (req, reply) => (content, locale) => {
19
- const { locale: currentLocale, defaultLocale } = reply.locals;
20
- const targetLocale = locale ?? currentLocale;
21
- if (typeof content === "undefined") return "";
22
- if (typeof content === "string") return content;
23
- if (typeof content?.[targetLocale] === "undefined") if (typeof content?.[defaultLocale] === "undefined") return content;
24
- else return (0, _intlayer_core.getTranslation)(content, defaultLocale);
25
- return (0, _intlayer_core.getTranslation)(content, targetLocale);
26
- };
27
- /**
28
- * Fastify plugin to detect locale used by the user and load it into reply locals
29
- */
30
- const intlayerPlugin = async (fastify) => {
31
- fastify.addHook("onRequest", async (request, reply) => {
32
- if (!reply.locals) reply.locals = {};
33
- const localeFromStorage = getStorageLocale(request);
34
- const negotiatorHeaders = {};
35
- if (request && typeof request.headers === "object") {
36
- for (const key in request.headers) if (typeof request.headers[key] === "string") negotiatorHeaders[key] = request.headers[key];
37
- }
38
- const localeDetected = (0, _intlayer_core.localeDetector)(negotiatorHeaders, internationalization.locales, internationalization.defaultLocale);
39
- reply.locals.locale_storage = localeFromStorage;
40
- reply.locals.locale_detected = localeDetected;
41
- reply.locals.locale = localeFromStorage ?? localeDetected;
42
- reply.locals.defaultLocale = internationalization.defaultLocale;
43
- const t$1 = translateFunction(request, reply);
44
- const getIntlayer$1 = (key, localeArg = localeDetected, ...props) => (0, _intlayer_core.getIntlayer)(key, localeArg, ...props);
45
- const getDictionary$1 = (key, localeArg = localeDetected, ...props) => (0, _intlayer_core.getDictionary)(key, localeArg, ...props);
46
- reply.locals.t = t$1;
47
- reply.locals.getIntlayer = getIntlayer$1;
48
- reply.locals.getDictionary = getDictionary$1;
49
- appNamespace.run(() => {
50
- appNamespace.set("t", t$1);
51
- appNamespace.set("getIntlayer", getIntlayer$1);
52
- appNamespace.set("getDictionary", getDictionary$1);
53
- });
54
- });
55
- };
56
- const t = (content, locale) => {
57
- try {
58
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `fastify.register(intlayerPlugin);` plugin before using this function.");
59
- if (typeof appNamespace.get("t") !== "function") throw new Error("Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the reply.locals.t syntax instead.");
60
- return appNamespace.get("t")(content, locale);
61
- } catch (error) {
62
- if (process.env.NODE_ENV === "development") console.error(error.message);
63
- return (0, _intlayer_core.getTranslation)(content, locale ?? internationalization.defaultLocale);
64
- }
65
- };
66
- const getIntlayer = (...args) => {
67
- try {
68
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `fastify.register(intlayerPlugin);` plugin before using this function.");
69
- if (typeof appNamespace.get("getIntlayer") !== "function") throw new Error("Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the reply.locals.t syntax instead.");
70
- return appNamespace.get("getIntlayer")(...args);
71
- } catch (error) {
72
- if (process.env.NODE_ENV === "development") console.error(error.message);
73
- return (0, _intlayer_core.getIntlayer)(...args);
74
- }
75
- };
76
- const getDictionary = (...args) => {
77
- try {
78
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `fastify.register(intlayerPlugin);` plugin before using this function.");
79
- if (typeof appNamespace.get("getDictionary") !== "function") throw new Error("Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the reply.locals.t syntax instead.");
80
- return appNamespace.get("getDictionary")(...args);
81
- } catch (error) {
82
- if (process.env.NODE_ENV === "development") console.error(error.message);
83
- return (0, _intlayer_core.getDictionary)(...args);
84
- }
85
- };
86
-
87
- //#endregion
88
- exports.getDictionary = getDictionary;
89
- exports.getIntlayer = getIntlayer;
90
- exports.intlayerPlugin = intlayerPlugin;
91
- exports.t = t;
92
- exports.translateFunction = translateFunction;
93
- //# sourceMappingURL=fastify.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fastify.cjs","names":["negotiatorHeaders: Record<string, string>","t","getIntlayer: typeof getIntlayerFunction","getDictionary: typeof getDictionaryFunction","getIntlayer","getDictionary"],"sources":["../../src/fastify.ts"],"sourcesContent":["import { prepareIntlayer } from '@intlayer/chokidar';\nimport { getConfiguration } from '@intlayer/config';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getLocaleFromStorage,\n getTranslation,\n localeDetector,\n} from '@intlayer/core';\nimport type { Locale, StrictModeLocaleMap } from '@intlayer/types';\nimport { createNamespace } from 'cls-hooked';\nimport type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * Retrieves the locale from storage (cookies, localStorage, sessionStorage).\n */\nconst getStorageLocale = (req: FastifyRequest): Locale | undefined =>\n getLocaleFromStorage({\n getCookie: (name: string) => req.cookies?.[name],\n getHeader: (name: string) => req.headers[name] as string | undefined,\n });\n\nconst appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\nexport const translateFunction =\n (req: FastifyRequest, reply: FastifyReply) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n const { locale: currentLocale, defaultLocale } = (reply as any).locals 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 * Fastify plugin to detect locale used by the user and load it into reply locals\n */\nexport const intlayerPlugin = async (fastify: FastifyInstance) => {\n fastify.addHook('onRequest', async (request: FastifyRequest, reply: FastifyReply) => {\n // Initialize reply.locals if it doesn't exist\n if (!(reply as any).locals) {\n (reply as any).locals = {};\n }\n\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeFromStorage = getStorageLocale(request);\n // Interpret browser locale\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // Check if request.headers exists and is an object\n if (request && typeof request.headers === 'object') {\n // Copy all headers from the request to negotiatorHeaders\n for (const key in request.headers) {\n if (typeof request.headers[key] === 'string') {\n negotiatorHeaders[key] = request.headers[key] as string;\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n (reply as any).locals.locale_storage = localeFromStorage;\n (reply as any).locals.locale_detected = localeDetected;\n (reply as any).locals.locale = localeFromStorage ?? localeDetected;\n (reply as any).locals.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(request, reply);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n (reply as any).locals.t = t;\n (reply as any).locals.getIntlayer = getIntlayer;\n (reply as any).locals.getDictionary = getDictionary;\n\n appNamespace.run(() => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n });\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 `fastify.register(intlayerPlugin);` plugin before using this function.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the reply.locals.t syntax instead.'\n );\n }\n\n return appNamespace.get('t')(content, locale);\n } catch (error) {\n if (process.env.NODE_ENV === 'development') {\n console.error((error as Error).message);\n }\n\n return getTranslation(\n content,\n locale ?? internationalization.defaultLocale\n );\n }\n};\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `fastify.register(intlayerPlugin);` plugin before using this function.'\n );\n }\n\n if (typeof appNamespace.get('getIntlayer') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the reply.locals.t syntax instead.'\n );\n }\n\n return appNamespace.get('getIntlayer')(...args);\n } catch (error) {\n if (process.env.NODE_ENV === 'development') {\n console.error((error as Error).message);\n }\n return getIntlayerFunction(...args);\n }\n};\n\nexport const getDictionary: typeof getDictionaryFunction = (...args) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `fastify.register(intlayerPlugin);` plugin before using this function.'\n );\n }\n\n if (typeof appNamespace.get('getDictionary') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the reply.locals.t syntax instead.'\n );\n }\n\n return appNamespace.get('getDictionary')(...args);\n } catch (error) {\n if (process.env.NODE_ENV === 'development') {\n console.error((error as Error).message);\n }\n return getDictionaryFunction(...args);\n }\n};\n\n"],"mappings":";;;;;;AAaA,MAAM,wDAAkC;AACxC,MAAM,EAAE,yBAAyB;;;;AAKjC,MAAM,oBAAoB,iDACH;CACnB,YAAY,SAAiB,IAAI,UAAU;CAC3C,YAAY,SAAiB,IAAI,QAAQ;CAC1C,CAAC;AAEJ,MAAM,+CAA+B,MAAM;wCAE3B,cAAc;AAE9B,MAAa,qBACV,KAAqB,WAEpB,SACA,WACM;CACN,MAAM,EAAE,QAAQ,eAAe,kBAAmB,MAAc;CAKhE,MAAM,eAAe,UAAU;AAE/B,KAAI,OAAO,YAAY,YACrB,QAAO;AAGT,KAAI,OAAO,YAAY,SACrB,QAAO;AAGT,KACE,OAAO,UACL,kBACI,YAEN,KACE,OAAO,UACL,mBACI,YAEN,QAAO;KAEP,2CAAsB,SAAS,cAAc;AAIjD,2CAAsB,SAAS,aAAa;;;;;AAMhD,MAAa,iBAAiB,OAAO,YAA6B;AAChE,SAAQ,QAAQ,aAAa,OAAO,SAAyB,UAAwB;AAEnF,MAAI,CAAE,MAAc,OAClB,CAAC,MAAc,SAAS,E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O,iBAAiB,YAC1B,OAAM,IAAI,MACR,8GACD;AAGH,MAAI,OAAO,aAAa,IAAI,cAAc,KAAK,WAC7C,OAAM,IAAI,MACR,gIACD;AAGH,SAAO,aAAa,IAAI,cAAc,CAAC,GAAG,KAAK;UACxC,OAAO;AACd,MAAI,QAAQ,IAAI,aAAa,cAC3B,SAAQ,MAAO,MAAgB,QAAQ;AAEzC,yCAA2B,GAAG,KAAK;;;AAIvC,MAAaC,iBAA+C,GAAG,SAAS;AACtE,KAAI;AACF,MAAI,OAAO,iBAAiB,YAC1B,OAAM,IAAI,MACR,8GACD;AAGH,MAAI,OAAO,aAAa,IAAI,gBAAgB,KAAK,WAC/C,OAAM,IAAI,MACR,gIACD;AAGH,SAAO,aAAa,IAAI,gBAAgB,CAAC,GAAG,KAAK;UAC1C,OAAO;AACd,MAAI,QAAQ,IAAI,aAAa,cAC3B,SAAQ,MAAO,MAAgB,QAAQ;AAEzC,2CAA6B,GAAG,KAAK"}
@@ -1,89 +0,0 @@
1
- import { prepareIntlayer } from "@intlayer/chokidar";
2
- import { getConfiguration } from "@intlayer/config";
3
- import { getDictionary as getDictionary$1, getIntlayer as getIntlayer$1, getLocaleFromStorage, getTranslation, localeDetector } from "@intlayer/core";
4
- import { createNamespace } from "cls-hooked";
5
-
6
- //#region src/fastify.ts
7
- const configuration = getConfiguration();
8
- const { internationalization } = configuration;
9
- /**
10
- * Retrieves the locale from storage (cookies, localStorage, sessionStorage).
11
- */
12
- const getStorageLocale = (req) => getLocaleFromStorage({
13
- getCookie: (name) => req.cookies?.[name],
14
- getHeader: (name) => req.headers[name]
15
- });
16
- const appNamespace = createNamespace("app");
17
- prepareIntlayer(configuration);
18
- const translateFunction = (req, reply) => (content, locale) => {
19
- const { locale: currentLocale, defaultLocale } = reply.locals;
20
- const targetLocale = locale ?? currentLocale;
21
- if (typeof content === "undefined") return "";
22
- if (typeof content === "string") return content;
23
- if (typeof content?.[targetLocale] === "undefined") if (typeof content?.[defaultLocale] === "undefined") return content;
24
- else return getTranslation(content, defaultLocale);
25
- return getTranslation(content, targetLocale);
26
- };
27
- /**
28
- * Fastify plugin to detect locale used by the user and load it into reply locals
29
- */
30
- const intlayerPlugin = async (fastify) => {
31
- fastify.addHook("onRequest", async (request, reply) => {
32
- if (!reply.locals) reply.locals = {};
33
- const localeFromStorage = getStorageLocale(request);
34
- const negotiatorHeaders = {};
35
- if (request && typeof request.headers === "object") {
36
- for (const key in request.headers) if (typeof request.headers[key] === "string") negotiatorHeaders[key] = request.headers[key];
37
- }
38
- const localeDetected = localeDetector(negotiatorHeaders, internationalization.locales, internationalization.defaultLocale);
39
- reply.locals.locale_storage = localeFromStorage;
40
- reply.locals.locale_detected = localeDetected;
41
- reply.locals.locale = localeFromStorage ?? localeDetected;
42
- reply.locals.defaultLocale = internationalization.defaultLocale;
43
- const t$1 = translateFunction(request, reply);
44
- const getIntlayer$2 = (key, localeArg = localeDetected, ...props) => getIntlayer$1(key, localeArg, ...props);
45
- const getDictionary$2 = (key, localeArg = localeDetected, ...props) => getDictionary$1(key, localeArg, ...props);
46
- reply.locals.t = t$1;
47
- reply.locals.getIntlayer = getIntlayer$2;
48
- reply.locals.getDictionary = getDictionary$2;
49
- appNamespace.run(() => {
50
- appNamespace.set("t", t$1);
51
- appNamespace.set("getIntlayer", getIntlayer$2);
52
- appNamespace.set("getDictionary", getDictionary$2);
53
- });
54
- });
55
- };
56
- const t = (content, locale) => {
57
- try {
58
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `fastify.register(intlayerPlugin);` plugin before using this function.");
59
- if (typeof appNamespace.get("t") !== "function") throw new Error("Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the reply.locals.t syntax instead.");
60
- return appNamespace.get("t")(content, locale);
61
- } catch (error) {
62
- console.error(error.message);
63
- return getTranslation(content, locale ?? internationalization.defaultLocale);
64
- }
65
- };
66
- const getIntlayer = (...args) => {
67
- try {
68
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `fastify.register(intlayerPlugin);` plugin before using this function.");
69
- if (typeof appNamespace.get("getIntlayer") !== "function") throw new Error("Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the reply.locals.t syntax instead.");
70
- return appNamespace.get("getIntlayer")(...args);
71
- } catch (error) {
72
- console.error(error.message);
73
- return getIntlayer$1(...args);
74
- }
75
- };
76
- const getDictionary = (...args) => {
77
- try {
78
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `fastify.register(intlayerPlugin);` plugin before using this function.");
79
- if (typeof appNamespace.get("getDictionary") !== "function") throw new Error("Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the reply.locals.t syntax instead.");
80
- return appNamespace.get("getDictionary")(...args);
81
- } catch (error) {
82
- console.error(error.message);
83
- return getDictionary$1(...args);
84
- }
85
- };
86
-
87
- //#endregion
88
- export { getDictionary, getIntlayer, intlayerPlugin, t, translateFunction };
89
- //# sourceMappingURL=fastify.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fastify.mjs","names":["negotiatorHeaders: Record<string, string>","t","getIntlayer: typeof getIntlayerFunction","getIntlayerFunction","getDictionary: typeof getDictionaryFunction","getDictionaryFunction","getIntlayer","getDictionary"],"sources":["../../src/fastify.ts"],"sourcesContent":["import { prepareIntlayer } from '@intlayer/chokidar';\nimport { getConfiguration } from '@intlayer/config';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getLocaleFromStorage,\n getTranslation,\n localeDetector,\n} from '@intlayer/core';\nimport type { Locale, StrictModeLocaleMap } from '@intlayer/types';\nimport { createNamespace } from 'cls-hooked';\nimport type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * Retrieves the locale from storage (cookies, localStorage, sessionStorage).\n */\nconst getStorageLocale = (req: FastifyRequest): Locale | undefined =>\n getLocaleFromStorage({\n getCookie: (name: string) => req.cookies?.[name],\n getHeader: (name: string) => req.headers[name] as string | undefined,\n });\n\nconst appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\nexport const translateFunction =\n (req: FastifyRequest, reply: FastifyReply) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n const { locale: currentLocale, defaultLocale } = (reply as any).locals 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 * Fastify plugin to detect locale used by the user and load it into reply locals\n */\nexport const intlayerPlugin = async (fastify: FastifyInstance) => {\n fastify.addHook('onRequest', async (request: FastifyRequest, reply: FastifyReply) => {\n // Initialize reply.locals if it doesn't exist\n if (!(reply as any).locals) {\n (reply as any).locals = {};\n }\n\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeFromStorage = getStorageLocale(request);\n // Interpret browser locale\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // Check if request.headers exists and is an object\n if (request && typeof request.headers === 'object') {\n // Copy all headers from the request to negotiatorHeaders\n for (const key in request.headers) {\n if (typeof request.headers[key] === 'string') {\n negotiatorHeaders[key] = request.headers[key] as string;\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n (reply as any).locals.locale_storage = localeFromStorage;\n (reply as any).locals.locale_detected = localeDetected;\n (reply as any).locals.locale = localeFromStorage ?? localeDetected;\n (reply as any).locals.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(request, reply);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n (reply as any).locals.t = t;\n (reply as any).locals.getIntlayer = getIntlayer;\n (reply as any).locals.getDictionary = getDictionary;\n\n appNamespace.run(() => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n });\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 `fastify.register(intlayerPlugin);` plugin before using this function.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the reply.locals.t syntax instead.'\n );\n }\n\n return appNamespace.get('t')(content, locale);\n } catch (error) {\n if (process.env.NODE_ENV === 'development') {\n console.error((error as Error).message);\n }\n\n return getTranslation(\n content,\n locale ?? internationalization.defaultLocale\n );\n }\n};\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `fastify.register(intlayerPlugin);` plugin before using this function.'\n );\n }\n\n if (typeof appNamespace.get('getIntlayer') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the reply.locals.t syntax instead.'\n );\n }\n\n return appNamespace.get('getIntlayer')(...args);\n } catch (error) {\n if (process.env.NODE_ENV === 'development') {\n console.error((error as Error).message);\n }\n return getIntlayerFunction(...args);\n }\n};\n\nexport const getDictionary: typeof getDictionaryFunction = (...args) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `fastify.register(intlayerPlugin);` plugin before using this function.'\n );\n }\n\n if (typeof appNamespace.get('getDictionary') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"express-intlayer\" is not supported in your environment. Use the reply.locals.t syntax instead.'\n );\n }\n\n return appNamespace.get('getDictionary')(...args);\n } catch (error) {\n if (process.env.NODE_ENV === 'development') {\n console.error((error as Error).message);\n }\n return getDictionaryFunction(...args);\n }\n};\n\n"],"mappings":";;;;;;AAaA,MAAM,gBAAgB,kBAAkB;AACxC,MAAM,EAAE,yBAAyB;;;;AAKjC,MAAM,oBAAoB,QACxB,qBAAqB;CACnB,YAAY,SAAiB,IAAI,UAAU;CAC3C,YAAY,SAAiB,IAAI,QAAQ;CAC1C,CAAC;AAEJ,MAAM,eAAe,gBAAgB,MAAM;AAE3C,gBAAgB,cAAc;AAE9B,MAAa,qBACV,KAAqB,WAEpB,SACA,WACM;CACN,MAAM,EAAE,QAAQ,eAAe,kBAAmB,MAAc;CAKhE,MAAM,eAAe,UAAU;AAE/B,KAAI,OAAO,YAAY,YACrB,QAAO;AAGT,KAAI,OAAO,YAAY,SACrB,QAAO;AAGT,KACE,OAAO,UACL,kBACI,YAEN,KACE,OAAO,UACL,mBACI,YAEN,QAAO;KAEP,QAAO,eAAe,SAAS,cAAc;AAIjD,QAAO,eAAe,SAAS,aAAa;;;;;AAMhD,MAAa,iBAAiB,OAAO,YAA6B;AAChE,SAAQ,QAAQ,aAAa,OAAO,SAAyB,UAAwB;AAEnF,MAAI,CAAE,MAAc,OAClB,CAAC,MAAc,SAAS,EAAE;EAI5B,MAAM,oBAAoB,iBAAiB,QAAQ;EAGnD,MAAMA,oBAA4C,EAAE;AAGpD,MAAI,WAAW,OAAO,QAAQ,YAAY,UAExC;QAAK,MAAM,OAAO,QAAQ,QACxB,KAAI,OAAO,QAAQ,QAAQ,SAAS,SAClC,mBAAkB,OAAO,QAAQ,QAAQ;;EAK/C,MAAM,iBAAiB,eACrB,mBACA,qBAAqB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AA+C,GAAG,SAAS;AACtE,KAAI;AACF,MAAI,OAAO,iBAAiB,YAC1B,OAAM,IAAI,MACR,8GACD;AAGH,MAAI,OAAO,aAAa,IAAI,gBAAgB,KAAK,WAC/C,OAAM,IAAI,MACR,gIACD;AAGH,SAAO,aAAa,IAAI,gBAAgB,CAAC,GAAG,KAAK;UAC1C,OAAO;AAEZ,UAAQ,MAAO,MAAgB,QAAQ;AAEzC,SAAOC,gBAAsB,GAAG,KAAK"}
@@ -1,16 +0,0 @@
1
- import { getDictionary as getDictionary$1, getIntlayer as getIntlayer$1 } from "@intlayer/core";
2
- import { Locale, StrictModeLocaleMap } from "@intlayer/types";
3
- import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
4
-
5
- //#region src/fastify.d.ts
6
- declare const translateFunction: (req: FastifyRequest, reply: FastifyReply) => <T extends string>(content: StrictModeLocaleMap<T> | string, locale?: Locale) => T;
7
- /**
8
- * Fastify plugin to detect locale used by the user and load it into reply locals
9
- */
10
- declare const intlayerPlugin: (fastify: FastifyInstance) => Promise<void>;
11
- declare const t: <Content = string>(content: StrictModeLocaleMap<Content>, locale?: Locale) => Content;
12
- declare const getIntlayer: typeof getIntlayer$1;
13
- declare const getDictionary: typeof getDictionary$1;
14
- //#endregion
15
- export { getDictionary, getIntlayer, intlayerPlugin, t, translateFunction };
16
- //# sourceMappingURL=fastify.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fastify.d.ts","names":[],"sources":["../../src/fastify.ts"],"sourcesContent":[],"mappings":";;;;;cA6Ba,yBACL,uBAAuB,6CAElB,oBAAoB,sBACpB,WACR;;AALL;;AAC+B,cA0ClB,cA1CkB,EAAA,CAAA,OAAA,EA0Ce,eA1Cf,EAAA,GA0C8B,OA1C9B,CAAA,IAAA,CAAA;AAEE,cAoGpB,CApGoB,EAAA,CAAA,UAAA,MAAA,CAAA,CAAA,OAAA,EAqGtB,mBArGsB,CAqGF,OArGE,CAAA,EAAA,MAAA,CAAA,EAsGtB,MAtGsB,EAAA,GAuG9B,OAvG8B;AAApB,cAkIA,WAlIA,EAAA,OAkIoB,aAlIpB;AACA,cAwJA,aAxJA,EAAA,OAwJsB,eAxJtB"}