Setting Up Internationalization
This guide walks you through the process of setting up internationalization in your Next.js application using the@repo/internationalization
package.
Prerequisites
Before you begin, make sure you have:- A Next.js application using the App Router
- Access to the
@repo/internationalization
package
Installation
Add the internationalization package to your application’s dependencies:Configuration Steps
1. Set Up Middleware
Create or update yourmiddleware.ts
file to include the internationalization middleware:
The matcher pattern excludes static assets and API routes from the internationalization middleware.
2. Create Locale-Based Routes
Structure your application routes to include the locale parameter:3. Create a Root Layout
In yourapp/[locale]/layout.tsx
file, set up the root layout with internationalization support:
4. Set Up Pages with Translations
Create your pages with internationalization support:5. Set Up Client Components (Optional)
If you need translations in client components, create a wrapper for the client component:Verification
To verify that internationalization is working correctly:-
Start your development server:
-
Visit your application with different locale paths:
http://localhost:3000/en
for Englishhttp://localhost:3000/tr
for Turkishhttp://localhost:3000/es
for Spanishhttp://localhost:3000/de
for German
- Check that the content is displayed in the correct language based on the URL path.
Troubleshooting
Missing Translations
If you see untranslated content or errors related to missing translations:- Check that the locale in the URL matches one of the supported locales.
- Verify that the translation key exists in the dictionary file for the current locale.
- Check the browser console for any error messages related to translations.
Middleware Not Working
If the internationalization middleware is not working:- Make sure the matcher pattern in your middleware configuration is correct.
- Check that the middleware is being applied to the correct routes.
- Verify that the middleware is being exported correctly from your
middleware.ts
file.