Next.js
This comprehensive guide walks you through integrating Clocale into your Next.js application, with full support for both server and client components in the App Router.
Installation
Adding Dependencies
Install the Clocale React package to get started:
This library provides all the necessary tools for handling translations in your Next.js application, including hooks, providers, and server utilities.
Core Setup
Step 1: Create the Translation Provider
The ClocaleTranslationProvider wraps your entire application and makes translations available to all components. This is a client component that initializes Clocale with the user's locale and pre-fetched server-side translations.
Why this matters: By wrapping your app with this provider, you ensure that all child components can access translations without prop drilling, and translations are available immediately without additional fetching.
Step 2: Set Up Locale Management
Create server actions to handle locale persistence using cookies. This ensures the user's language preference is remembered across sessions:
Key functions:
getLocale()- Retrieves the user's saved locale from cookies (defaults to'en')setLocale(locale)- Updates the locale cookie when users change their language preference
Step 3: Configure the Server Client
Set up the Clocale client for server-side translation fetching. This creates utilities that work seamlessly with Next.js server components:
Configuration details:
baseUrl- Your Clocale integration URL from the developer settings (Learn more)getLocale- Function to retrieve the current localebuilder- Initializes a new Clocale instance for each request with the appropriate locale
Step 4: Configure the Root Layout
Modify your layout.tsx to fetch translations on the server and provide them to your entire application:
Important: Only set
isDev={true}in development. Always useisDev={false}in production to disable development features. Learn more
Building a Language Switcher
Creating the Locale Switcher Component
A language switcher is essential for allowing users to view your application in their preferred language. This reusable component allows users to change their language preference. It uses the setLocale server action and triggers a page refresh to reload translations:
Using the Locale Switcher
Add the locale switcher to your layout or navigation component. Pass the current locale as a prop:
Using Translations
In Server Components
For server components and server actions, use the getTranslation function to access translations:
In Client Components
For client-side components, use the useTranslation hook to access reactive translations:
Quick Reference
| Component Type | Method | Import From | Usage |
|---|---|---|---|
| Server Component | getTranslation() | './path/to/clocale' | await getTranslation('namespace') |
| Client Component | useTranslation() | '@clocale/react' | useTranslation('namespace') |
| Server Action | getLocale() | './path/to/locale' | await getLocale() |
| Server Action | setLocale() | './path/to/locale' | await setLocale('fr') |