hono-intlayer 8.1.2 → 8.1.3

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,132 +1,2 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- let _intlayer_chokidar = require("@intlayer/chokidar");
3
- let _intlayer_config = require("@intlayer/config");
4
- let _intlayer_core = require("@intlayer/core");
5
- let cls_hooked = require("cls-hooked");
6
- let hono_cookie = require("hono/cookie");
7
-
8
- //#region src/index.ts
9
- const configuration = (0, _intlayer_config.getConfiguration)();
10
- const { internationalization } = configuration;
11
- /**
12
- * Retrieves the locale from storage (cookies, headers).
13
- */
14
- const getStorageLocale = (context) => (0, _intlayer_core.getLocaleFromStorage)({
15
- getCookie: (name) => (0, hono_cookie.getCookie)(context, name),
16
- getHeader: (name) => context.req.header(name)
17
- });
18
- const appNamespace = (0, cls_hooked.createNamespace)("app");
19
- (0, _intlayer_chokidar.prepareIntlayer)(configuration);
20
- const translateFunction = (context) => (content, locale) => {
21
- const currentLocale = context.get("locale");
22
- const defaultLocale = context.get("defaultLocale");
23
- const targetLocale = locale ?? currentLocale;
24
- if (typeof content === "undefined") return "";
25
- if (typeof content === "string") return content;
26
- if (typeof content?.[targetLocale] === "undefined") if (typeof content?.[defaultLocale] === "undefined") return content;
27
- else return (0, _intlayer_core.getTranslation)(content, defaultLocale);
28
- return (0, _intlayer_core.getTranslation)(content, targetLocale);
29
- };
30
- /**
31
- * Hono middleware that detects the user's locale and populates context with Intlayer data.
32
- *
33
- * It performs:
34
- * 1. Locale detection from cookies, headers, or default settings.
35
- * 2. Injects `t`, `getIntlayer`, and `getDictionary` functions into the context.
36
- * 3. Sets up a `cls-hooked` namespace for accessing these functions anywhere in the request lifecycle.
37
- *
38
- * @returns A Hono middleware function.
39
- *
40
- * @example
41
- * ```ts
42
- * import { Hono } from 'hono';
43
- * import { intlayer } from 'hono-intlayer';
44
- *
45
- * const app = new Hono();
46
- * app.use('*', intlayer());
47
- * ```
48
- */
49
- const intlayer = () => async (c, next) => {
50
- const localeFromStorage = getStorageLocale(c);
51
- const localeDetected = (0, _intlayer_core.localeDetector)(c.req.header(), internationalization.locales, internationalization.defaultLocale);
52
- const locale = localeFromStorage ?? localeDetected;
53
- c.set("locale_storage", localeFromStorage);
54
- c.set("locale_detected", localeDetected);
55
- c.set("locale", locale);
56
- c.set("defaultLocale", internationalization.defaultLocale);
57
- const t = translateFunction(c);
58
- const getIntlayer = (key, localeArg = locale, ...props) => (0, _intlayer_core.getIntlayer)(key, localeArg, ...props);
59
- const getDictionary = (key, localeArg = locale, ...props) => (0, _intlayer_core.getDictionary)(key, localeArg, ...props);
60
- c.set("t", t);
61
- c.set("getIntlayer", getIntlayer);
62
- c.set("getDictionary", getDictionary);
63
- return new Promise((resolve) => {
64
- appNamespace.run(async () => {
65
- appNamespace.set("t", t);
66
- appNamespace.set("getIntlayer", getIntlayer);
67
- appNamespace.set("getDictionary", getDictionary);
68
- await next();
69
- resolve();
70
- });
71
- });
72
- };
73
- /**
74
- * Translation function to retrieve content for the current locale.
75
- *
76
- * This function works within the request lifecycle managed by the `intlayer` middleware.
77
- *
78
- * @param content - A map of locales to content.
79
- * @param locale - Optional locale override.
80
- * @returns The translated content.
81
- *
82
- * @example
83
- * ```ts
84
- * import { t } from 'hono-intlayer';
85
- *
86
- * app.get('/', (c) => {
87
- * const greeting = t({
88
- * en: 'Hello',
89
- * fr: 'Bonjour',
90
- * });
91
- * return c.text(greeting);
92
- * });
93
- * ```
94
- */
95
- const t = (content, locale) => {
96
- try {
97
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.");
98
- if (typeof appNamespace.get("t") !== "function") throw new Error("Using the import { t } from \"hono-intlayer\" is not supported in your environment. Use the context instead.");
99
- return appNamespace.get("t")(content, locale);
100
- } catch (error) {
101
- if (process.env.NODE_ENV === "development") console.error(error.message);
102
- return (0, _intlayer_core.getTranslation)(content, locale ?? internationalization.defaultLocale);
103
- }
104
- };
105
- const getIntlayer = (...args) => {
106
- try {
107
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.");
108
- if (typeof appNamespace.get("getIntlayer") !== "function") throw new Error("Using the import { getIntlayer } from \"hono-intlayer\" is not supported in your environment. Use the context instead.");
109
- return appNamespace.get("getIntlayer")(...args);
110
- } catch (error) {
111
- if (process.env.NODE_ENV === "development") console.error(error.message);
112
- return (0, _intlayer_core.getIntlayer)(...args);
113
- }
114
- };
115
- const getDictionary = (...args) => {
116
- try {
117
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.");
118
- if (typeof appNamespace.get("getDictionary") !== "function") throw new Error("Using the import { getDictionary } from \"hono-intlayer\" is not supported in your environment. Use the context instead.");
119
- return appNamespace.get("getDictionary")(...args);
120
- } catch (error) {
121
- if (process.env.NODE_ENV === "development") console.error(error.message);
122
- return (0, _intlayer_core.getDictionary)(...args);
123
- }
124
- };
125
-
126
- //#endregion
127
- exports.getDictionary = getDictionary;
128
- exports.getIntlayer = getIntlayer;
129
- exports.intlayer = intlayer;
130
- exports.t = t;
131
- exports.translateFunction = translateFunction;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@intlayer/chokidar/build`),t=require(`@intlayer/config/node`),n=require(`@intlayer/core/interpreter`),r=require(`@intlayer/core/localization`),i=require(`@intlayer/core/utils`),a=require(`cls-hooked`),o=require(`hono/cookie`);const s=(0,t.getConfiguration)(),{internationalization:c}=s,l=e=>(0,i.getLocaleFromStorage)({getCookie:t=>(0,o.getCookie)(e,t),getHeader:t=>e.req.header(t)}),u=(0,a.createNamespace)(`app`);(0,e.prepareIntlayer)(s);const d=e=>(t,r)=>{let i=e.get(`locale`),a=e.get(`defaultLocale`),o=r??i;return t===void 0?``:typeof t==`string`?t:t?.[o]===void 0?t?.[a]===void 0?t:(0,n.getTranslation)(t,a):(0,n.getTranslation)(t,o)},f=()=>async(e,t)=>{let i=l(e),a=(0,r.localeDetector)(e.req.header(),c.locales,c.defaultLocale),o=i??a;e.set(`locale_storage`,i),e.set(`locale_detected`,a),e.set(`locale`,o),e.set(`defaultLocale`,c.defaultLocale);let s=d(e),f=(e,t=o,...r)=>(0,n.getIntlayer)(e,t,...r),p=(e,t=o,...r)=>(0,n.getDictionary)(e,t,...r);return e.set(`t`,s),e.set(`getIntlayer`,f),e.set(`getDictionary`,p),new Promise(e=>{u.run(async()=>{u.set(`t`,s),u.set(`getIntlayer`,f),u.set(`getDictionary`,p),await t(),e()})})},p=(e,t)=>{try{if(u===void 0)throw Error('Intlayer is not initialized. Add the `app.use("*", intlayer());` middleware before using this function.');if(typeof u.get(`t`)!=`function`)throw Error(`Using the import { t } from "hono-intlayer" is not supported in your environment. Use the context instead.`);return u.get(`t`)(e,t)}catch(r){return process.env.NODE_ENV===`development`&&console.error(r.message),(0,n.getTranslation)(e,t??c.defaultLocale)}},m=(...e)=>{try{if(u===void 0)throw Error('Intlayer is not initialized. Add the `app.use("*", intlayer());` middleware before using this function.');if(typeof u.get(`getIntlayer`)!=`function`)throw Error(`Using the import { getIntlayer } from "hono-intlayer" is not supported in your environment. Use the context instead.`);return u.get(`getIntlayer`)(...e)}catch(t){return process.env.NODE_ENV===`development`&&console.error(t.message),(0,n.getIntlayer)(...e)}},h=(...e)=>{try{if(u===void 0)throw Error('Intlayer is not initialized. Add the `app.use("*", intlayer());` middleware before using this function.');if(typeof u.get(`getDictionary`)!=`function`)throw Error(`Using the import { getDictionary } from "hono-intlayer" is not supported in your environment. Use the context instead.`);return u.get(`getDictionary`)(...e)}catch(t){return process.env.NODE_ENV===`development`&&console.error(t.message),(0,n.getDictionary)(...e)}};exports.getDictionary=h,exports.getIntlayer=m,exports.intlayer=f,exports.t=p,exports.translateFunction=d;
132
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":[],"sources":["../../src/index.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 { Context, MiddlewareHandler } from 'hono';\nimport { getCookie } from 'hono/cookie';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n */\nconst getStorageLocale = (context: Context): Locale | undefined =>\n getLocaleFromStorage({\n getCookie: (name: string) => getCookie(context, name),\n getHeader: (name: string) => context.req.header(name),\n });\n\nconst appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\nexport const translateFunction =\n (context: Context) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n const currentLocale = context.get('locale') as Locale;\n const defaultLocale = context.get('defaultLocale') as Locale;\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 * Hono middleware that detects the user's locale and populates context with Intlayer data.\n *\n * It performs:\n * 1. Locale detection from cookies, headers, or default settings.\n * 2. Injects `t`, `getIntlayer`, and `getDictionary` functions into the context.\n * 3. Sets up a `cls-hooked` namespace for accessing these functions anywhere in the request lifecycle.\n *\n * @returns A Hono middleware function.\n *\n * @example\n * ```ts\n * import { Hono } from 'hono';\n * import { intlayer } from 'hono-intlayer';\n *\n * const app = new Hono();\n * app.use('*', intlayer());\n * ```\n */\nexport const intlayer =\n (): MiddlewareHandler => async (c: Context, next: () => Promise<void>) => {\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeFromStorage = getStorageLocale(c);\n\n const negotiatorHeaders: Record<string, string> = c.req.header();\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n\n c.set('locale_storage', localeFromStorage);\n c.set('locale_detected', localeDetected);\n c.set('locale', locale);\n c.set('defaultLocale', internationalization.defaultLocale);\n\n const t = translateFunction(c);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key: Parameters<typeof getIntlayerFunction>[0],\n localeArg = locale as Parameters<typeof getIntlayerFunction>[1],\n ...props: any[]\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key: Parameters<typeof getDictionaryFunction>[0],\n localeArg = locale as Parameters<typeof getDictionaryFunction>[1],\n ...props: any[]\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n c.set('t', t);\n c.set('getIntlayer', getIntlayer);\n c.set('getDictionary', getDictionary);\n\n return new Promise<void>((resolve) => {\n appNamespace.run(async () => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n await next();\n resolve();\n });\n });\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 'hono-intlayer';\n *\n * app.get('/', (c) => {\n * const greeting = t({\n * en: 'Hello',\n * fr: 'Bonjour',\n * });\n * return c.text(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 `app.use(\"*\", intlayer());` middleware before using this function.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"hono-intlayer\" is not supported in your environment. Use the context 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 = (\n ...args: Parameters<typeof getIntlayerFunction>\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.'\n );\n }\n\n if (typeof appNamespace.get('getIntlayer') !== 'function') {\n throw new Error(\n 'Using the import { getIntlayer } from \"hono-intlayer\" is not supported in your environment. Use the context 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 = (\n ...args: Parameters<typeof getDictionaryFunction>\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.'\n );\n }\n\n if (typeof appNamespace.get('getDictionary') !== 'function') {\n throw new Error(\n 'Using the import { getDictionary } from \"hono-intlayer\" is not supported in your environment. Use the context 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"],"mappings":";;;;;;;;AAcA,MAAM,wDAAkC;AACxC,MAAM,EAAE,yBAAyB;;;;AAKjC,MAAM,oBAAoB,qDACH;CACnB,YAAY,oCAA2B,SAAS,KAAK;CACrD,YAAY,SAAiB,QAAQ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;;;;;;;;;;;;AAyBN,MAAa,KACX,SACA,WACY;AACZ,KAAI;AACF,MAAI,OAAO,iBAAiB,YAC1B,OAAM,IAAI,MACR,4GACD;AAGH,MAAI,OAAO,aAAa,IAAI,IAAI,KAAK,WACnC,OAAM,IAAI,MACR,+GACD;AAGH,SAAO,aAAa,IAAI,IAAI,CAAC,SAAS,OAAO;UACtC,OAAO;AACd,MAAI,QAAQ,IAAI,aAAa,cAC3B,SAAQ,MAAO,MAAgB,QAAQ;AAGzC,4CACE,SACA,UAAU,qBAAqB,cAChC;;;AAIL,MAAa,eACX,GAAG,SACA;AACH,KAAI;AACF,MAAI,OAAO,iBAAiB,YAC1B,OAAM,IAAI,MACR,4GACD;AAGH,MAAI,OAAO,aAAa,IAAI,cAAc,KAAK,WAC7C,OAAM,IAAI,MACR,yHACD;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,MAAa,iBACX,GAAG,SACA;AACH,KAAI;AACF,MAAI,OAAO,iBAAiB,YAC1B,OAAM,IAAI,MACR,4GACD;AAGH,MAAI,OAAO,aAAa,IAAI,gBAAgB,KAAK,WAC/C,OAAM,IAAI,MACR,2HACD;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
+ {"version":3,"file":"index.cjs","names":[],"sources":["../../src/index.ts"],"sourcesContent":["import { prepareIntlayer } from '@intlayer/chokidar/build';\nimport { getConfiguration } from '@intlayer/config/node';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n} from '@intlayer/core/interpreter';\nimport { localeDetector } from '@intlayer/core/localization';\nimport { getLocaleFromStorage } from '@intlayer/core/utils';\nimport type { Locale, StrictModeLocaleMap } from '@intlayer/types';\nimport { createNamespace } from 'cls-hooked';\nimport type { Context, MiddlewareHandler } from 'hono';\nimport { getCookie } from 'hono/cookie';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n */\nconst getStorageLocale = (context: Context): Locale | undefined =>\n getLocaleFromStorage({\n getCookie: (name: string) => getCookie(context, name),\n getHeader: (name: string) => context.req.header(name),\n });\n\nconst appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\nexport const translateFunction =\n (context: Context) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n const currentLocale = context.get('locale') as Locale;\n const defaultLocale = context.get('defaultLocale') as Locale;\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 * Hono middleware that detects the user's locale and populates context with Intlayer data.\n *\n * It performs:\n * 1. Locale detection from cookies, headers, or default settings.\n * 2. Injects `t`, `getIntlayer`, and `getDictionary` functions into the context.\n * 3. Sets up a `cls-hooked` namespace for accessing these functions anywhere in the request lifecycle.\n *\n * @returns A Hono middleware function.\n *\n * @example\n * ```ts\n * import { Hono } from 'hono';\n * import { intlayer } from 'hono-intlayer';\n *\n * const app = new Hono();\n * app.use('*', intlayer());\n * ```\n */\nexport const intlayer =\n (): MiddlewareHandler => async (c: Context, next: () => Promise<void>) => {\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeFromStorage = getStorageLocale(c);\n\n const negotiatorHeaders: Record<string, string> = c.req.header();\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n\n c.set('locale_storage', localeFromStorage);\n c.set('locale_detected', localeDetected);\n c.set('locale', locale);\n c.set('defaultLocale', internationalization.defaultLocale);\n\n const t = translateFunction(c);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key: Parameters<typeof getIntlayerFunction>[0],\n localeArg = locale as Parameters<typeof getIntlayerFunction>[1],\n ...props: any[]\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key: Parameters<typeof getDictionaryFunction>[0],\n localeArg = locale as Parameters<typeof getDictionaryFunction>[1],\n ...props: any[]\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n c.set('t', t);\n c.set('getIntlayer', getIntlayer);\n c.set('getDictionary', getDictionary);\n\n return new Promise<void>((resolve) => {\n appNamespace.run(async () => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n await next();\n resolve();\n });\n });\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 'hono-intlayer';\n *\n * app.get('/', (c) => {\n * const greeting = t({\n * en: 'Hello',\n * fr: 'Bonjour',\n * });\n * return c.text(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 `app.use(\"*\", intlayer());` middleware before using this function.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"hono-intlayer\" is not supported in your environment. Use the context 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 = (\n ...args: Parameters<typeof getIntlayerFunction>\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.'\n );\n }\n\n if (typeof appNamespace.get('getIntlayer') !== 'function') {\n throw new Error(\n 'Using the import { getIntlayer } from \"hono-intlayer\" is not supported in your environment. Use the context 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 = (\n ...args: Parameters<typeof getDictionaryFunction>\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.'\n );\n }\n\n if (typeof appNamespace.get('getDictionary') !== 'function') {\n throw new Error(\n 'Using the import { getDictionary } from \"hono-intlayer\" is not supported in your environment. Use the context 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"],"mappings":"oTAcA,MAAM,GAAA,EAAA,EAAA,mBAAkC,CAClC,CAAE,wBAAyB,EAK3B,EAAoB,IAAA,EAAA,EAAA,sBACH,CACnB,UAAY,IAAA,EAAA,EAAA,WAA2B,EAAS,EAAK,CACrD,UAAY,GAAiB,EAAQ,IAAI,OAAO,EAAK,CACtD,CAAC,CAEE,GAAA,EAAA,EAAA,iBAA+B,MAAM,uBAE3B,EAAc,CAE9B,MAAa,EACV,IAEC,EACA,IACM,CACN,IAAM,EAAgB,EAAQ,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AJI,QAAQ,IAAI,WAAa,eAC3B,QAAQ,MAAO,EAAgB,QAAQ,EAGzC,EAAA,EAAA,gBACE,EACA,GAAU,EAAqB,cAChC,GAIQ,GACX,GAAG,IACA,CACH,GAAI,CACF,GAAW,IAAiB,OAC1B,MAAU,MACR,0GACD,CAGH,GAAI,OAAO,EAAa,IAAI,cAAc,EAAK,WAC7C,MAAU,MACR,uHACD,CAGH,OAAO,EAAa,IAAI,cAAc,CAAC,GAAG,EAAK,OACxC,EAAO,CAId,OAHI,QAAQ,IAAI,WAAa,eAC3B,QAAQ,MAAO,EAAgB,QAAQ,EAEzC,EAAA,EAAA,aAA2B,GAAG,EAAK,GAI1B,GACX,GAAG,IACA,CACH,GAAI,CACF,GAAW,IAAiB,OAC1B,MAAU,MACR,0GACD,CAGH,GAAI,OAAO,EAAa,IAAI,gBAAgB,EAAK,WAC/C,MAAU,MACR,yHACD,CAGH,OAAO,EAAa,IAAI,gBAAgB,CAAC,GAAG,EAAK,OAC1C,EAAO,CAId,OAHI,QAAQ,IAAI,WAAa,eAC3B,QAAQ,MAAO,EAAgB,QAAQ,EAEzC,EAAA,EAAA,eAA6B,GAAG,EAAK"}
@@ -1,127 +1,2 @@
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
- import { getCookie } from "hono/cookie";
6
-
7
- //#region src/index.ts
8
- const configuration = getConfiguration();
9
- const { internationalization } = configuration;
10
- /**
11
- * Retrieves the locale from storage (cookies, headers).
12
- */
13
- const getStorageLocale = (context) => getLocaleFromStorage({
14
- getCookie: (name) => getCookie(context, name),
15
- getHeader: (name) => context.req.header(name)
16
- });
17
- const appNamespace = createNamespace("app");
18
- prepareIntlayer(configuration);
19
- const translateFunction = (context) => (content, locale) => {
20
- const currentLocale = context.get("locale");
21
- const defaultLocale = context.get("defaultLocale");
22
- const targetLocale = locale ?? currentLocale;
23
- if (typeof content === "undefined") return "";
24
- if (typeof content === "string") return content;
25
- if (typeof content?.[targetLocale] === "undefined") if (typeof content?.[defaultLocale] === "undefined") return content;
26
- else return getTranslation(content, defaultLocale);
27
- return getTranslation(content, targetLocale);
28
- };
29
- /**
30
- * Hono middleware that detects the user's locale and populates context with Intlayer data.
31
- *
32
- * It performs:
33
- * 1. Locale detection from cookies, headers, or default settings.
34
- * 2. Injects `t`, `getIntlayer`, and `getDictionary` functions into the context.
35
- * 3. Sets up a `cls-hooked` namespace for accessing these functions anywhere in the request lifecycle.
36
- *
37
- * @returns A Hono middleware function.
38
- *
39
- * @example
40
- * ```ts
41
- * import { Hono } from 'hono';
42
- * import { intlayer } from 'hono-intlayer';
43
- *
44
- * const app = new Hono();
45
- * app.use('*', intlayer());
46
- * ```
47
- */
48
- const intlayer = () => async (c, next) => {
49
- const localeFromStorage = getStorageLocale(c);
50
- const localeDetected = localeDetector(c.req.header(), internationalization.locales, internationalization.defaultLocale);
51
- const locale = localeFromStorage ?? localeDetected;
52
- c.set("locale_storage", localeFromStorage);
53
- c.set("locale_detected", localeDetected);
54
- c.set("locale", locale);
55
- c.set("defaultLocale", internationalization.defaultLocale);
56
- const t = translateFunction(c);
57
- const getIntlayer = (key, localeArg = locale, ...props) => getIntlayer$1(key, localeArg, ...props);
58
- const getDictionary = (key, localeArg = locale, ...props) => getDictionary$1(key, localeArg, ...props);
59
- c.set("t", t);
60
- c.set("getIntlayer", getIntlayer);
61
- c.set("getDictionary", getDictionary);
62
- return new Promise((resolve) => {
63
- appNamespace.run(async () => {
64
- appNamespace.set("t", t);
65
- appNamespace.set("getIntlayer", getIntlayer);
66
- appNamespace.set("getDictionary", getDictionary);
67
- await next();
68
- resolve();
69
- });
70
- });
71
- };
72
- /**
73
- * Translation function to retrieve content for the current locale.
74
- *
75
- * This function works within the request lifecycle managed by the `intlayer` middleware.
76
- *
77
- * @param content - A map of locales to content.
78
- * @param locale - Optional locale override.
79
- * @returns The translated content.
80
- *
81
- * @example
82
- * ```ts
83
- * import { t } from 'hono-intlayer';
84
- *
85
- * app.get('/', (c) => {
86
- * const greeting = t({
87
- * en: 'Hello',
88
- * fr: 'Bonjour',
89
- * });
90
- * return c.text(greeting);
91
- * });
92
- * ```
93
- */
94
- const t = (content, locale) => {
95
- try {
96
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.");
97
- if (typeof appNamespace.get("t") !== "function") throw new Error("Using the import { t } from \"hono-intlayer\" is not supported in your environment. Use the context instead.");
98
- return appNamespace.get("t")(content, locale);
99
- } catch (error) {
100
- console.error(error.message);
101
- return getTranslation(content, locale ?? internationalization.defaultLocale);
102
- }
103
- };
104
- const getIntlayer = (...args) => {
105
- try {
106
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.");
107
- if (typeof appNamespace.get("getIntlayer") !== "function") throw new Error("Using the import { getIntlayer } from \"hono-intlayer\" is not supported in your environment. Use the context instead.");
108
- return appNamespace.get("getIntlayer")(...args);
109
- } catch (error) {
110
- console.error(error.message);
111
- return getIntlayer$1(...args);
112
- }
113
- };
114
- const getDictionary = (...args) => {
115
- try {
116
- if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.");
117
- if (typeof appNamespace.get("getDictionary") !== "function") throw new Error("Using the import { getDictionary } from \"hono-intlayer\" is not supported in your environment. Use the context instead.");
118
- return appNamespace.get("getDictionary")(...args);
119
- } catch (error) {
120
- console.error(error.message);
121
- return getDictionary$1(...args);
122
- }
123
- };
124
-
125
- //#endregion
126
- export { getDictionary, getIntlayer, intlayer, t, translateFunction };
1
+ import{prepareIntlayer as e}from"@intlayer/chokidar/build";import{getConfiguration as t}from"@intlayer/config/node";import{getDictionary as n,getIntlayer as r,getTranslation as i}from"@intlayer/core/interpreter";import{localeDetector as a}from"@intlayer/core/localization";import{getLocaleFromStorage as o}from"@intlayer/core/utils";import{createNamespace as s}from"cls-hooked";import{getCookie as c}from"hono/cookie";const l=t(),{internationalization:u}=l,d=e=>o({getCookie:t=>c(e,t),getHeader:t=>e.req.header(t)}),f=s(`app`);e(l);const p=e=>(t,n)=>{let r=e.get(`locale`),a=e.get(`defaultLocale`),o=n??r;return t===void 0?``:typeof t==`string`?t:t?.[o]===void 0?t?.[a]===void 0?t:i(t,a):i(t,o)},m=()=>async(e,t)=>{let i=d(e),o=a(e.req.header(),u.locales,u.defaultLocale),s=i??o;e.set(`locale_storage`,i),e.set(`locale_detected`,o),e.set(`locale`,s),e.set(`defaultLocale`,u.defaultLocale);let c=p(e),l=(e,t=s,...n)=>r(e,t,...n),m=(e,t=s,...r)=>n(e,t,...r);return e.set(`t`,c),e.set(`getIntlayer`,l),e.set(`getDictionary`,m),new Promise(e=>{f.run(async()=>{f.set(`t`,c),f.set(`getIntlayer`,l),f.set(`getDictionary`,m),await t(),e()})})},h=(e,t)=>{try{if(f===void 0)throw Error('Intlayer is not initialized. Add the `app.use("*", intlayer());` middleware before using this function.');if(typeof f.get(`t`)!=`function`)throw Error(`Using the import { t } from "hono-intlayer" is not supported in your environment. Use the context instead.`);return f.get(`t`)(e,t)}catch{return i(e,t??u.defaultLocale)}},g=(...e)=>{try{if(f===void 0)throw Error('Intlayer is not initialized. Add the `app.use("*", intlayer());` middleware before using this function.');if(typeof f.get(`getIntlayer`)!=`function`)throw Error(`Using the import { getIntlayer } from "hono-intlayer" is not supported in your environment. Use the context instead.`);return f.get(`getIntlayer`)(...e)}catch{return r(...e)}},_=(...e)=>{try{if(f===void 0)throw Error('Intlayer is not initialized. Add the `app.use("*", intlayer());` middleware before using this function.');if(typeof f.get(`getDictionary`)!=`function`)throw Error(`Using the import { getDictionary } from "hono-intlayer" is not supported in your environment. Use the context instead.`);return f.get(`getDictionary`)(...e)}catch{return n(...e)}};export{_ as getDictionary,g as getIntlayer,m as intlayer,h as t,p as translateFunction};
127
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["getIntlayerFunction","getDictionaryFunction"],"sources":["../../src/index.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 { Context, MiddlewareHandler } from 'hono';\nimport { getCookie } from 'hono/cookie';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n */\nconst getStorageLocale = (context: Context): Locale | undefined =>\n getLocaleFromStorage({\n getCookie: (name: string) => getCookie(context, name),\n getHeader: (name: string) => context.req.header(name),\n });\n\nconst appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\nexport const translateFunction =\n (context: Context) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n const currentLocale = context.get('locale') as Locale;\n const defaultLocale = context.get('defaultLocale') as Locale;\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 * Hono middleware that detects the user's locale and populates context with Intlayer data.\n *\n * It performs:\n * 1. Locale detection from cookies, headers, or default settings.\n * 2. Injects `t`, `getIntlayer`, and `getDictionary` functions into the context.\n * 3. Sets up a `cls-hooked` namespace for accessing these functions anywhere in the request lifecycle.\n *\n * @returns A Hono middleware function.\n *\n * @example\n * ```ts\n * import { Hono } from 'hono';\n * import { intlayer } from 'hono-intlayer';\n *\n * const app = new Hono();\n * app.use('*', intlayer());\n * ```\n */\nexport const intlayer =\n (): MiddlewareHandler => async (c: Context, next: () => Promise<void>) => {\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeFromStorage = getStorageLocale(c);\n\n const negotiatorHeaders: Record<string, string> = c.req.header();\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n\n c.set('locale_storage', localeFromStorage);\n c.set('locale_detected', localeDetected);\n c.set('locale', locale);\n c.set('defaultLocale', internationalization.defaultLocale);\n\n const t = translateFunction(c);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key: Parameters<typeof getIntlayerFunction>[0],\n localeArg = locale as Parameters<typeof getIntlayerFunction>[1],\n ...props: any[]\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key: Parameters<typeof getDictionaryFunction>[0],\n localeArg = locale as Parameters<typeof getDictionaryFunction>[1],\n ...props: any[]\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n c.set('t', t);\n c.set('getIntlayer', getIntlayer);\n c.set('getDictionary', getDictionary);\n\n return new Promise<void>((resolve) => {\n appNamespace.run(async () => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n await next();\n resolve();\n });\n });\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 'hono-intlayer';\n *\n * app.get('/', (c) => {\n * const greeting = t({\n * en: 'Hello',\n * fr: 'Bonjour',\n * });\n * return c.text(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 `app.use(\"*\", intlayer());` middleware before using this function.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"hono-intlayer\" is not supported in your environment. Use the context 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 = (\n ...args: Parameters<typeof getIntlayerFunction>\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.'\n );\n }\n\n if (typeof appNamespace.get('getIntlayer') !== 'function') {\n throw new Error(\n 'Using the import { getIntlayer } from \"hono-intlayer\" is not supported in your environment. Use the context 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 = (\n ...args: Parameters<typeof getDictionaryFunction>\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.'\n );\n }\n\n if (typeof appNamespace.get('getDictionary') !== 'function') {\n throw new Error(\n 'Using the import { getDictionary } from \"hono-intlayer\" is not supported in your environment. Use the context 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"],"mappings":";;;;;;;AAcA,MAAM,gBAAgB,kBAAkB;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Aa,IAAI,YAAY;AAC3B,gBAAa,IAAI,KAAK,EAAE;AACxB,gBAAa,IAAI,eAAe,YAAY;AAC5C,gBAAa,IAAI,iBAAiB,cAAc;AAEhD,SAAM,MAAM;AACZ,YAAS;IACT;GACF;;;;;;;;;;;;;;;;;;;;;;;;AAyBN,MAAa,KACX,SACA,WACY;AACZ,KAAI;AACF,MAAI,OAAO,iBAAiB,YAC1B,OAAM,IAAI,MACR,4GACD;AAGH,MAAI,OAAO,aAAa,IAAI,IAAI,KAAK,WACnC,OAAM,IAAI,MACR,+GACD;AAGH,SAAO,aAAa,IAAI,IAAI,CAAC,SAAS,OAAO;UACtC,OAAO;AAEZ,UAAQ,MAAO,MAAgB,QAAQ;AAGzC,SAAO,eACL,SACA,UAAU,qBAAqB,cAChC;;;AAIL,MAAa,eACX,GAAG,SACA;AACH,KAAI;AACF,MAAI,OAAO,iBAAiB,YAC1B,OAAM,IAAI,MACR,4GACD;AAGH,MAAI,OAAO,aAAa,IAAI,cAAc,KAAK,WAC7C,OAAM,IAAI,MACR,yHACD;AAGH,SAAO,aAAa,IAAI,cAAc,CAAC,GAAG,KAAK;UACxC,OAAO;AAEZ,UAAQ,MAAO,MAAgB,QAAQ;AAEzC,SAAOD,cAAoB,GAAG,KAAK;;;AAIvC,MAAa,iBACX,GAAG,SACA;AACH,KAAI;AACF,MAAI,OAAO,iBAAiB,YAC1B,OAAM,IAAI,MACR,4GACD;AAGH,MAAI,OAAO,aAAa,IAAI,gBAAgB,KAAK,WAC/C,OAAM,IAAI,MACR,2HACD;AAGH,SAAO,aAAa,IAAI,gBAAgB,CAAC,GAAG,KAAK;UAC1C,OAAO;AAEZ,UAAQ,MAAO,MAAgB,QAAQ;AAEzC,SAAOC,gBAAsB,GAAG,KAAK"}
1
+ {"version":3,"file":"index.mjs","names":["getIntlayerFunction","getDictionaryFunction"],"sources":["../../src/index.ts"],"sourcesContent":["import { prepareIntlayer } from '@intlayer/chokidar/build';\nimport { getConfiguration } from '@intlayer/config/node';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n} from '@intlayer/core/interpreter';\nimport { localeDetector } from '@intlayer/core/localization';\nimport { getLocaleFromStorage } from '@intlayer/core/utils';\nimport type { Locale, StrictModeLocaleMap } from '@intlayer/types';\nimport { createNamespace } from 'cls-hooked';\nimport type { Context, MiddlewareHandler } from 'hono';\nimport { getCookie } from 'hono/cookie';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n */\nconst getStorageLocale = (context: Context): Locale | undefined =>\n getLocaleFromStorage({\n getCookie: (name: string) => getCookie(context, name),\n getHeader: (name: string) => context.req.header(name),\n });\n\nconst appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\nexport const translateFunction =\n (context: Context) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n const currentLocale = context.get('locale') as Locale;\n const defaultLocale = context.get('defaultLocale') as Locale;\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 * Hono middleware that detects the user's locale and populates context with Intlayer data.\n *\n * It performs:\n * 1. Locale detection from cookies, headers, or default settings.\n * 2. Injects `t`, `getIntlayer`, and `getDictionary` functions into the context.\n * 3. Sets up a `cls-hooked` namespace for accessing these functions anywhere in the request lifecycle.\n *\n * @returns A Hono middleware function.\n *\n * @example\n * ```ts\n * import { Hono } from 'hono';\n * import { intlayer } from 'hono-intlayer';\n *\n * const app = new Hono();\n * app.use('*', intlayer());\n * ```\n */\nexport const intlayer =\n (): MiddlewareHandler => async (c: Context, next: () => Promise<void>) => {\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeFromStorage = getStorageLocale(c);\n\n const negotiatorHeaders: Record<string, string> = c.req.header();\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n\n c.set('locale_storage', localeFromStorage);\n c.set('locale_detected', localeDetected);\n c.set('locale', locale);\n c.set('defaultLocale', internationalization.defaultLocale);\n\n const t = translateFunction(c);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key: Parameters<typeof getIntlayerFunction>[0],\n localeArg = locale as Parameters<typeof getIntlayerFunction>[1],\n ...props: any[]\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key: Parameters<typeof getDictionaryFunction>[0],\n localeArg = locale as Parameters<typeof getDictionaryFunction>[1],\n ...props: any[]\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n c.set('t', t);\n c.set('getIntlayer', getIntlayer);\n c.set('getDictionary', getDictionary);\n\n return new Promise<void>((resolve) => {\n appNamespace.run(async () => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n await next();\n resolve();\n });\n });\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 'hono-intlayer';\n *\n * app.get('/', (c) => {\n * const greeting = t({\n * en: 'Hello',\n * fr: 'Bonjour',\n * });\n * return c.text(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 `app.use(\"*\", intlayer());` middleware before using this function.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"hono-intlayer\" is not supported in your environment. Use the context 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 = (\n ...args: Parameters<typeof getIntlayerFunction>\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.'\n );\n }\n\n if (typeof appNamespace.get('getIntlayer') !== 'function') {\n throw new Error(\n 'Using the import { getIntlayer } from \"hono-intlayer\" is not supported in your environment. Use the context 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 = (\n ...args: Parameters<typeof getDictionaryFunction>\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `app.use(\"*\", intlayer());` middleware before using this function.'\n );\n }\n\n if (typeof appNamespace.get('getDictionary') !== 'function') {\n throw new Error(\n 'Using the import { getDictionary } from \"hono-intlayer\" is not supported in your environment. Use the context 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"],"mappings":"kaAcA,MAAM,EAAgB,GAAkB,CAClC,CAAE,wBAAyB,EAK3B,EAAoB,GACxB,EAAqB,CACnB,UAAY,GAAiB,EAAU,EAAS,EAAK,CACrD,UAAY,GAAiB,EAAQ,IAAI,OAAO,EAAK,CACtD,CAAC,CAEE,EAAe,EAAgB,MAAM,C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/B,CAKd,OAAO,EACL,EACA,GAAU,EAAqB,cAChC,GAIQ,GACX,GAAG,IACA,CACH,GAAI,CACF,GAAW,IAAiB,OAC1B,MAAU,MACR,0GACD,CAGH,GAAI,OAAO,EAAa,IAAI,cAAc,EAAK,WAC7C,MAAU,MACR,uHACD,CAGH,OAAO,EAAa,IAAI,cAAc,CAAC,GAAG,EAAK,MACjC,CAId,OAAOD,EAAoB,GAAG,EAAK,GAI1B,GACX,GAAG,IACA,CACH,GAAI,CACF,GAAW,IAAiB,OAC1B,MAAU,MACR,0GACD,CAGH,GAAI,OAAO,EAAa,IAAI,gBAAgB,EAAK,WAC/C,MAAU,MACR,yHACD,CAGH,OAAO,EAAa,IAAI,gBAAgB,CAAC,GAAG,EAAK,MACnC,CAId,OAAOC,EAAsB,GAAG,EAAK"}
@@ -1,4 +1,4 @@
1
- import { getDictionary as getDictionary$1, getIntlayer as getIntlayer$1 } from "@intlayer/core";
1
+ import { getDictionary as getDictionary$1, getIntlayer as getIntlayer$1 } from "@intlayer/core/interpreter";
2
2
  import { Locale, StrictModeLocaleMap } from "@intlayer/types";
3
3
  import { Context, MiddlewareHandler } from "hono";
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono-intlayer",
3
- "version": "8.1.2",
3
+ "version": "8.1.3",
4
4
  "private": false,
5
5
  "description": "Manage internationalization i18n in a simple way for hono 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": "8.1.2",
80
- "@intlayer/config": "8.1.2",
81
- "@intlayer/core": "8.1.2",
82
- "@intlayer/types": "8.1.2",
79
+ "@intlayer/chokidar": "8.1.3",
80
+ "@intlayer/config": "8.1.3",
81
+ "@intlayer/core": "8.1.3",
82
+ "@intlayer/types": "8.1.3",
83
83
  "cls-hooked": "4.2.2",
84
- "intlayer": "8.1.2"
84
+ "intlayer": "8.1.3"
85
85
  },
86
86
  "devDependencies": {
87
87
  "@types/cls-hooked": "4.3.9",