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/internationalizationpackage
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:
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/enfor Englishhttp://localhost:3000/trfor Turkishhttp://localhost:3000/esfor Spanishhttp://localhost:3000/defor 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.tsfile.
Next Steps
Server Components
Learn how to use translations in server components
Client Components
Learn how to use translations in client components