An i18n (internationalization) implementation for NextJS using Lingui.
The logic for detecting which locale to use is in src/app/middleware.ts
.
The locale is chosen based on this order:
- Locale from pathname
- Locale from a request cookie
- Locale from HTTP headers
NextJS's <Link />
, client-side useRouter()
hook and server-side redirect()
function were extended to include the current locale.
For example, you can write the href as <Link href="/about-us" />
and the final href will be /en/about-us
These components can be imported with the following statements:
import { Link } from "@/i18n/link";
import { useRouter } from "@/i18n/client";
import { redirect } from "@/i18n/redirect";
Lingui has tutorials on how to use it's APIs for translating React apps:
- React (Client Components) Tutorial - https://lingui.dev/tutorials/react
- React Server Components Tutorial - https://lingui.dev/tutorials/react-rsc
You can use the following npm scripts to extract and compile messages:
npm run lingui:extract
npm run lingui:compile