Getting Started
Let's see how you can use the NextGlobeGen package to setup i18n for your Next.js app.
If you haven't done so already, create a new Next.js app that uses the App Router.
Installation
Next.js 15
A version of the package which is compatible with Next.js 15 is released under the latest
distribution tag. To install this version of the package, run the following command.
- npm
- Yarn
- pnpm
npm install next-globe-gen@latest
yarn add next-globe-gen@latest
pnpm add next-globe-gen@latest
Next.js 14
A version of the package which is compatible with Next.js 14 is released under next-14
distribution tag. To install this version of the package, run the following command.
- npm
- Yarn
- pnpm
npm install next-globe-gen@next-14
yarn add next-globe-gen@next-14
pnpm add next-globe-gen@next-14
Setup
To setup the package we will be creating the following file structure. The default directory and file locations can be altered by configuration, but let's use the defaults for now.
.
├── i18n.config.ts 1)
├── next.config.ts 2)
└── src
├── messages 3)
│ ├── en.json
│ └── fi.json
├── _app 4)
| ├── dashboard
│ │ ├── i18n.ts 5)
│ │ └── page.tsx
│ ├── LanguageSwitcher.tsx
│ ├── layout.tsx
│ └── page.tsx
├── app
└── middleware.ts 6)
1) Configuration
Create an i18n.config.ts
file to the application root directory and export your configuration as a default export. At least the supported locales and the default locale needs to be configured.
import type { Config } from "next-globe-gen";
const config: Config = {
locales: ["en", "fi"],
defaultLocale: "en",
};
export default config;
2) Plugin
Enable the NextGlobeGen plugin in the Next.js configuration file.
- next.config.ts
- next.config.mjs
- next.config.js
import type { NextConfig } from "next";
import createNextGlobeGenPlugin from "next-globe-gen/plugin";
const withNextGlobeGen = createNextGlobeGenPlugin();
const nextConfig: NextConfig = {
/* Next.js config options here */
};
export default withNextGlobeGen(nextConfig);
// @ts-check
import createNextGlobeGenPlugin from "next-globe-gen/plugin";
const withNextGlobeGen = createNextGlobeGenPlugin();
/** @type {import('next').NextConfig} */
const nextConfig: NextConfig = {
/* Next.js config options here */
};
export default withNextGlobeGen(nextConfig);
// @ts-check
const createNextGlobeGenPlugin = require("next-globe-gen/plugin");
const withNextGlobeGen = createNextGlobeGenPlugin();
/** @type {import('next').NextConfig} */
const nextConfig: NextConfig = {
/* Next.js config options here */
};
module.exports = withNextGlobeGen(nextConfig);
3) Messages
Create message translations to the src/messages
directory. There should be one <locale>.json
file for each configured locale
.
- Messages can be divided to namespaced
<locale>/<namespace>.json
files. - The package also supports messages in YAML files using
.yaml
or.yml
extensions.
- en.json
- fi.json
{
"title": "Homepage",
"description": "{name} package provides seamless internationalization experience for Next.js App Router devs.",
"dashboard": {
"title": "Dashboard",
"projects": "You have {count, plural, =0 {no projects} =1 {<b>one</b> project} other {<b>#</b> projects}}."
}
}
{
"title": "Kotisivu",
"description": "{name} paketti tarjoaa saumattoman kansainvälistämiskokemuksen Next.js App Router kehittäjille.",
"dashboard": {
"title": "Hallintapaneeli",
"projects": "Sinulla {count, plural, =0 {ei ole projekteja} =1 {on <b>yksi</b> projekti} other {on <b>#</b> projektia}}."
}
}
4) Routing
Create or move your Next.js file-system based routing files into the _app
directory from the app
directory. All new routes should be created and modified in the _app
directory.
NextGlobeGen APIs can be used to handle navigation and rendering translations.
- RootLayout
- Home
- Dashboard
- LanguageSwitcher
import { Metadata } from "next";
import { Link, useLocale, useTranslations } from "next-globe-gen";
import { ReactNode } from "react";
import LanguageSwitcher from "./LanguageSwitcher";
export const metadata: Metadata = {
title: { template: "%s | NextGlobeGen", default: "NextGlobeGen" },
};
export default function RootLayout({ children }: { children: ReactNode }) {
const locale = useLocale();
const t = useTranslations();
return (
<html lang={locale}>
<body>
<header>
<LanguageSwitcher />
<nav>
<ul>
<li>
<Link href="/">{t("title")}</Link>
</li>
<li>
<Link href="/dashboard">{t("dashboard.title")}</Link>
</li>
</ul>
</nav>
</header>
<main>{children}</main>
</body>
</html>
);
}
import type { Metadata } from "next";
import { getTranslations, useTranslations } from "next-globe-gen";
export function generateMetadata(): Metadata {
const t = getTranslations();
return { description: t("description", { name: "NextGlobeGen" }) };
}
export default function Home() {
const t = useTranslations();
return (
<section>
<h1>{t("title")}</h1>
<p>{t("description", { name: "NextGlobeGen" })}</p>
</section>
);
}
import type { Metadata } from "next";
import { getTranslations, useTranslations } from "next-globe-gen";
export function generateMetadata(): Metadata {
const t = getTranslations("dashboard");
return { title: t("title") };
}
export default function Dashboard() {
const t = useTranslations("dashboard");
return (
<section>
<h1>{t("title")}</h1>
<p>{t("projects", { count: 1, b: (children) => <b>{children}</b> })}</p>
</section>
);
}
"use client";
import { Link, useRoute } from "next-globe-gen";
/**
* If there is dynamic route segments in some of the application routes (i.e. "/images/[id]"),
* the params provided by Next.js useParams function have to be passed as a prop to
* Link components for language switching to work properly.
*/
export default function LanguageSwitcher() {
const route = useRoute();
return (
<nav>
<ul>
<li>
<Link href={route} locale="en">
In English
</Link>
</li>
<li>
<Link href={route} locale="fi">
Suomeksi
</Link>
</li>
</ul>
</nav>
);
}
5) Localized pathnames (optional)
In NextGlobeGen pathnames are localized by translating each route segment (directory) separately using i18n.ts
file convention.
The simplest way to add the localizations is to export an object with localized versions of the route segment for each locale as a default export.
const segmentTranslations = {
en: "dashboard", // If omitted, the directory name is used
fi: "hallintapaneeli",
};
export default segmentTranslations;
With these translations, the Finnish page will be served from /fi/hallintapaneeli
path.
6) Middleware (optional)
Add middleware to the application by exporting NextGlobeGen middleware
from src/middleware.ts
file. The middleware handles locale negotiation and adds alternate links of the page to the response headers.
export { middleware } from "next-globe-gen/middleware";
export const config = {
// Matcher ignoring next internals and static assets
matcher: ["/((?!_next|.*\\.).*)"],
};
Running the App
After the setup has been done, start the Next.js development server and enjoy the seamless internationalization experience.
- npm
- Yarn
- pnpm
npm run dev
yarn dev
pnpm run dev
The NextGlobeGen plugin generates the required files for the app so that the routes can be served in the configured locales. It also re-generates the required files on the fly as long as the Next.js development server is running.
Following directories and files are generated by the NextGlobeGen plugin.
src
├── .next-globe-gen
├── .next-globe-gen.d.ts
└── app
└── (i18n)
├── en
│ ├── dashboard
│ │ └── page.tsx
│ ├── layout.tsx
│ └── page.tsx
└── fi
├── hallintapaneeli
│ └── page.tsx
├── layout.tsx
└── page.tsx
Do not modify the generated files yourself. The package re-generates the files when necessary. Create and modify your routes in the _app
directory.
You can inspect what each generated file has eaten, if you would like to know how the package works under the hood.
What Next?
- NextGlobeGen Playground: Check out the example app.
- Configuration: Check out how you can modify the default functionality.
- API Reference: See everything this package is capable of.