Translating page metadata
Setting localized <title> and <description> tags
Problem
A user views a page in Spanish, and all the visible content is correctly translated. However, the browser tab and the search engine result snippet still show the English title and description. This metadata mismatch creates a confusing user experience and harms SEO by presenting irrelevant information in search.
Solution
Use the Next.js generateMetadata function within your pages and layouts. This server-side function can load the correct translations based on the lang parameter (using the same dictionary-loading logic as your components) and return a dynamic metadata object with the localized title and description.
Steps
1. Create a function to load dictionaries
You need a way to load your flat translation files on the server. Create a helper function for this.
// app/get-dictionary.ts
import 'server-only';
type Messages = Record<string, string>;
const dictionaries: { [key: string]: () => Promise<Messages> } = {
en: () => import('@/dictionaries/en.json').then((module) => module.default),
es: () => import('@/dictionaries/es.json').then((module) => module.default),
};
export const getDictionary = async (lang: string) => {
const load = dictionaries[lang];
if (load) {
return load();
}
return dictionaries.en();
};
2. Define metadata on a page
In your page file (e.g., app/[lang]/about/page.tsx), export an async function called generateMetadata. Next.js will automatically call this when rendering the page.
// app/[lang]/about/page.tsx
import { getDictionary } from '@/app/get-dictionary';
import type { Metadata } from 'next';
type Props = {
params: { lang: string };
};
// This function generates metadata
export async function generateMetadata({ params }: Props): Promise<Metadata> {
// Load the dictionary for this page
const dict = await getDictionary(params.lang);
return {
title: dict['about.title'], // e.g., "About Us" or "Sobre Nosotros"
description: dict['about.description'],
};
}
// The rest of your page component
export default function AboutPage() {
return (
<div>
{/* Page content */}
<h1>...</h1>
</div>
);
}
3. Set a title template in the root layout
To avoid repeating your site name in every title, you can set a template in your root layout.
// app/[lang]/layout.tsx
import { getDictionary } from '@/app/get-dictionary';
import type { Metadata } from 'next';
type Props = {
params: { lang: string };
children: React.ReactNode;
};
// You can generate metadata in layouts too
export async function generateMetadata({ params }: Props): Promise<Metadata> {
const dict = await getDictionary(params.lang);
return {
// This provides a base title and a template
title: {
default: dict['site.name'], // e.g., "My Awesome Site"
template: `%s | ${dict['site.name']}`, // e.g., "About Us | My Awesome Site"
},
description: dict['site.description'],
};
}
export default async function RootLayout({ children, params }: Props) {
// ... rest of your layout (loading providers, etc.)
return (
<html lang={params.lang}>
<body>{children}</body>
</html>
);
}