next-i18next
Version:
The easiest way to translate your NextJs apps.
128 lines (127 loc) • 5.13 kB
text/typescript
import { FlatNamespace, InitOptions, KeyPrefix, Resource, TFunction, i18n } from "i18next";
//#region src/appRouter/types.d.ts
type ResourceLoader = (language: string, namespace: string) => Promise<any>;
interface I18nConfig {
/** Supported languages, e.g. ['en', 'de', 'it'] */
supportedLngs: string[];
/** Default/fallback language */
fallbackLng: string;
/** Default namespace (defaults to 'common') */
defaultNS?: string;
/** All known namespaces (defaults to [defaultNS]) */
ns?: string[];
/** Path to locale files relative to /public (defaults to '/locales') */
localePath?: string;
/** Directory structure pattern (defaults to '{{lng}}/{{ns}}') */
localeStructure?: string;
/** File extension (defaults to 'json') */
localeExtension?: string;
/** Pre-loaded resources (if provided, skips dynamic loading) */
resources?: Resource;
/** Custom resource loader function (overrides localePath) */
resourceLoader?: ResourceLoader;
/** Whether to include locale in URL path (defaults to true) */
localeInPath?: boolean;
/** When true (and localeInPath is true), the default language has no URL prefix.
* e.g. `/about` serves the default language, `/de/about` serves German.
* Requests to the explicit default prefix (`/en/about`) are redirected to `/about`. */
hideDefaultLocale?: boolean;
/** Cookie name for storing selected language (defaults to 'i18next') */
cookieName?: string;
/** Custom header name for passing language to server components (defaults to 'x-i18next-current-language') */
headerName?: string;
/** Cookie max age in seconds (defaults to 365 * 24 * 60 * 60) */
cookieMaxAge?: number;
/** URL path prefixes to ignore in middleware (defaults to ['/api', '/_next', '/static']) */
ignoredPaths?: string[];
/** Base path prefix for middleware to handle (e.g., '/app-router'). When set, the middleware only processes requests under this prefix and locale segments are placed after it. Useful for mixed App Router + Pages Router setups. */
basePath?: string;
/** Extra i18next plugins to use */
use?: any[];
/** Additional i18next init options (merged into init call) */
i18nextOptions?: Omit<InitOptions, 'lng' | 'resources' | 'ns' | 'defaultNS' | 'supportedLngs' | 'fallbackLng'>;
/** Legacy i18n config for Pages Router / next.config.js */
i18n?: {
defaultLocale: string;
locales: string[];
domains?: {
defaultLocale: string;
domain: string;
http?: true;
locales?: string[];
}[];
localeDetection?: false;
};
/** @deprecated Use i18nextOptions instead */
serializeConfig?: boolean;
/**
* Dev-only: when true (and `NODE_ENV !== 'production'`), reload translation
* resources from the backend before each render so edits to locale files
* appear without restarting `next dev`. No effect in production builds.
*/
reloadOnPrerender?: boolean;
/** Support non-explicit language codes like 'en' matching 'en-US' */
nonExplicitSupportedLngs?: boolean;
}
type GetTResult<Ns extends FlatNamespace = FlatNamespace, KPrefix = undefined> = {
t: TFunction<Ns, KPrefix>;
i18n: i18n; /** The resolved language for the current request */
lng: string;
};
//#endregion
//#region src/appRouter/server.d.ts
/**
* Initialize the server-side i18next configuration.
* Call this once in your root layout or a shared setup file.
*/
declare function initServerI18next(userConfig: I18nConfig): void;
/**
* Get a translation function for use in Server Components, layouts, and generateMetadata.
*
* The underlying i18next instance is a **module-level singleton** that persists across
* requests. This means custom backends (i18next-http-backend, i18next-locize-backend, etc.)
* only fetch translations once (or according to their own reloadInterval), not on every request.
*
* @example
* ```tsx
* import { getT } from 'next-i18next/server'
*
* export default async function Page() {
* const { t, i18n } = await getT('home')
* return <h1>{t('heading')}</h1>
* }
* ```
*/
declare function getT<Ns extends FlatNamespace = FlatNamespace, KPrefix extends KeyPrefix<Ns> = undefined>(ns?: Ns | Ns[], options?: {
keyPrefix?: KPrefix;
lng?: string;
}): Promise<GetTResult<Ns, KPrefix>>;
/**
* Extract loaded resources from the server i18next instance for passing to I18nProvider.
*
* @example
* ```tsx
* const { i18n } = await getT()
* const resources = getResources(i18n, ['common', 'footer'])
* return <I18nProvider language={i18n.language} resources={resources}>{children}</I18nProvider>
* ```
*/
declare function getResources(i18n: i18n, namespaces?: string[]): Resource;
/**
* Helper for generateStaticParams — returns params for all supported languages.
*
* @example
* ```tsx
* import { generateI18nStaticParams } from 'next-i18next/server'
*
* export async function generateStaticParams() {
* return generateI18nStaticParams()
* }
* ```
*/
declare function generateI18nStaticParams(): {
lng: string;
}[];
//#endregion
export { generateI18nStaticParams, getResources, getT, initServerI18next };
//# sourceMappingURL=server.d.mts.map