Internationalization (i18n)
The codebase supports internationalization, allowing the website to be translated into multiple languages. Understanding this aspect is crucial for supporting multilingual users.
Key Aspects
- i18n setup and configuration
- Language file structure and management
- Translation process and workflows
i18n Setup and Configuration
The i18n functionality is implemented using Next.js’s built-in internationalization features. This framework provides a convenient way to manage translations and handle language switching seamlessly.
The i18n configuration is defined in the app/layout.js
file. This file sets up the supported languages, default language, and the directory structure for translation files.
// app/layout.js
import { locale, locales } from '@/lib/i18n';
export const metadata = {
title: {
default: 'Heli XML',
en: 'Heli XML',
fr: 'Héli XML',
// ... other languages
},
description: {
default: 'A website about Heli XML.',
en: 'A website about Heli XML.',
fr: 'Un site Web sur Heli XML.',
// ... other languages
},
};
export default function RootLayout({ children }) {
return (
<html lang={locale}>
<body>
{children}
</body>
</html>
);
}
Language File Structure and Management
Translations are stored in JSON files within the app/locales
directory. Each language has its own directory, and the files within these directories map to components or sections of the website.
Example:
app/
locales/
en/
common.json
home.json
fr/
common.json
home.json
common.json
{
"hello": "Hello",
"world": "World"
}
home.json
{
"title": "Welcome to the Home Page",
"description": "This is the home page of the website."
}
Translation Process and Workflows
- Identify translatable content: Developers must identify the text that needs to be translated, such as page titles, button labels, and content within components.
- Create translation files: Create corresponding JSON files for each language, adding keys and values for the translatable content.
- Use translation functions: Utilize the
useTranslation
hook provided by Next.js to access translations within components.
Example:
// components/HomePage.js
import { useTranslation } from 'next-i18next';
export default function HomePage() {
const { t } = useTranslation('common');
return (
<div>
<h1>{t('hello')} {t('world')}</h1>
</div>
);
}
Conclusion
The codebase leverages Next.js’s internationalization features to provide a robust and scalable solution for supporting multiple languages. Developers should understand the i18n setup, the structure of language files, and the process for translating content.