npx create-next-app@latest intlayer
npm install intlayer next-intlayer
- declares the diff languages of app
import { Locales, type IntlayerConfig } from "intlayer";
const config: IntlayerConfig = {
internationalisation: {
locales: [
Locales.ENGLISH,
Locales.CHINESE,
// Your other locales
],
defaultLocale: Locales.ENGLISH,
},
};
export default config;- wrap export using
withIntlayer
import type { NextConfig } from "next";
import { withIntlayer } from "next-intlayer/server";
const nextConfig: NextConfig = {
/* config options here */
};
export default withIntlayer(nextConfig);- return just children
import type { PropsWithChildren, FC } from "react";
import "./globals.css";
const RootLayout: FC<PropsWithChildren> = ({ children }) => {
return children;
};
export default RootLayout;- note: export
generateStaticParams-> allows Intlayer to statically generate each of the pages
import type { NextLayoutIntlayer } from "next-intlayer";
import { Inter } from "next/font/google";
import { getHTMLTextDir } from "intlayer";
export { generateStaticParams } from "next-intlayer"; // Line to insert
const inter = Inter({ subsets: ["latin"] });
const LocaleLayout: NextLayoutIntlayer = async ({ children, params }) => {
const { locale } = await params;
return (
<html lang={locale} dir={getHTMLTextDir(locale)}>
<body className={inter.className}>{children}</body>
</html>
);
};
export default LocaleLayout;- add the
IntlayerServerProvider, andIntlayerClientProvider - import
<MyComponent />from src/app/components/ - import
<LocaleSwitcher />from src/app/components/- this is language switching buttons
import type { FC } from "react";
import { type NextPageIntlayer, IntlayerClientProvider } from "next-intlayer";
import { IntlayerServerProvider, useIntlayer } from "next-intlayer/server";
import { MyComponent } from "@/components/MyComponent";
import { LocaleSwitcher } from "@/components/LocaleSwitcher";
const Page: NextPageIntlayer = async ({ params }) => {
const { locale } = await params;
return (
<IntlayerServerProvider locale={locale}>
<IntlayerClientProvider locale={locale}>
<MyComponent />
<LocaleSwitcher />
</IntlayerClientProvider>
</IntlayerServerProvider>
);
};
export default Page;- picks up default locale
export { intlayerMiddleware as middleware } from "next-intlayer/middleware";
export const config = {
matcher:
"/((?!api|static|assets|robots|sitemap|sw|service-worker|manifest|.*\\..*|_next).*)",
};- from this folder:
src/app/components/MyComponent.tsx - in vscode:
CTRL + SHIFT + P->Intlayer create dictionary file (typescript)- this creates:
myComponent.content.ts
- this creates:
- useIntLayer() references the 'key' we created with dictionary - myComponent.content.ts
"use client";
import { useIntlayer } from "next-intlayer";
// CTRL + SHIFT + P: Intlayer create dictionary file (typescript)
("use client");
import Image from "next/image";
import { useIntlayer } from "next-intlayer";
// import { useIntlayer } from "next-intlayer/server"; for server components
import styles from "./MyComponent.module.css";
// CTRL + SHIFT + P: Intlayer create dictionary file (typescript)
export const MyComponent = () => {
const content = useIntlayer("my-component");
return (
<div className={styles.tattoo}>
<Image src={"/clean-arm.jpg"} alt="logo" width="600" height="430" />
<p className={styles["tattoo-word"]}>{content.title}</p>
</div>
);
};- create the content declaration file
- these files can be anywhere in the codebase (
.content.*)
import { t, type Dictionary } from "intlayer";
const myComponentContent = {
key: "my-component",
content: {
title: t({
en: "salmon",
zh: "鮭魚",
}),
},
} satisfies Dictionary;
export default myComponentContent;- add to .gitignore:
.intlayer
- add types generated by intlayer
- "include": [".intlayer/**/*.ts"]
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
".intlayer/**/*.ts"
],
"exclude": ["node_modules"]
}- with 'use client' removed
- update the import path
import { useIntlayer } from "next-intlayer/server
import Image from "next/image";
import { useIntlayer } from "next-intlayer/server"; for server components
import styles from "./MyComponent.module.css";
// CTRL + SHIFT + P: Intlayer create dictionary file (typescript)
export const MyComponent = () => {
const content = useIntlayer("my-component");
return (
<div className={styles.tattoo}>
<Image src={"/clean-arm.jpg"} alt="logo" width="600" height="430" />
<p className={styles["tattoo-word"]}>{content.title}</p>
</div>
);
};