Repository navigation
feat: port upstream bundle D — dashboard error fix + i18n browser locale auto-detect #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * Pure browser-language detector used to pick an initial locale on first | ||
| * visit, before the user has made an explicit selection (no cookie set). | ||
| * | ||
| * Matching order: | ||
| * 1. Exact match against `navigator.languages` entries (case-insensitive). | ||
| * 2. `zh-HK` / `zh-MO` are treated as `zh-TW` (Traditional Chinese) since | ||
| * OmniRoute does not ship a dedicated Hong-Kong/Macau locale. | ||
| * 3. Language-prefix match — e.g. `en-US` matches a supported `en` locale. | ||
| * 4. No match → `null` (caller should keep the existing default). | ||
| * | ||
| * Kept dependency-free (no DOM/`navigator` access) so it is trivially unit | ||
| * testable and reusable from both client components and future server code. | ||
| */ | ||
| export function detectBrowserLocale( | ||
| languages: readonly string[], | ||
| locales: readonly string[] | ||
| ): string | null { | ||
| if (!languages || languages.length === 0 || !locales || locales.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const normalizedLocales = locales.map((locale) => locale.toLowerCase()); | ||
|
|
||
| for (const rawLanguage of languages) { | ||
| if (!rawLanguage) continue; | ||
| const language = rawLanguage.toLowerCase(); | ||
|
|
||
| // 1. Exact match. | ||
| const exactIndex = normalizedLocales.indexOf(language); | ||
| if (exactIndex !== -1) { | ||
| return locales[exactIndex]; | ||
| } | ||
|
|
||
| // 2. zh-HK / zh-MO fold to zh-TW when zh-TW is supported. | ||
| if (language === "zh-hk" || language === "zh-mo") { | ||
| const zhTwIndex = normalizedLocales.indexOf("zh-tw"); | ||
| if (zhTwIndex !== -1) { | ||
| return locales[zhTwIndex]; | ||
| } | ||
| } | ||
|
|
||
| // 3. Language-prefix match (e.g. "en-US" -> "en"). | ||
| const prefix = language.split("-")[0]; | ||
| const prefixIndex = normalizedLocales.indexOf(prefix); | ||
| if (prefixIndex !== -1) { | ||
| return locales[prefixIndex]; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect } from "react"; | ||
| import { useRouter } from "next/navigation"; | ||
| import { LOCALES, LOCALE_COOKIE } from "@/i18n/config"; | ||
| import type { Locale } from "@/i18n/config"; | ||
| import { detectBrowserLocale } from "@/i18n/detectBrowserLocale"; | ||
| import { persistLocale } from "@/shared/lib/persistLocale"; | ||
|
|
||
| function hasLocaleCookie(): boolean { | ||
| return document.cookie | ||
| .split(";") | ||
| .some((entry) => entry.trim().startsWith(`${LOCALE_COOKIE}=`)); | ||
| } | ||
|
|
||
| /** | ||
| * Auto-detects the browser language on first visit (no locale cookie set | ||
| * yet) and persists it via the same writer `LanguageSelector` uses for a | ||
| * manual selection, then refreshes the router so the server re-renders with | ||
| * the detected locale. Mounted once in the root layout; renders nothing. | ||
| */ | ||
| export function LocaleAutoDetect() { | ||
| const router = useRouter(); | ||
|
|
||
| useEffect(() => { | ||
| if (typeof navigator === "undefined" || hasLocaleCookie()) return; | ||
|
|
||
| const detected = detectBrowserLocale(navigator.languages ?? [navigator.language], LOCALES); | ||
| if (!detected) return; | ||
|
|
||
| persistLocale(detected as Locale); | ||
| router.refresh(); | ||
| }, [router]); | ||
|
|
||
| return null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { LOCALE_COOKIE } from "@/i18n/config"; | ||
| import type { Locale } from "@/i18n/config"; | ||
|
|
||
| /** | ||
| * Persist the locale preference in the cookie `src/i18n/request.ts` reads on | ||
| * the server, plus localStorage as a client-side convenience mirror. | ||
| * | ||
| * Shared by every client-side locale writer (manual selection in | ||
| * `LanguageSelector`, first-visit auto-detection in `LocaleAutoDetect`) so | ||
| * there is a single source of truth for the cookie name/format. | ||
| */ | ||
| export function persistLocale(code: Locale): void { | ||
| document.cookie = `${LOCALE_COOKIE}=${code};path=/;max-age=${365 * 24 * 60 * 60};samesite=lax`; | ||
| try { | ||
| localStorage.setItem(LOCALE_COOKIE, code); | ||
| } catch { | ||
| // Ignore (e.g. storage disabled/full) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { describe, it } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { detectBrowserLocale } from "../../src/i18n/detectBrowserLocale"; | ||
|
|
||
| const SUPPORTED_LOCALES = ["en", "pt-BR", "es", "zh-TW", "fr", "de"] as const; | ||
|
|
||
| describe("detectBrowserLocale", () => { | ||
| it("returns the exact match when a browser language equals a supported locale", () => { | ||
| assert.equal(detectBrowserLocale(["pt-BR"], SUPPORTED_LOCALES), "pt-BR"); | ||
| }); | ||
|
|
||
| it("folds zh-HK to zh-TW when zh-TW is supported", () => { | ||
| assert.equal(detectBrowserLocale(["zh-HK"], SUPPORTED_LOCALES), "zh-TW"); | ||
| }); | ||
|
|
||
| it("folds zh-MO to zh-TW when zh-TW is supported", () => { | ||
| assert.equal(detectBrowserLocale(["zh-MO"], SUPPORTED_LOCALES), "zh-TW"); | ||
| }); | ||
|
|
||
| it("falls back to a language-prefix match when no exact match exists", () => { | ||
| assert.equal(detectBrowserLocale(["en-US"], SUPPORTED_LOCALES), "en"); | ||
| }); | ||
|
|
||
| it("returns null when nothing matches", () => { | ||
| assert.equal(detectBrowserLocale(["ja-JP"], SUPPORTED_LOCALES), null); | ||
| }); | ||
|
|
||
| it("returns null for an empty languages list", () => { | ||
| assert.equal(detectBrowserLocale([], SUPPORTED_LOCALES), null); | ||
| }); | ||
|
|
||
| it("returns null for an empty locales list", () => { | ||
| assert.equal(detectBrowserLocale(["en-US"], []), null); | ||
| }); | ||
|
|
||
| it("tries each browser language in order until one matches", () => { | ||
| assert.equal(detectBrowserLocale(["ja-JP", "fr-CA"], SUPPORTED_LOCALES), "fr"); | ||
| }); | ||
|
|
||
| it("is case-insensitive", () => { | ||
| assert.equal(detectBrowserLocale(["PT-br"], SUPPORTED_LOCALES), "pt-BR"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { readFileSync } from "node:fs"; | ||
| import { dirname, resolve } from "node:path"; | ||
| import { describe, it } from "node:test"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const here = dirname(fileURLToPath(import.meta.url)); | ||
| const source = readFileSync( | ||
| resolve(here, "../../../src/app/(dashboard)/dashboard/HomePageClient.tsx"), | ||
| "utf8" | ||
| ); | ||
|
|
||
| describe("HomePageClient update error rendering", () => { | ||
| it("imports the safe API error extractor", () => { | ||
| assert.match( | ||
| source, | ||
| /import\s*\{\s*extractApiErrorMessage\s*\}\s*from\s*["']@\/shared\/http\/apiErrorMessage["']/, | ||
| "HomePageClient must import extractApiErrorMessage to render API errors safely" | ||
| ); | ||
| }); | ||
|
|
||
| it("funnels the update error body through extractApiErrorMessage", () => { | ||
| assert.match( | ||
| source, | ||
| /notify\.error\(\s*extractApiErrorMessage\(\s*data\s*,/, | ||
| "the update-error notify.error call must use extractApiErrorMessage(data, ...)" | ||
| ); | ||
| }); | ||
|
|
||
| it("does not pass the raw API error object to notify.error", () => { | ||
| assert.doesNotMatch( | ||
| source, | ||
| /notify\.error\(\s*data\.error\b/, | ||
| "notify.error(data.error ...) can render an error envelope object as a React child" | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a supported locale is regional without a bare-language entry, this lookup fails the new first-visit detection path. The live locale list includes
uk-UAandzh-CNbut notukorzh(config/i18n.json), so common browser values likeukorzh-Hans-CNfall through tonullinstead of selecting the available Ukrainian/Simplified Chinese UI, leaving those users on the default locale despite a supported translation being present. Consider also matching supported locales whose primary subtag equalsprefixwhen no bare locale exists.Useful? React with 👍 / 👎.