Server Components
This guide explains how to use the internationalization package with React Server Components in your Next.js application.Overview
Server Components are rendered on the server and can directly access server-side resources. With the internationalization package, you can load translations directly in your Server Components without additional client-side JavaScript.Loading Translations
To use translations in a Server Component, import thegetDictionary
function and use it to load the appropriate dictionary based on the locale:
getDictionary
function:
- Takes a locale string as input
- Loads the corresponding dictionary file
- Returns the dictionary object with all translations for that locale
Accessing Nested Translations
Dictionary files are structured as nested objects. You can access nested translations using dot notation:Working with Arrays
If your translations include arrays, you can map over them to render multiple elements:Handling Dynamic Content
You can combine translations with dynamic content:Fallback Handling
ThegetDictionary
function includes built-in fallback handling. If a translation is not available in the requested locale, it will fall back to English:
Metadata and SEO
You can use translations for page metadata to improve SEO:Passing Translations to Client Components
To use translations in Client Components, you need to pass them from a Server Component:Best Practices
Organize Translations by Feature
Organize Translations by Feature
Structure your translation files by feature or page to make them easier to manage:
Use TypeScript for Type Safety
Use TypeScript for Type Safety
Define types for your translations to get better IDE support and catch errors early:
Avoid String Concatenation
Avoid String Concatenation
Instead of concatenating strings:Use placeholders: