Skip to content

Add automatic language detection from Accept-Language header#29

Merged
huangyiirene merged 3 commits into
mainfrom
copilot/add-language-auto-detection
Jan 16, 2026
Merged

Add automatic language detection from Accept-Language header#29
huangyiirene merged 3 commits into
mainfrom
copilot/add-language-auto-detection

Conversation

Copilot AI commented Jan 16, 2026

Copy link
Copy Markdown
Contributor

The documentation site hardcoded a redirect to /en/docs, ignoring users' browser language preferences.

Implementation

Modified root page to parse Accept-Language header and redirect to best matching locale:

// Before: hardcoded English
redirect('/en/docs');

// After: respects browser preferences
const headersList = await headers();
const acceptLanguage = headersList.get('accept-language') || '';
const preferredLang = getPreferredLanguage(acceptLanguage, i18n.languages);
redirect(`/${preferredLang}/docs`);

Language Matching Logic

  • Parses quality values (zh-CN;q=0.9,en;q=0.8) and sorts by preference
  • Exact match first (e.g., zh-CN)
  • Falls back to prefix match (e.g., zhzh-CN)
  • Validates quality parameters and filters invalid entries
  • Defaults to English when no match found

Behavior

  • Accept-Language: zh-CN,zh;q=0.9,en;q=0.8/zh-CN/docs
  • Accept-Language: en-US,en;q=0.9/en/docs
  • Accept-Language: fr-FR/en/docs (fallback)
  • No header → /en/docs (fallback)
Original prompt

文档站点默认为英文,应该能默自动识别当前用户的语言并跳转


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@vercel

vercel Bot commented Jan 16, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
docs Ready Ready Preview, Comment Jan 16, 2026 10:17am

Review with Vercel Agent

Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
Copilot AI changed the title [WIP] Add automatic language detection for documentation site Add automatic language detection from Accept-Language header Jan 16, 2026
Copilot AI requested a review from huangyiirene January 16, 2026 10:17
@github-actions github-actions Bot added the ui label Jan 16, 2026
@huangyiirene huangyiirene requested review from Copilot and removed request for huangyiirene January 16, 2026 10:18
@github-actions

Copy link
Copy Markdown

🚀 Preview Deployment

This pull request will be automatically deployed to Vercel.

Preview Links

  • 📝 Documentation: Will be available once Vercel deployment completes
  • 🌍 Languages: English (/en/docs) and Chinese (/zh-CN/docs)

Build Status

Check the CI workflow for build status and any errors.


Automated preview information for PR #29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds automatic language detection to redirect users to documentation in their preferred language based on the Accept-Language HTTP header, replacing the previous hardcoded English redirect.

Changes:

  • Modified root page handler to be async and parse the Accept-Language header
  • Implemented getPreferredLanguage function with quality value parsing and locale matching logic
  • Added fallback behavior to default language when no match is found

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread app/page.tsx
Comment on lines +47 to +49
// Try exact match first
if (availableLanguages.includes(lang.code)) {
return lang.code;

Copilot AI Jan 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case-sensitive comparison may fail to match language codes. The Accept-Language header can contain mixed case (e.g., 'en-US', 'EN-us'), but this performs exact case-sensitive matching. Consider normalizing both the lang.code and availableLanguages entries to lowercase before comparison to ensure reliable matching.

Suggested change
// Try exact match first
if (availableLanguages.includes(lang.code)) {
return lang.code;
// Try exact match first (case-insensitive)
const exactMatch = availableLanguages.find(
available => available.toLowerCase() === lang.code.toLowerCase()
);
if (exactMatch) {
return exactMatch;

Copilot uses AI. Check for mistakes.
Comment thread app/page.tsx
redirect(`/${preferredLang}/docs`);
}

function getPreferredLanguage(acceptLanguage: string, availableLanguages: readonly string[]): string {

Copilot AI Jan 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getPreferredLanguage function lacks test coverage for critical edge cases including empty Accept-Language headers, malformed quality values, case sensitivity handling, and fallback behavior. Consider adding unit tests to verify correct behavior across these scenarios.

Copilot uses AI. Check for mistakes.
@huangyiirene huangyiirene marked this pull request as ready for review January 16, 2026 10:23
@huangyiirene huangyiirene merged commit 709692d into main Jan 16, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants