Skip to main content

Client Components

This guide explains how to use the internationalization package with React Client Components in your Next.js application.

Overview

Client Components run in the browser and can include interactive features. To use translations in Client Components, you need to:
  1. Wrap your Client Components with the TranslationProvider in a Server Component
  2. Use the useTranslation hook in your Client Components to access translations

Setting Up the Translation Provider

In a Server Component that renders your Client Component, wrap the Client Component with the TranslationProvider:
The TranslationProvider component:
  • Takes the current locale and dictionary as props
  • Makes translations available to all Client Components within its subtree
  • Uses next-intl under the hood to provide translations

Using the useTranslation Hook

In your Client Components, import and use the useTranslation hook to access translations:
The useTranslation hook:
  • Takes a namespace parameter that specifies which part of the translation object to use
  • Returns a t function that you can use to access translations within that namespace
  • Automatically updates when the locale changes

Namespace Parameter

The namespace parameter in the useTranslation hook helps you access a specific section of your translations:
You can use different namespaces in different components based on the section of translations they need:

Dynamic Values and Formatting

You can include dynamic values in your translations:
For date and number formatting:

Pluralization

You can handle pluralization in your translations:

Creating a Language Switcher

You can create a language switcher component to allow users to change the language:

Best Practices

Split large Client Components into smaller ones and use the appropriate namespace for each component to keep translations organized.
Define types for your translations to get better IDE support:
Always provide fallbacks for translations that might be missing:

Examples

Interactive Form with Validation

Dynamic Content Loading

Next Steps

Adding Languages

Learn how to add support for new languages

Advanced Usage

Advanced internationalization techniques