adonis-intlayer 8.9.1 → 8.9.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.
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":[],"sources":["../../src/index.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport { 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 { getLocaleFromStorageServer } from '@intlayer/core/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\nimport { createNamespace } from 'cls-hooked';\n\n// Zero-cost fallback, will be updated with AdonisJS logger or console in dev mode\nlet debug: (message: string) => void = () => {};\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\nif (process.env['NODE_ENV'] === 'development') {\n try {\n const logger = require('@adonisjs/core/services/logger').default;\n debug = (msg: string) => logger.debug(msg);\n } catch {\n debug = (msg: string) => console.debug(msg);\n }\n}\n\nexport const appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n */\nexport const getStorageLocale = (ctx: HttpContext): Locale | undefined =>\n getLocaleFromStorageServer({\n getCookie: (name: string) => ctx.request.cookie(name),\n getHeader: (name: string) => ctx.request.header(name),\n });\n\n/**\n * Translation function for context\n */\nexport const translateFunction =\n (ctx: HttpContext) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n const { locale: currentLocale, defaultLocale } = ctx as unknown as {\n locale: Locale;\n defaultLocale: Locale;\n };\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[\n targetLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Translation function to retrieve content for the current locale.\n *\n * This function works within the request lifecycle managed by the `intlayer` middleware.\n *\n * @param content - A map of locales to content.\n * @param locale - Optional locale override.\n * @returns The translated content.\n *\n * @example\n * ```ts\n * import { t } from 'adonis-intlayer';\n *\n * router.get('/', async () => {\n * const greeting = t({\n * en: 'Hello',\n * fr: 'Bonjour',\n * });\n * return greeting;\n * });\n * ```\n */\nexport const t = <Content = string>(\n content: StrictModeLocaleMap<Content>,\n locale?: Locale\n): Content => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return appNamespace.get('t')(content, locale);\n } catch (error) {\n debug((error as Error).message);\n\n return getTranslation(\n content,\n locale ?? internationalization.defaultLocale\n );\n }\n};\n\nexport const getIntlayer: typeof getIntlayerFunction = ((\n key: any,\n localeArg?: any,\n ...props: any[]\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n const getIntlayerWrapped = appNamespace.get('getIntlayer');\n\n if (typeof getIntlayerWrapped !== 'function') {\n throw new Error(\n 'Using the import { getIntlayer } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return getIntlayerWrapped(key, localeArg, ...props);\n } catch (error) {\n debug((error as Error).message);\n\n return getIntlayerFunction(key, localeArg, ...props);\n }\n}) as typeof getIntlayerFunction;\n\nexport const getDictionary: typeof getDictionaryFunction = ((\n key: any,\n localeArg?: any,\n ...props: any[]\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n const getDictionaryWrapped = appNamespace.get('getDictionary');\n\n if (typeof getDictionaryWrapped !== 'function') {\n throw new Error(\n 'Using the import { getDictionary } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return getDictionaryWrapped(key, localeArg, ...props);\n } catch (error) {\n debug((error as Error).message);\n\n return getDictionaryFunction(key, localeArg, ...props);\n }\n}) as typeof getDictionaryFunction;\n\nexport const getLocale = (locale?: Locale): Locale => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n return (\n appNamespace.get('locale') ?? locale ?? internationalization.defaultLocale\n );\n } catch (_error) {\n return locale ?? internationalization.defaultLocale;\n }\n};\n\nexport { default as IntlayerMiddleware } from './middleware';\n"],"mappings":";;;;;;;;;AAcA,IAAI,cAAyC;AAE7C,MAAM,6DAAkC;AACxC,MAAM,EAAE,yBAAyB;AAEjC,IAAI,QAAQ,IAAI,gBAAgB,
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../../src/index.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport { 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 { getLocaleFromStorageServer } from '@intlayer/core/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\nimport { createNamespace } from 'cls-hooked';\n\n// Zero-cost fallback, will be updated with AdonisJS logger or console in dev mode\nlet debug: (message: string) => void = () => {};\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\nif (process.env['NODE_ENV'] === 'development') {\n try {\n const logger = require('@adonisjs/core/services/logger').default;\n debug = (msg: string) => logger.debug(msg);\n } catch {\n debug = (msg: string) => console.debug(msg);\n }\n}\n\nexport const appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n */\nexport const getStorageLocale = (ctx: HttpContext): Locale | undefined =>\n getLocaleFromStorageServer({\n getCookie: (name: string) => ctx.request.cookie(name),\n getHeader: (name: string) => ctx.request.header(name),\n });\n\n/**\n * Translation function for context\n */\nexport const translateFunction =\n (ctx: HttpContext) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n const { locale: currentLocale, defaultLocale } = ctx as unknown as {\n locale: Locale;\n defaultLocale: Locale;\n };\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[\n targetLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Translation function to retrieve content for the current locale.\n *\n * This function works within the request lifecycle managed by the `intlayer` middleware.\n *\n * @param content - A map of locales to content.\n * @param locale - Optional locale override.\n * @returns The translated content.\n *\n * @example\n * ```ts\n * import { t } from 'adonis-intlayer';\n *\n * router.get('/', async () => {\n * const greeting = t({\n * en: 'Hello',\n * fr: 'Bonjour',\n * });\n * return greeting;\n * });\n * ```\n */\nexport const t = <Content = string>(\n content: StrictModeLocaleMap<Content>,\n locale?: Locale\n): Content => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return appNamespace.get('t')(content, locale);\n } catch (error) {\n debug((error as Error).message);\n\n return getTranslation(\n content,\n locale ?? internationalization.defaultLocale\n );\n }\n};\n\nexport const getIntlayer: typeof getIntlayerFunction = ((\n key: any,\n localeArg?: any,\n ...props: any[]\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n const getIntlayerWrapped = appNamespace.get('getIntlayer');\n\n if (typeof getIntlayerWrapped !== 'function') {\n throw new Error(\n 'Using the import { getIntlayer } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return getIntlayerWrapped(key, localeArg, ...props);\n } catch (error) {\n debug((error as Error).message);\n\n return getIntlayerFunction(key, localeArg, ...props);\n }\n}) as typeof getIntlayerFunction;\n\nexport const getDictionary: typeof getDictionaryFunction = ((\n key: any,\n localeArg?: any,\n ...props: any[]\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n const getDictionaryWrapped = appNamespace.get('getDictionary');\n\n if (typeof getDictionaryWrapped !== 'function') {\n throw new Error(\n 'Using the import { getDictionary } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return getDictionaryWrapped(key, localeArg, ...props);\n } catch (error) {\n debug((error as Error).message);\n\n return getDictionaryFunction(key, localeArg, ...props);\n }\n}) as typeof getDictionaryFunction;\n\nexport const getLocale = (locale?: Locale): Locale => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n return (\n appNamespace.get('locale') ?? locale ?? internationalization.defaultLocale\n );\n } catch (_error) {\n return locale ?? internationalization.defaultLocale;\n }\n};\n\nexport { default as IntlayerMiddleware } from './middleware';\n"],"mappings":";;;;;;;;;AAcA,IAAI,cAAyC;AAE7C,MAAM,6DAAkC;AACxC,MAAM,EAAE,yBAAyB;AAEjC,IAAI,QAAQ,IAAI,gBAAgB,eAC9B,IAAI;CACF,MAAM,SAAS,QAAQ,iCAAiC,CAAC;CACzD,SAAS,QAAgB,OAAO,MAAM,IAAI;QACpC;CACN,SAAS,QAAgB,QAAQ,MAAM,IAAI;;AAI/C,MAAa,+CAA+B,MAAM;8CAElC,cAAc;;;;AAK9B,MAAa,oBAAoB,6DACJ;CACzB,YAAY,SAAiB,IAAI,QAAQ,OAAO,KAAK;CACrD,YAAY,SAAiB,IAAI,QAAQ,OAAO,KAAK;CACtD,CAAC;;;;AAKJ,MAAa,qBACV,SAEC,SACA,WACM;CACN,MAAM,EAAE,QAAQ,eAAe,kBAAkB;CAKjD,MAAM,eAAe,UAAU;CAE/B,IAAI,OAAO,YAAY,aACrB,OAAO;CAGT,IAAI,OAAO,YAAY,UACrB,OAAO;CAGT,IACE,OAAO,UACL,kBACI,aAEN,IACE,OAAO,UACL,mBACI,aAEN,OAAO;MAEP,sDAAsB,SAAS,cAAc;CAIjD,sDAAsB,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;AAyBhD,MAAa,KACX,SACA,WACY;CACZ,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,wGACD;EAGH,IAAI,OAAO,aAAa,IAAI,IAAI,KAAK,YACnC,MAAM,IAAI,MACR,iIACD;EAGH,OAAO,aAAa,IAAI,IAAI,CAAC,SAAS,OAAO;UACtC,OAAO;EACd,MAAO,MAAgB,QAAQ;EAE/B,sDACE,SACA,UAAU,qBAAqB,cAChC;;;AAIL,MAAa,gBACX,KACA,WACA,GAAG,UACA;CACH,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,wGACD;EAGH,MAAM,qBAAqB,aAAa,IAAI,cAAc;EAE1D,IAAI,OAAO,uBAAuB,YAChC,MAAM,IAAI,MACR,2IACD;EAGH,OAAO,mBAAmB,KAAK,WAAW,GAAG,MAAM;UAC5C,OAAO;EACd,MAAO,MAAgB,QAAQ;EAE/B,mDAA2B,KAAK,WAAW,GAAG,MAAM;;;AAIxD,MAAa,kBACX,KACA,WACA,GAAG,UACA;CACH,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,wGACD;EAGH,MAAM,uBAAuB,aAAa,IAAI,gBAAgB;EAE9D,IAAI,OAAO,yBAAyB,YAClC,MAAM,IAAI,MACR,6IACD;EAGH,OAAO,qBAAqB,KAAK,WAAW,GAAG,MAAM;UAC9C,OAAO;EACd,MAAO,MAAgB,QAAQ;EAE/B,qDAA6B,KAAK,WAAW,GAAG,MAAM;;;AAI1D,MAAa,aAAa,WAA4B;CACpD,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,wGACD;EAGH,OACE,aAAa,IAAI,SAAS,IAAI,UAAU,qBAAqB;UAExD,QAAQ;EACf,OAAO,UAAU,qBAAqB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.cjs","names":["getStorageLocale","translateFunction","appNamespace"],"sources":["../../src/middleware.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport type { NextFn } from '@adonisjs/core/types/http';\nimport { getConfiguration } from '@intlayer/config/node';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n} from '@intlayer/core/interpreter';\nimport { appNamespace, getStorageLocale, translateFunction } from './index';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * AdonisJS middleware that detects the user's locale and populates the context with Intlayer data.\n */\nexport default class IntlayerMiddleware {\n async handle(ctx: HttpContext, next: NextFn) {\n // Detect if locale is set by intlayer frontend lib in the headers or cookies\n const localeFromStorage = getStorageLocale(ctx);\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // Copy all headers from the request to negotiatorHeaders\n const headers = ctx.request.headers();\n for (const key in headers) {\n const val = headers[key];\n if (typeof val === 'string') {\n negotiatorHeaders[key] = val;\n } else if (Array.isArray(val)) {\n negotiatorHeaders[key] = val.join(',');\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n\n // Decorate context\n (ctx as any).locale = locale;\n (ctx as any).defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(ctx);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = locale as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = locale as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n // Make functions available via CLS\n await appNamespace.runPromise(async () => {\n appNamespace.set('locale', locale);\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n await next();\n });\n }\n}\n"],"mappings":";;;;;AAUA,MAAM,EAAE,sEAAsC;;;;AAK9C,IAAqB,qBAArB,MAAwC;CACtC,MAAM,OAAO,KAAkB,MAAc;EAE3C,MAAM,oBAAoBA,+BAAiB,IAAI;EAE/C,MAAM,oBAA4C,EAAE;EAGpD,MAAM,UAAU,IAAI,QAAQ,SAAS;
|
|
1
|
+
{"version":3,"file":"middleware.cjs","names":["getStorageLocale","translateFunction","appNamespace"],"sources":["../../src/middleware.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport type { NextFn } from '@adonisjs/core/types/http';\nimport { getConfiguration } from '@intlayer/config/node';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n} from '@intlayer/core/interpreter';\nimport { appNamespace, getStorageLocale, translateFunction } from './index';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * AdonisJS middleware that detects the user's locale and populates the context with Intlayer data.\n */\nexport default class IntlayerMiddleware {\n async handle(ctx: HttpContext, next: NextFn) {\n // Detect if locale is set by intlayer frontend lib in the headers or cookies\n const localeFromStorage = getStorageLocale(ctx);\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // Copy all headers from the request to negotiatorHeaders\n const headers = ctx.request.headers();\n for (const key in headers) {\n const val = headers[key];\n if (typeof val === 'string') {\n negotiatorHeaders[key] = val;\n } else if (Array.isArray(val)) {\n negotiatorHeaders[key] = val.join(',');\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n\n // Decorate context\n (ctx as any).locale = locale;\n (ctx as any).defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(ctx);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = locale as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = locale as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n // Make functions available via CLS\n await appNamespace.runPromise(async () => {\n appNamespace.set('locale', locale);\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n await next();\n });\n }\n}\n"],"mappings":";;;;;AAUA,MAAM,EAAE,sEAAsC;;;;AAK9C,IAAqB,qBAArB,MAAwC;CACtC,MAAM,OAAO,KAAkB,MAAc;EAE3C,MAAM,oBAAoBA,+BAAiB,IAAI;EAE/C,MAAM,oBAA4C,EAAE;EAGpD,MAAM,UAAU,IAAI,QAAQ,SAAS;EACrC,KAAK,MAAM,OAAO,SAAS;GACzB,MAAM,MAAM,QAAQ;GACpB,IAAI,OAAO,QAAQ,UACjB,kBAAkB,OAAO;QACpB,IAAI,MAAM,QAAQ,IAAI,EAC3B,kBAAkB,OAAO,IAAI,KAAK,IAAI;;EAI1C,MAAM,iBAAiB,eACrB,mBACA,qBAAqB,SACrB,qBAAqB,cACtB;EAED,MAAM,SAAS,qBAAqB;EAGpC,AAAC,IAAY,SAAS;EACtB,AAAC,IAAY,gBAAgB,qBAAqB;EAElD,MAAM,IAAIC,gCAAkB,IAAI;EAEhC,MAAM,eACJ,KACA,YAAY,QACZ,GAAG,sDACoB,KAAK,WAAW,GAAG,MAAM;EAElD,MAAM,iBACJ,KACA,YAAY,QACZ,GAAG,wDACsB,KAAK,WAAW,GAAG,MAAM;EAGpD,MAAMC,2BAAa,WAAW,YAAY;GACxC,2BAAa,IAAI,UAAU,OAAO;GAClC,2BAAa,IAAI,KAAK,EAAE;GACxB,2BAAa,IAAI,eAAe,YAAY;GAC5C,2BAAa,IAAI,iBAAiB,cAAc;GAEhD,MAAM,MAAM;IACZ"}
|
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 { 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 { getLocaleFromStorageServer } from '@intlayer/core/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\nimport { createNamespace } from 'cls-hooked';\n\n// Zero-cost fallback, will be updated with AdonisJS logger or console in dev mode\nlet debug: (message: string) => void = () => {};\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\nif (process.env['NODE_ENV'] === 'development') {\n try {\n const logger = require('@adonisjs/core/services/logger').default;\n debug = (msg: string) => logger.debug(msg);\n } catch {\n debug = (msg: string) => console.debug(msg);\n }\n}\n\nexport const appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n */\nexport const getStorageLocale = (ctx: HttpContext): Locale | undefined =>\n getLocaleFromStorageServer({\n getCookie: (name: string) => ctx.request.cookie(name),\n getHeader: (name: string) => ctx.request.header(name),\n });\n\n/**\n * Translation function for context\n */\nexport const translateFunction =\n (ctx: HttpContext) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n const { locale: currentLocale, defaultLocale } = ctx as unknown as {\n locale: Locale;\n defaultLocale: Locale;\n };\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[\n targetLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Translation function to retrieve content for the current locale.\n *\n * This function works within the request lifecycle managed by the `intlayer` middleware.\n *\n * @param content - A map of locales to content.\n * @param locale - Optional locale override.\n * @returns The translated content.\n *\n * @example\n * ```ts\n * import { t } from 'adonis-intlayer';\n *\n * router.get('/', async () => {\n * const greeting = t({\n * en: 'Hello',\n * fr: 'Bonjour',\n * });\n * return greeting;\n * });\n * ```\n */\nexport const t = <Content = string>(\n content: StrictModeLocaleMap<Content>,\n locale?: Locale\n): Content => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return appNamespace.get('t')(content, locale);\n } catch (error) {\n debug((error as Error).message);\n\n return getTranslation(\n content,\n locale ?? internationalization.defaultLocale\n );\n }\n};\n\nexport const getIntlayer: typeof getIntlayerFunction = ((\n key: any,\n localeArg?: any,\n ...props: any[]\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n const getIntlayerWrapped = appNamespace.get('getIntlayer');\n\n if (typeof getIntlayerWrapped !== 'function') {\n throw new Error(\n 'Using the import { getIntlayer } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return getIntlayerWrapped(key, localeArg, ...props);\n } catch (error) {\n debug((error as Error).message);\n\n return getIntlayerFunction(key, localeArg, ...props);\n }\n}) as typeof getIntlayerFunction;\n\nexport const getDictionary: typeof getDictionaryFunction = ((\n key: any,\n localeArg?: any,\n ...props: any[]\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n const getDictionaryWrapped = appNamespace.get('getDictionary');\n\n if (typeof getDictionaryWrapped !== 'function') {\n throw new Error(\n 'Using the import { getDictionary } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return getDictionaryWrapped(key, localeArg, ...props);\n } catch (error) {\n debug((error as Error).message);\n\n return getDictionaryFunction(key, localeArg, ...props);\n }\n}) as typeof getDictionaryFunction;\n\nexport const getLocale = (locale?: Locale): Locale => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n return (\n appNamespace.get('locale') ?? locale ?? internationalization.defaultLocale\n );\n } catch (_error) {\n return locale ?? internationalization.defaultLocale;\n }\n};\n\nexport { default as IntlayerMiddleware } from './middleware';\n"],"mappings":";;;;;;;;;AAcA,IAAI,cAAyC;AAE7C,MAAM,gBAAgB,kBAAkB;AACxC,MAAM,EAAE,yBAAyB;AAG/B,IAAI;CACF,MAAM,mBAAiB,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["getIntlayerFunction","getDictionaryFunction"],"sources":["../../src/index.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport { 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 { getLocaleFromStorageServer } from '@intlayer/core/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { StrictModeLocaleMap } from '@intlayer/types/module_augmentation';\nimport { createNamespace } from 'cls-hooked';\n\n// Zero-cost fallback, will be updated with AdonisJS logger or console in dev mode\nlet debug: (message: string) => void = () => {};\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\nif (process.env['NODE_ENV'] === 'development') {\n try {\n const logger = require('@adonisjs/core/services/logger').default;\n debug = (msg: string) => logger.debug(msg);\n } catch {\n debug = (msg: string) => console.debug(msg);\n }\n}\n\nexport const appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n */\nexport const getStorageLocale = (ctx: HttpContext): Locale | undefined =>\n getLocaleFromStorageServer({\n getCookie: (name: string) => ctx.request.cookie(name),\n getHeader: (name: string) => ctx.request.header(name),\n });\n\n/**\n * Translation function for context\n */\nexport const translateFunction =\n (ctx: HttpContext) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n const { locale: currentLocale, defaultLocale } = ctx as unknown as {\n locale: Locale;\n defaultLocale: Locale;\n };\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[\n targetLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Translation function to retrieve content for the current locale.\n *\n * This function works within the request lifecycle managed by the `intlayer` middleware.\n *\n * @param content - A map of locales to content.\n * @param locale - Optional locale override.\n * @returns The translated content.\n *\n * @example\n * ```ts\n * import { t } from 'adonis-intlayer';\n *\n * router.get('/', async () => {\n * const greeting = t({\n * en: 'Hello',\n * fr: 'Bonjour',\n * });\n * return greeting;\n * });\n * ```\n */\nexport const t = <Content = string>(\n content: StrictModeLocaleMap<Content>,\n locale?: Locale\n): Content => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return appNamespace.get('t')(content, locale);\n } catch (error) {\n debug((error as Error).message);\n\n return getTranslation(\n content,\n locale ?? internationalization.defaultLocale\n );\n }\n};\n\nexport const getIntlayer: typeof getIntlayerFunction = ((\n key: any,\n localeArg?: any,\n ...props: any[]\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n const getIntlayerWrapped = appNamespace.get('getIntlayer');\n\n if (typeof getIntlayerWrapped !== 'function') {\n throw new Error(\n 'Using the import { getIntlayer } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return getIntlayerWrapped(key, localeArg, ...props);\n } catch (error) {\n debug((error as Error).message);\n\n return getIntlayerFunction(key, localeArg, ...props);\n }\n}) as typeof getIntlayerFunction;\n\nexport const getDictionary: typeof getDictionaryFunction = ((\n key: any,\n localeArg?: any,\n ...props: any[]\n) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n const getDictionaryWrapped = appNamespace.get('getDictionary');\n\n if (typeof getDictionaryWrapped !== 'function') {\n throw new Error(\n 'Using the import { getDictionary } from \"adonis-intlayer\" is not supported in your environment. Ensure you are within a request context.'\n );\n }\n\n return getDictionaryWrapped(key, localeArg, ...props);\n } catch (error) {\n debug((error as Error).message);\n\n return getDictionaryFunction(key, localeArg, ...props);\n }\n}) as typeof getDictionaryFunction;\n\nexport const getLocale = (locale?: Locale): Locale => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Add the `intlayer` middleware to your kernel before using this function.'\n );\n }\n\n return (\n appNamespace.get('locale') ?? locale ?? internationalization.defaultLocale\n );\n } catch (_error) {\n return locale ?? internationalization.defaultLocale;\n }\n};\n\nexport { default as IntlayerMiddleware } from './middleware';\n"],"mappings":";;;;;;;;;AAcA,IAAI,cAAyC;AAE7C,MAAM,gBAAgB,kBAAkB;AACxC,MAAM,EAAE,yBAAyB;AAG/B,IAAI;CACF,MAAM,mBAAiB,iCAAiC,CAAC;CACzD,SAAS,QAAgB,OAAO,MAAM,IAAI;QACpC;CACN,SAAS,QAAgB,QAAQ,MAAM,IAAI;;AAI/C,MAAa,eAAe,gBAAgB,MAAM;AAElD,gBAAgB,cAAc;;;;AAK9B,MAAa,oBAAoB,QAC/B,2BAA2B;CACzB,YAAY,SAAiB,IAAI,QAAQ,OAAO,KAAK;CACrD,YAAY,SAAiB,IAAI,QAAQ,OAAO,KAAK;CACtD,CAAC;;;;AAKJ,MAAa,qBACV,SAEC,SACA,WACM;CACN,MAAM,EAAE,QAAQ,eAAe,kBAAkB;CAKjD,MAAM,eAAe,UAAU;CAE/B,IAAI,OAAO,YAAY,aACrB,OAAO;CAGT,IAAI,OAAO,YAAY,UACrB,OAAO;CAGT,IACE,OAAO,UACL,kBACI,aAEN,IACE,OAAO,UACL,mBACI,aAEN,OAAO;MAEP,OAAO,eAAe,SAAS,cAAc;CAIjD,OAAO,eAAe,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;AAyBhD,MAAa,KACX,SACA,WACY;CACZ,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,wGACD;EAGH,IAAI,OAAO,aAAa,IAAI,IAAI,KAAK,YACnC,MAAM,IAAI,MACR,iIACD;EAGH,OAAO,aAAa,IAAI,IAAI,CAAC,SAAS,OAAO;UACtC,OAAO;EACd,MAAO,MAAgB,QAAQ;EAE/B,OAAO,eACL,SACA,UAAU,qBAAqB,cAChC;;;AAIL,MAAa,gBACX,KACA,WACA,GAAG,UACA;CACH,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,wGACD;EAGH,MAAM,qBAAqB,aAAa,IAAI,cAAc;EAE1D,IAAI,OAAO,uBAAuB,YAChC,MAAM,IAAI,MACR,2IACD;EAGH,OAAO,mBAAmB,KAAK,WAAW,GAAG,MAAM;UAC5C,OAAO;EACd,MAAO,MAAgB,QAAQ;EAE/B,OAAOA,cAAoB,KAAK,WAAW,GAAG,MAAM;;;AAIxD,MAAa,kBACX,KACA,WACA,GAAG,UACA;CACH,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,wGACD;EAGH,MAAM,uBAAuB,aAAa,IAAI,gBAAgB;EAE9D,IAAI,OAAO,yBAAyB,YAClC,MAAM,IAAI,MACR,6IACD;EAGH,OAAO,qBAAqB,KAAK,WAAW,GAAG,MAAM;UAC9C,OAAO;EACd,MAAO,MAAgB,QAAQ;EAE/B,OAAOC,gBAAsB,KAAK,WAAW,GAAG,MAAM;;;AAI1D,MAAa,aAAa,WAA4B;CACpD,IAAI;EACF,IAAI,OAAO,iBAAiB,aAC1B,MAAM,IAAI,MACR,wGACD;EAGH,OACE,aAAa,IAAI,SAAS,IAAI,UAAU,qBAAqB;UAExD,QAAQ;EACf,OAAO,UAAU,qBAAqB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.mjs","names":["getIntlayer","getIntlayerFunction","getDictionary","getDictionaryFunction"],"sources":["../../src/middleware.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport type { NextFn } from '@adonisjs/core/types/http';\nimport { getConfiguration } from '@intlayer/config/node';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n} from '@intlayer/core/interpreter';\nimport { appNamespace, getStorageLocale, translateFunction } from './index';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * AdonisJS middleware that detects the user's locale and populates the context with Intlayer data.\n */\nexport default class IntlayerMiddleware {\n async handle(ctx: HttpContext, next: NextFn) {\n // Detect if locale is set by intlayer frontend lib in the headers or cookies\n const localeFromStorage = getStorageLocale(ctx);\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // Copy all headers from the request to negotiatorHeaders\n const headers = ctx.request.headers();\n for (const key in headers) {\n const val = headers[key];\n if (typeof val === 'string') {\n negotiatorHeaders[key] = val;\n } else if (Array.isArray(val)) {\n negotiatorHeaders[key] = val.join(',');\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n\n // Decorate context\n (ctx as any).locale = locale;\n (ctx as any).defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(ctx);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = locale as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = locale as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n // Make functions available via CLS\n await appNamespace.runPromise(async () => {\n appNamespace.set('locale', locale);\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n await next();\n });\n }\n}\n"],"mappings":";;;;;AAUA,MAAM,EAAE,yBADc,kBACwB;;;;AAK9C,IAAqB,qBAArB,MAAwC;CACtC,MAAM,OAAO,KAAkB,MAAc;EAE3C,MAAM,oBAAoB,iBAAiB,IAAI;EAE/C,MAAM,oBAA4C,EAAE;EAGpD,MAAM,UAAU,IAAI,QAAQ,SAAS;
|
|
1
|
+
{"version":3,"file":"middleware.mjs","names":["getIntlayer","getIntlayerFunction","getDictionary","getDictionaryFunction"],"sources":["../../src/middleware.ts"],"sourcesContent":["import type { HttpContext } from '@adonisjs/core/http';\nimport type { NextFn } from '@adonisjs/core/types/http';\nimport { getConfiguration } from '@intlayer/config/node';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n} from '@intlayer/core/interpreter';\nimport { appNamespace, getStorageLocale, translateFunction } from './index';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * AdonisJS middleware that detects the user's locale and populates the context with Intlayer data.\n */\nexport default class IntlayerMiddleware {\n async handle(ctx: HttpContext, next: NextFn) {\n // Detect if locale is set by intlayer frontend lib in the headers or cookies\n const localeFromStorage = getStorageLocale(ctx);\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // Copy all headers from the request to negotiatorHeaders\n const headers = ctx.request.headers();\n for (const key in headers) {\n const val = headers[key];\n if (typeof val === 'string') {\n negotiatorHeaders[key] = val;\n } else if (Array.isArray(val)) {\n negotiatorHeaders[key] = val.join(',');\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n\n // Decorate context\n (ctx as any).locale = locale;\n (ctx as any).defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(ctx);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = locale as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = locale as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n // Make functions available via CLS\n await appNamespace.runPromise(async () => {\n appNamespace.set('locale', locale);\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n await next();\n });\n }\n}\n"],"mappings":";;;;;AAUA,MAAM,EAAE,yBADc,kBACwB;;;;AAK9C,IAAqB,qBAArB,MAAwC;CACtC,MAAM,OAAO,KAAkB,MAAc;EAE3C,MAAM,oBAAoB,iBAAiB,IAAI;EAE/C,MAAM,oBAA4C,EAAE;EAGpD,MAAM,UAAU,IAAI,QAAQ,SAAS;EACrC,KAAK,MAAM,OAAO,SAAS;GACzB,MAAM,MAAM,QAAQ;GACpB,IAAI,OAAO,QAAQ,UACjB,kBAAkB,OAAO;QACpB,IAAI,MAAM,QAAQ,IAAI,EAC3B,kBAAkB,OAAO,IAAI,KAAK,IAAI;;EAI1C,MAAM,iBAAiB,eACrB,mBACA,qBAAqB,SACrB,qBAAqB,cACtB;EAED,MAAM,SAAS,qBAAqB;EAGpC,AAAC,IAAY,SAAS;EACtB,AAAC,IAAY,gBAAgB,qBAAqB;EAElD,MAAM,IAAI,kBAAkB,IAAI;EAEhC,MAAMA,iBACJ,KACA,YAAY,QACZ,GAAG,UACAC,YAAoB,KAAK,WAAW,GAAG,MAAM;EAElD,MAAMC,mBACJ,KACA,YAAY,QACZ,GAAG,UACAC,cAAsB,KAAK,WAAW,GAAG,MAAM;EAGpD,MAAM,aAAa,WAAW,YAAY;GACxC,aAAa,IAAI,UAAU,OAAO;GAClC,aAAa,IAAI,KAAK,EAAE;GACxB,aAAa,IAAI,eAAeH,cAAY;GAC5C,aAAa,IAAI,iBAAiBE,gBAAc;GAEhD,MAAM,MAAM;IACZ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "adonis-intlayer",
|
|
3
|
-
"version": "8.9.
|
|
3
|
+
"version": "8.9.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Manage internationalization i18n in a simple way for AdonisJS application.",
|
|
6
6
|
"keywords": [
|
|
@@ -80,20 +80,20 @@
|
|
|
80
80
|
"typecheck": "tsc --noEmit --project tsconfig.types.json"
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
|
-
"@intlayer/chokidar": "8.9.
|
|
84
|
-
"@intlayer/config": "8.9.
|
|
85
|
-
"@intlayer/core": "8.9.
|
|
86
|
-
"@intlayer/types": "8.9.
|
|
83
|
+
"@intlayer/chokidar": "8.9.3",
|
|
84
|
+
"@intlayer/config": "8.9.3",
|
|
85
|
+
"@intlayer/core": "8.9.3",
|
|
86
|
+
"@intlayer/types": "8.9.3",
|
|
87
87
|
"cls-hooked": "4.2.2"
|
|
88
88
|
},
|
|
89
89
|
"devDependencies": {
|
|
90
90
|
"@types/cls-hooked": "4.3.9",
|
|
91
|
-
"@types/node": "25.6.
|
|
91
|
+
"@types/node": "25.6.1",
|
|
92
92
|
"@utils/ts-config": "1.0.4",
|
|
93
93
|
"@utils/ts-config-types": "1.0.4",
|
|
94
94
|
"@utils/tsdown-config": "1.0.4",
|
|
95
95
|
"rimraf": "6.1.3",
|
|
96
|
-
"tsdown": "0.
|
|
96
|
+
"tsdown": "0.22.00",
|
|
97
97
|
"typescript": "6.0.3",
|
|
98
98
|
"vitest": "4.1.5"
|
|
99
99
|
},
|