Issue Description:
I am encountering an infinite redirection bug in my Next.js application, which is caused by a language code parsing issue. The application uses the external library, "next-international," to handle internationalization.
Reproduction Steps:
Set up a Next.js application with internationalization using the "next-international" library.
Use language codes containing a hyphen ("-"), e.g., "en-US" and "fr-FR," to represent different locales.
Expected Behavior:
The application should successfully identify and use the correct locale based on the "Accept-Language" header or the "Next-Locale" cookie.
Actual Behavior:
The "negotiateAcceptLanguage" function does not correctly parse the "Accept-Language" header when the language codes contain a hyphen ("-"). As a result, it retrieves only the first part of the language code and ignores the region-specific part (e.g., "fr" instead of "fr-FR").
Issue Analysis:
The root cause of the bug lies in the "negotiateAcceptLanguage" function's splitting logic for the "Accept-Language" header. When the function attempts to split the header by a comma (","), it only retrieves the first part before the hyphen ("-"). This leads to incorrect locale identification and may cause an infinite redirection loop when handling internationalized routes.
Code Snippet with the Issue:
// File: src/app/middleware/index.ts
function negotiateAcceptLanguage(request: NextRequest) {
const header = request.headers.get('Accept-Language');
console.log(header);
const locale = header?.split(',')?.[0]; // Incorrect splitting logic for language codes with hyphen ("-")
return locale ?? null;
}
// ... Rest of the code ...
Proposed Solution:
To resolve the infinite redirection issue, the library's parsing logic for language codes should be modified to handle hyphens ("-") correctly. This can be achieved by using a different approach to split the "Accept-Language" header, allowing it to retrieve the complete language code, including the region-specific part.
Additional Information:
Next.js version: 13.4.12
next-international version: ^0.8.1
If it's good for you, I'll be happy to help you by doing the PR, indeed, this would be one of my first contributions to an open source project, especially since I really appreciate your project, because it taught me a lot about how the core of next.js works and it saved me a lot of time.
In all cases, I hope this information helps in resolving the issue. Please let me know if you need any further details or assistance with the bug fix. Thank you!
Best regards,