UNPKG

express-intlayer

Version:

Manage internationalization i18n in a simple way for express application.

54 lines 1.99 kB
import { getDictionary as getDictionary$1, getIntlayer as getIntlayer$1 } from "@intlayer/core/interpreter"; import { Locale } from "@intlayer/types/allLocales"; import { StrictModeLocaleMap } from "@intlayer/types/module_augmentation"; import { NextFunction, Request, RequestHandler, Response } from "express"; //#region src/index.d.ts declare const translateFunction: (_req: Request, res: Response, _next?: NextFunction) => <T extends string>(content: StrictModeLocaleMap<T> | string, locale?: Locale) => T; /** * Express middleware that detects the user's locale and populates `res.locals` with Intlayer data. * * It performs: * 1. Locale detection from cookies, headers, or default settings. * 2. Injects `t`, `getIntlayer`, and `getDictionary` functions into `res.locals`. * 3. Sets up a `cls-hooked` namespace for accessing these functions anywhere in the request lifecycle. * * @returns An Express middleware function. * * @example * ```ts * import express from 'express'; * import { intlayer } from 'express-intlayer'; * * const app = express(); * app.use(intlayer()); * ``` */ declare const intlayer: () => RequestHandler; /** * Translation function to retrieve content for the current locale. * * This function works within the request lifecycle managed by the `intlayer` middleware. * * @param content - A map of locales to content. * @param locale - Optional locale override. * @returns The translated content. * * @example * ```ts * import { t } from 'express-intlayer'; * * app.get('/', (req, res) => { * const greeting = t({ * en: 'Hello', * fr: 'Bonjour', * }); * res.send(greeting); * }); * ``` */ declare const t: <Content = string>(content: StrictModeLocaleMap<Content>, locale?: Locale) => Content; declare const getIntlayer: typeof getIntlayer$1; declare const getDictionary: typeof getDictionary$1; //#endregion export { getDictionary, getIntlayer, intlayer, t, translateFunction }; //# sourceMappingURL=index.d.ts.map