adonis-intlayer 8.7.11 → 8.7.12

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.
@@ -10,7 +10,7 @@ let cls_hooked = require("cls-hooked");
10
10
  let debug = () => {};
11
11
  const configuration = (0, _intlayer_config_node.getConfiguration)();
12
12
  const { internationalization } = configuration;
13
- if (process.env.NODE_ENV === "development") try {
13
+ if (process.env["NODE_ENV"] === "development") try {
14
14
  const logger = require("@adonisjs/core/services/logger").default;
15
15
  debug = (msg) => logger.debug(msg);
16
16
  } catch {
@@ -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,aAAa,cAC3B,KAAI;CACF,MAAM,SAAS,QAAQ,iCAAiC,CAAC;AACzD,UAAS,QAAgB,OAAO,MAAM,IAAI;QACpC;AACN,UAAS,QAAgB,QAAQ,MAAM,IAAI;;AAI/C,MAAa,+CAA+B,MAAM;8CAElC,cAAc;;;;AAK9B,MAAa,oBAAoB,6DACJ;CACzB,YAA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6IACD;AAGH,SAAO,qBAAqB,KAAK,WAAW,GAAG,MAAM;UAC9C,OAAO;AACd,QAAO,MAAgB,QAAQ;AAE/B,uDAA6B,KAAK,WAAW,GAAG,MAAM;;;AAI1D,MAAa,aAAa,WAA4B;AACpD,KAAI;AACF,MAAI,OAAO,iBAAiB,YAC1B,OAAM,IAAI,MACR,wGACD;AAGH,SACE,aAAa,IAAI,SAAS,IAAI,UAAU,qBAAqB;UAExD,QAAQ;AACf,SAAO,UAAU,qBAAqB"}
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,cAC9B,KAAI;CACF,MAAM,SAAS,QAAQ,iCAAiC,CAAC;AACzD,UAAS,QAAgB,OAAO,MAAM,IAAI;QACpC;AACN,UAAS,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,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9C,OAAO;AACd,QAAO,MAAgB,QAAQ;AAE/B,uDAA6B,KAAK,WAAW,GAAG,MAAM;;;AAI1D,MAAa,aAAa,WAA4B;AACpD,KAAI;AACF,MAAI,OAAO,iBAAiB,YAC1B,OAAM,IAAI,MACR,wGACD;AAGH,SACE,aAAa,IAAI,SAAS,IAAI,UAAU,qBAAqB;UAExD,QAAQ;AACf,SAAO,UAAU,qBAAqB"}
@@ -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;AACzD,UAAS,QAAgB,OAAO,MAAM,IAAI;QACpC;AACN,UAAS,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;CACt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ACd,QAAO,MAAgB,QAAQ;AAE/B,SAAOC,gBAAsB,KAAK,WAAW,GAAG,MAAM;;;AAI1D,MAAa,aAAa,WAA4B;AACpD,KAAI;AACF,MAAI,OAAO,iBAAiB,YAC1B,OAAM,IAAI,MACR,wGACD;AAGH,SACE,aAAa,IAAI,SAAS,IAAI,UAAU,qBAAqB;UAExD,QAAQ;AACf,SAAO,UAAU,qBAAqB"}
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;AACzD,UAAS,QAAgB,OAAO,MAAM,IAAI;QACpC;AACN,UAAS,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;;;;AA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B,QAAQ;AAE/B,SAAOC,gBAAsB,KAAK,WAAW,GAAG,MAAM;;;AAI1D,MAAa,aAAa,WAA4B;AACpD,KAAI;AACF,MAAI,OAAO,iBAAiB,YAC1B,OAAM,IAAI,MACR,wGACD;AAGH,SACE,aAAa,IAAI,SAAS,IAAI,UAAU,qBAAqB;UAExD,QAAQ;AACf,SAAO,UAAU,qBAAqB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adonis-intlayer",
3
- "version": "8.7.11",
3
+ "version": "8.7.12",
4
4
  "private": false,
5
5
  "description": "Manage internationalization i18n in a simple way for AdonisJS application.",
6
6
  "keywords": [
@@ -80,10 +80,10 @@
80
80
  "typecheck": "tsc --noEmit --project tsconfig.types.json"
81
81
  },
82
82
  "dependencies": {
83
- "@intlayer/chokidar": "8.7.11",
84
- "@intlayer/config": "8.7.11",
85
- "@intlayer/core": "8.7.11",
86
- "@intlayer/types": "8.7.11",
83
+ "@intlayer/chokidar": "8.7.12",
84
+ "@intlayer/config": "8.7.12",
85
+ "@intlayer/core": "8.7.12",
86
+ "@intlayer/types": "8.7.12",
87
87
  "cls-hooked": "4.2.2"
88
88
  },
89
89
  "devDependencies": {