Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(i18n): format dates in the UI language, not the browser's (#6841)
Bohan Jiang13706d60d03d
diff --git a/packages/views/i18n/index.ts b/packages/views/i18n/index.tsindex 403035109b..b1c8c8a899 100644--- a/packages/views/i18n/index.ts+++ b/packages/views/i18n/index.ts@@ -1,2 +1,3 @@export { useT } from "./use-t";+export { useLocale } from "./use-locale";export { useTimeAgo } from "./use-time-ago";diff --git a/packages/views/i18n/use-locale.ts b/packages/views/i18n/use-locale.tsnew file mode 100644index 0000000000..786c243e5f--- /dev/null+++ b/packages/views/i18n/use-locale.ts@@ -0,0 +1,16 @@+import { useT } from "./use-t";++// The locale to hand to `Intl` / `toLocaleDateString` / `toLocaleString`.+//+// This is the language the member picked for the UI, which is NOT the same+// as `navigator.language`: someone reading a Chinese UI in an English-language+// browser must still get Chinese month and weekday names. Formatting against+// the browser language leaves half a screen in one language and half in+// another, which is exactly the bug this hook exists to prevent.+//+// `resolvedLanguage` is the language i18next actually rendered the strings+// with after fallback, so dates always agree with the text around them.+export function useLocale(): string {+ const { i18n } = useT();+ return i18n.resolvedLanguage ?? i18n.language;+}diff --git a/packages/views/issues/components/gantt-view.test.tsx b/packages/views/issues/components/gantt-view.test.tsxnew file mode 100644index 0000000000..63a1d85d0c--- /dev/null+++ b/packages/views/issues/components/gantt-view.test.tsx@@ -0,0 +1,135 @@+import { createStore } from "zustand/vanilla";+import { describe, expect, it, vi, beforeAll, afterAll } from "vitest";+import { screen } from "@testing-library/react";+import {+ type IssueViewState,+ viewStoreSlice,+} from "@multica/core/issues/stores/view-store";+import { ViewStoreProvider } from "@multica/core/issues/stores/view-store-context";+import type { Issue } from "@multica/core/types";+import { renderWithI18n } from "../../test/i18n";+import { IssueContextMenuProvider } from "../actions";++vi.mock("@tanstack/react-query", async (importOriginal) => ({+ ...(await importOriginal<typeof import("@tanstack/react-query")>()),+ useQuery: () => ({ data: [] }),+}));++vi.mock("@multica/core/hooks", () => ({+ useWorkspaceId: () => "workspace-1",+}));++// The row's start / due range lives in a tooltip. This test is about how the+// dates are formatted, not about hover timing, so render the content inline.+vi.mock("@multica/ui/components/ui/tooltip", () => ({+ Tooltip: ({ children }: { children: React.ReactNode }) => children,+ TooltipTrigger: ({ render }: { render: React.ReactNode }) => render,+ TooltipContent: ({ children }: { children: React.ReactNode }) => children,+}));++vi.mock("../../navigation", () => ({+ AppLink: ({+ children,+ href,+ }: {+ children: React.ReactNode;+ href: string;+ }) => <a href={href}>{children}</a>,+ useNavigation: () => ({ push: vi.fn(), pathname: "/issues" }),+ resolveClickIntent: () => "push",+ useIntentNavigate: () => () => {},+ NavigationProvider: ({ children }: { children: React.ReactNode }) => children,+}));++vi.mock("@multica/core/paths", async (importOriginal) => {+ const actual = await importOriginal<typeof import("@multica/core/paths")>();+ return {+ ...actual,+ useWorkspaceSlug: () => "acme",+ useRequiredWorkspaceSlug: () => "acme",+ useWorkspacePaths: () => actual.paths.workspace("acme"),+ };+});++import { GanttView } from "./gantt-view";++const ISSUE = {+ id: "issue-1",+ identifier: "MUL-1",+ number: 1,+ title: "Ship the thing",+ description: "",+ status: "todo",+ priority: "medium",+ workspace_id: "workspace-1",+ project_id: null,+ parent_issue_id: null,+ assignee_id: null,+ assignee_type: null,+ creator_id: "user-1",+ creator_type: "member",+ labels: [],+ position: 0,+ stage: null,+ start_date: "2026-03-02",+ due_date: "2026-03-06",+ created_at: "2026-03-01T00:00:00Z",+ updated_at: "2026-03-01T00:00:00Z",+} as unknown as Issue;++function renderGantt(locale: "en" | "zh-Hans") {+ const store = createStore<IssueViewState>()(viewStoreSlice);+ return renderWithI18n(+ <ViewStoreProvider store={store}>+ <IssueContextMenuProvider>+ <GanttView issues={[ISSUE]} />+ </IssueContextMenuProvider>+ </ViewStoreProvider>,+ { locale },+ );+}++describe("GanttView date localization", () => {+ beforeAll(() => {+ // The browser speaks English while the UI does not. Dates must follow the+ // UI language, not this — see useLocale in packages/views/i18n.+ vi.stubGlobal("navigator", {+ ...globalThis.navigator,+ language: "en-US",+ languages: ["en-US"],+ });+ vi.useFakeTimers();+ vi.setSystemTime(new Date("2026-03-04T12:00:00Z"));+ });++ afterAll(() => {+ vi.useRealTimers();+ vi.unstubAllGlobals();+ });++ it("formats axis dates in the UI language, not the browser's", () => {+ const { container, unmount } = renderGantt("zh-Hans");++ expect(screen.getByText("2026年3月")).toBeTruthy();+ // Nothing may fall back to the browser language.+ expect(container.textContent ?? "").not.toMatch(/Mar\b/);++ unmount();+ });++ it("formats the row's start / due range in the UI language", () => {+ const { unmount } = renderGantt("zh-Hans");++ const range = screen.getByText(/2026年3月2日/);+ expect(range.textContent).toContain("2026年3月6日");++ unmount();+ });++ it("still formats in English when the UI is English", () => {+ const { container } = renderGantt("en");++ expect(screen.getByText("Mar 2026")).toBeTruthy();+ expect(container.textContent ?? "").not.toMatch(/年/);+ });+});diff --git a/packages/views/issues/components/gantt-view.tsx b/packages/views/issues/components/gantt-view.tsxindex 303b3b2d81..cb3f5d8658 100644--- a/packages/views/issues/components/gantt-view.tsx+++ b/packages/views/issues/components/gantt-view.tsx@@ -23,7 +23,7 @@ import { StatusIcon } from "./status-icon";import { PriorityIcon } from "./priority-icon";import { IssueActionsContextMenu } from "../actions";import { sortIssues } from "../utils/sort";-import { useT } from "../../i18n";+import { useLocale, useT } from "../../i18n";// ---------------------------------------------------------------------------// Date utilities — everything is UTC-day-aligned so a `due_date` ISO string@@ -123,7 +123,7 @@ function GanttAxis({todayOffsetDays: number;width: number;}) {- const locale = typeof navigator !== "undefined" ? navigator.language : "en";+ const locale = useLocale();const totalDays = daysBetween(range.start, range.end);const monthBlocks = useMemo(() => {@@ -316,6 +316,7 @@ function ScheduledRow({totalDays: number;}) {const { t } = useT("issues");+ const locale = useLocale();const p = useWorkspacePaths();const wsId = useWorkspaceId();const { data: projects = [] } = useQuery({@@ -349,7 +350,6 @@ function ScheduledRow({}}- const locale = typeof navigator !== "undefined" ? navigator.language : "en";const fmt = (d: Date) =>d.toLocaleDateString(locale, {month: "short",diff --git a/packages/views/runtimes/components/charts/activity-heatmap.tsx b/packages/views/runtimes/components/charts/activity-heatmap.tsxindex 58326086de..65e30083b7 100644--- a/packages/views/runtimes/components/charts/activity-heatmap.tsx+++ b/packages/views/runtimes/components/charts/activity-heatmap.tsx@@ -2,7 +2,7 @@ import { useMemo } from "react";import type { RuntimeUsage } from "@multica/core/types";import { useCustomPricingStore } from "@multica/core/runtimes/custom-pricing-store";import { addDaysIso, estimateCost, formatUsd, todayIso, weekStartIso } from "../../utils";-import { useT } from "../../../i18n";+import { useLocale, useT } from "../../../i18n";// 26 weeks (~6 months) gives the heatmap real presence in the wider chart// card and turns "long-view" into a meaningful tab — a 13-week strip looked@@ -68,8 +68,8 @@ export function ActivityHeatmap({usage: RuntimeUsage[];tz: string;}) {- const { t, i18n } = useT("runtimes");- const locale = i18n.resolvedLanguage ?? i18n.language;+ const { t } = useT("runtimes");+ const locale = useLocale();const weekdayLabels = useMemo(() => fmtWeekdays(locale), [locale]);// Memo dep — estimateCost (called inside the body below) consults the// user-override store, so saving a custom rate must invalidate the cells.