Skip to content

Detect default homepage language from browser locale#20

Merged
afc163 merged 5 commits into
mainfrom
copilot/add-default-language-switching
Jul 2, 2026
Merged

Detect default homepage language from browser locale#20
afc163 merged 5 commits into
mainfrom
copilot/add-default-language-switching

Conversation

Copilot AI commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Direct visits to https://afx-team.github.io/ always started in Chinese unless ?lang=en was present. This change makes the homepage choose English or Chinese from the browser locale by default, while preserving explicit ?lang= overrides.

  • Language selection

    • Add initial language resolution to js/i18n.js
    • Keep ?lang= as the highest-priority source
    • Fall back to browser locale detection via navigator.languages / navigator.language
    • Normalize locale values like en-US, en-GB, zh-CN, zh-TW to en / zh
  • Fallback behavior

    • Preserve the existing Chinese default when the query parameter is unsupported
    • Avoid browser-language detection when ?lang= is explicitly provided
  • Coverage

    • Add tests for:
      • English browser default
      • Chinese browser default
      • valid ?lang= taking precedence
      • invalid ?lang= falling back to Chinese
getInitialLanguage() {
  const urlLanguage = this.getLanguageFromURL();

  if (urlLanguage !== null) {
    return this.normalizeLanguage(urlLanguage) || 'zh';
  }

  return this.detectBrowserLanguage();
}

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

✅ Preview is ready!

PR preview ✅ Ready ✅ Ready
🔗 Preview https://afx-team-afx-team-github-io-preview-pr-20.surge.sh
📝 Commit0b4eefa
⏱️ Build time0.006s
📦 Size126.8 KB (+126 B ⬆️) · 14 files
🪵 LogsView logs
📱 MobileScan to open preview on mobile

↩️ Previous: ⚡️ 0b4eefa · afx-team-afx-team-github-io-preview-pr-20.surge.sh (open ↗) · 2026-07-02 08:11:53 UTC

🤖 Powered by surge-preview

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 2, 2026

Copy link
Copy Markdown

Deploying afx-team with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0b4eefa
Status: ✅  Deploy successful!
Preview URL: https://28fe3acf.afx-team.pages.dev
Branch Preview URL: https://copilot-add-default-language.afx-team.pages.dev

View logs

Copilot finished work on behalf of afc163 July 2, 2026 06:23
Copilot AI requested a review from afc163 July 2, 2026 06:23
@afc163 afc163 marked this pull request as ready for review July 2, 2026 07:58
Copilot AI review requested due to automatic review settings July 2, 2026 07:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates the site’s i18n initialization so the homepage default language is derived from the browser locale (when ?lang= is not explicitly provided), and adds automated coverage for the new language-resolution behavior.

Changes:

  • Add language normalization (en-*/zh-*en/zh) plus browser-locale detection in js/i18n.js.
  • Change I18nManager startup to use getInitialLanguage() instead of defaulting to Chinese unconditionally.
  • Extend tests/aria-label-i18n.test.mjs to execute js/i18n.js in a VM and assert default/override behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
js/i18n.js Introduces initial-language resolution (URL override → browser locale fallback) and normalization.
tests/aria-label-i18n.test.mjs Adds VM-based tests covering language defaults and URL ?lang= precedence.

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

Comment thread js/i18n.js
Comment on lines +81 to +86
getInitialLanguage() {
const urlLanguage = this.getLanguageFromURL();

if (urlLanguage !== null) {
return this.normalizeLanguage(urlLanguage) || 'en';
}
Comment on lines +128 to +135
test('invalid lang query ignores browser locale and falls back to English', () => {
const manager = createI18nManager({
search: '?lang=fr',
language: 'zh-CN',
});

assert.equal(manager.currentLang, 'en');
});
Comment thread tests/aria-label-i18n.test.mjs
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@afc163 afc163 merged commit e0b829c into main Jul 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants