UNPKG

express-intlayer

Version:

Manage internationalization i18n in a simple way for express application.

1 lines 4.59 kB
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { createModuleAugmentation } from '@intlayer/chokidar';\nimport { type Locales, getConfiguration } from '@intlayer/config';\nimport {\n getTranslation,\n localeDetector,\n type LanguageContent,\n} from '@intlayer/core';\nimport { createNamespace } from 'cls-hooked';\nimport type { NextFunction, RequestHandler, Request, Response } from 'express';\n\nconst { middleware, internationalization } = getConfiguration({\n verbose: true,\n});\nconst { headerName, cookieName } = middleware;\n\nconst appNamespace = createNamespace('app');\n\ncreateModuleAugmentation();\n\nexport const translateFunction =\n (_req: Request, res: Response, _next?: NextFunction) =>\n <T extends string>(\n content: LanguageContent<T> | string,\n locale?: Locales\n ): T => {\n const { locale: currentLocale, defaultLocale } = res.locals as {\n locale: Locales;\n defaultLocale: Locales;\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?.[targetLocale as unknown as keyof LanguageContent<T>] ===\n 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof LanguageContent<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 * Detect locale used by the user and load it into res locale storage\n *\n * @returns\n */\nexport const intlayer = (): RequestHandler => (req, res, next) => {\n // Detect if locale is set by intlayer frontend lib in the cookies\n const localeCookie = req.cookies?.[cookieName];\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeHeader = req.headers?.[headerName];\n // Interpret browser locale\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // // Check if req.headers exists and is an object\n if (req && typeof req.headers === 'object') {\n // Copy all headers from the request to negotiatorHeaders\n for (const key in req.headers) {\n if (typeof req.headers[key] === 'string') {\n negotiatorHeaders[key] = req.headers[key];\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n res.locals.locale_header = localeHeader;\n res.locals.locale_cookie = localeCookie;\n res.locals.locale_detected = localeDetected;\n res.locals.locale = localeCookie ?? localeHeader ?? localeDetected;\n res.locals.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(req, res, next);\n res.locals.t = t;\n\n appNamespace.run(() => {\n appNamespace.set('t', t);\n\n next();\n });\n};\n\nexport const t = <Content = string>(\n content: LanguageContent<Content>,\n locale?: Locales\n): Content => appNamespace.get('t')(content, locale);\n\nexport { LanguageContent };\n"],"mappings":"AAAA,SAAS,gCAAgC;AACzC,SAAuB,wBAAwB;AAC/C;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAS,uBAAuB;AAGhC,MAAM,EAAE,YAAY,qBAAqB,IAAI,iBAAiB;AAAA,EAC5D,SAAS;AACX,CAAC;AACD,MAAM,EAAE,YAAY,WAAW,IAAI;AAEnC,MAAM,eAAe,gBAAgB,KAAK;AAE1C,yBAAyB;AAElB,MAAM,oBACX,CAAC,MAAe,KAAe,UAC/B,CACE,SACA,WACM;AACN,QAAM,EAAE,QAAQ,eAAe,cAAc,IAAI,IAAI;AAKrD,QAAM,eAAe,UAAU;AAE/B,MAAI,OAAO,YAAY,aAAa;AAClC,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,YAAY,UAAU;AAC/B,WAAO;AAAA,EACT;AAEA,MACE,OAAO,UAAU,YAAmD,MACpE,aACA;AACA,QACE,OAAO,UACL,aACF,MAAM,aACN;AACA,aAAO;AAAA,IACT,OAAO;AACL,aAAO,eAAe,SAAS,aAAa;AAAA,IAC9C;AAAA,EACF;AAEA,SAAO,eAAe,SAAS,YAAY;AAC7C;AAOK,MAAM,WAAW,MAAsB,CAAC,KAAK,KAAK,SAAS;AAEhE,QAAM,eAAe,IAAI,UAAU,UAAU;AAE7C,QAAM,eAAe,IAAI,UAAU,UAAU;AAG7C,QAAM,oB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