Overview
The framework provides out-of-the-box internationalization (i18n) support, fully integrated with Strapi. It relies onnext-intl to handle server-side translations and routing, ensuring that your application is accessible in multiple languages with minimal configuration.
How It Works
The i18n system operates through two main server-side components that communicate directly with your Strapi backend.Middleware configuration
Thesrc/middleware.ts file acts as the entry point for every request. Its primary role is to determine the user’s locale based on the URL path.
- Fetching Locales: It dynamically fetches the list of available locales from Strapi (
/api/littlebox-strapi-suite/modules/locales). - Routing: It checks if the current path starts with a valid locale (e.g.,
/en/about). - Cookie Management: If a locale is detected, it proceeds; otherwise, it assigns the default locale and sets the
NEXT_LOCALEcookie.
src/middleware.ts
Cookie Synchronization
TheNEXT_LOCALE cookie serves as the source of truth for the user’s preferred language across the session. The framework ensures your application stays in sync with this cookie through the BaseLayout component (src/app/core/components/base-layout.tsx).
This component actively monitors the NEXT_LOCALE cookie. If it detects a change in the cookie’s value (for example, after a redirection or a manual set), it automatically triggers a router.refresh(). This action ensures that the server-side components re-render with the correct locale data without requiring a full page reload, maintaining a smooth user experience.
Loading Translations
Thesrc/i18n/request.ts file is responsible for populating the translation messages for the determined locale.
It automatically fetches translations from Strapi (/api/littlebox-strapi-suite/modules/translations), specifically requesting data for the active language. The system then maps these translations by their unique identifier (uid), enabling you to reference them easily in your components.
Displaying Translations
Once translations are loaded, you can display them in your components using theuseTranslations hook from next-intl.
Basic Usage
-
Import the hook:
-
Initialize the hook: Call
useTranslations()inside your component to get the translation function (commonly namedt).In this example,"Page not found"serves as the key to look up the translation. If a translation is found for the current locale, it will be displayed; otherwise, it may fall back to the key itself or the default locale’s value depending on configuration.
Managing Locales Client-Side
You can control language settings and navigation within your components using theuseLocale hook. This hook exposes the LocaleContext, providing methods to switch languages and retrieve locale-specific data.
Using the Hook
To access the locale functions, import the hook from the core context:Changing the Language
Use theset method to change the current language. This function handles the redirection logic, sending the user to the translated version of the current page if it exists, or falling back to the target language’s homepage.
Listening to Changes
Since the language state is managed via the URL and React Context, components wrapped in theLocaleProvider will automatically re-render when the locale changes. You can access the current active locale via the current property.
Getting Available Languages
To retrieve the list of all active languages (e.g., to render a language switcher), use theget method:
Links to Homepages
If you need to programmatically link to the homepage of a specific locale, use thegetHomepageLink method. This is crucial because of the Home page slug strategy configured in the backend (see the Slug documentation).
The homepage URL isn’t always just the locale prefix; it depends on which document is set as home and the chosen strategy (e.g., identifying by language or by content content). getHomepageLink abstracts this complexity, ensuring you always direct users to the correct root path for that language.