@flotiq/nextjs-internationalization
v1.0.1
Published
Support for internationalization in Next.JS framework
Downloads
10
Readme
@flotiq/nextjs-internationalization
Lightweight internationalization helpers for Next.js applications.
This package provides:
- A middleware that redirects requests to language-prefixed paths (for example
/en/...) based on theAccept-Languageheader. - A helper function
_translatethat applies translations from an object's__translationsarray onto the object recursively.
Installation
Install the package in your Next.js project:
npm install @flotiq/nextjs-internationalizationRequirements
- Next.js: 15.0.0 or higher
- @formatjs/intl-localematcher: 0.6.2 or higher
- negotiator: 1.0.0 or higher
Middleware: automatic language redirect
Use createInternationalizationMiddleware in your Next.js middleware to automatically redirect users to a path prefixed with the chosen locale.
Create a middleware.ts file and use internationalization helper:
import { createInternationalizationMiddleware } from "@flotiq/nextjs-internationalization";
export async function middleware(request: Request) {
const locales = ["en", "pl"];
const defaultLocale = "en";
return createInternationalizationMiddleware(request, locales, 'pl');
}How it behaves:
- If the request path already starts with one of the provided
locales(for example/pl), no redirect is performed. - Otherwise it picks the best matching locale from the
Accept-Languageheader usingnegotiatorand@formatjs/intl-localematcher, then returns a redirect to the same path prefixed with the chosen locale.
Helper: _translate
_translate applies translations embedded in an object. The object can include a __translations array containing translation objects with a __language property. The helper will merge matching translation properties into the base object recursively.
Example:
import { _translate } from "@flotiq/nextjs-internationalization";
const item = {
title: "Hello",
content: "Default content",
__translations: [
{ __language: "pl", title: "Cześć", content: "Treść po polsku" }
]
};
const translated = _translate(item, "pl");
// translated.title === 'Cześć'The helper walks nested objects and arrays recursively and applies translations where available.
