Skip to content

fix(web): write numbers and dates in en-US whatever the browser's locale, and lint against locale-less calls - #1549

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/locale-formatting
Oct 26, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/locale-formatting

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

The UI is English-only, but some numbers and dates still followed the browser's locale. A Turkish browser showed 1,232 as "1.232" and dates as "17.10.2026". #1095 added formatNumber (en-US) and converted the Prompts page. This converts the rest of web/src and adds a lint rule so locale-less calls can't come back.

Numbers → formatNumber (the same output for an English browser):

  • Citations;
  • Citations › URL;
  • Insights chart note;
  • prompt detail;
  • Topics tab and topic detail;
  • similar topics;
  • Shopping;
  • the opportunity's word count;
  • the sidebar's tracked count.

Calls that set their own options keep them, with 'en-US' in place of undefined:

  • Shopping's dates, times and prices (and its price helper in lib/actions/shopping.ts);
  • Competitors' dates.

Dates with no options → the helpers the rest of the app uses:

  • formatDate: "Oct 17, 2026";
  • formatDateTime: "Oct 17, 2026, 2:05 PM".

They're used for:

  • Improve tab: "Generated …" and "Sent …";
  • Insights result: "checked at";
  • Action Center basket: "results pending";
  • Team: invitation expiry.

For an English browser these few dates change from "10/17/2026" to "Oct 17, 2026". Both are still read in the browser's time zone, as before. A bad date in the basket shows "—" rather than throwing.

Lint (web/eslint.config.mjs, no-restricted-syntax; src/, tests excluded) flags:

  • toLocaleString() / toLocaleDateString() / toLocaleTimeString() with no locale, undefined or [];
  • an Intl formatter built without one (new Intl.NumberFormat(), Intl.NumberFormat(undefined, …), new Intl.Collator([])…).

Intl.DateTimeFormat() alone stays allowed: lib/time-zone.ts reads the browser's time zone with it. I checked the rule on a probe file: it flagged the 9 locale-less forms and passed the 4 correct ones. Nothing in src/ breaks it after this change.

Related issue

Follow-up to #1095 (no issue).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Switch the browser to Turkish, or simulate it in DevTools:
    const n = Number.prototype.toLocaleString;
    Number.prototype.toLocaleString = function (l, o) { return n.call(this, l ?? 'tr-TR', o); };
    Then switch tabs client-side, without a reload.
  2. Open Citations, a prompt's detail, Topics and the Shopping page. Thousands read "1,232", not "1.232".
  3. Open a prompt's Improve tab after a send: "Sent Oct 17, 2026 · AC-12".
  4. Lint flags a locale-less call:
    cd web && echo "export const x = (1).toLocaleString();" > src/lib/zz.ts && npx eslint src/lib/zz.ts; rm src/lib/zz.ts

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): 0 errors; the 6 warnings were already on main
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run: yarn test (427 files, 4,055 tests) and yarn build.

Test: the Improve tab renders with Date's locale methods defaulting to tr-TR and shows English dates. It fails without the fix and passes in UTC, Asia/Kolkata and Pacific/Auckland. The day and time aren't pinned, since they follow the zone the tests run in.

Pre-push review

A review agent read the change before the push. It found no missed call sites in web/src. Fixed from it:

  • the new test pinned a time that half-hour and +13 zones don't show;
  • the date mocks are now restored in afterEach;
  • the basket's date is guarded;
  • the lint rule now also catches [], calls without new, and other Intl constructors.

Left out of scope: the reviewer noted that a few client-side sorts use localeCompare without a locale. In a Turkish browser they could order labels starting with "I" differently. That's a rare case and not a formatting issue.

🤖 Generated with Claude Code

…ale, and lint against locale-less calls

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@gkhngyk gkhngyk 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.

Looks good, with a follow-up.

What I checked:

  • Numbers. Every toLocaleString() the PR turns into formatNumber gives the same text for an English browser: formatNumber is toLocaleString('en-US') with no options, so the grouping and the default of up to 3 decimals don't change. The calls that keep their own options change only undefined to 'en-US': Shopping's dates, times and prices, formatPrice in lib/actions/shopping.ts, and Competitors' dates. Under en-US their output is the same, including hour: '2-digit' and the 2-decimal prices.
  • Dates. formatDate and formatDateTime (lib/format.ts) take no timeZone, so they read the browser's zone as the old calls did, and no day moves. For an English browser, five lines change:
    • Improve tab "Generated" (prompt-improve-tab.tsx:383) and the Insights result's "Checked" (insights/[id]/page.tsx:213): "10/17/2026, 2:05:33 PM" becomes "Oct 17, 2026, 2:05 PM". The seconds go too; the PR body mentions only the month.
    • Improve tab "Sent" (:389), the basket's "Measured from" (opportunity-basket.tsx:395) and Team's "Expires" (team-section.tsx:385): "10/17/2026" becomes "Oct 17, 2026".
    • These match the format the rest of the app uses. In the basket, a bad date now reads "—" where it read "Invalid Date". The guard is needed because toISOString() throws on an invalid date.
  • Server and client. Every file it touches is a client component whose values arrive after mount (effects or the Improve controller). formatPrice runs in a server action and returns plain data. Nothing new is rendered on the server, so no hydration mismatch is added.
  • The lint rule. I ran its seven selectors, read from the PR's eslint.config.mjs, over a probe and over web/src after merging main.
    • It flags each locale-less form: toLocaleString(), toLocaleDateString() and toLocaleTimeString() (also with ?.), the same calls with undefined or [], and Intl formatters built with no locale, with undefined or with [], with or without new.
    • It leaves alone calls with 'en-US', a locale variable, localeCompare, toLocaleLowerCase, Intl.supportedValuesOf('timeZone') and new Intl.Segmenter('en', …).
    • On the merged tree it finds nothing in 738 source files. The only matches are the two in lib/format.test.ts, which simulate a Turkish browser on purpose, and the config skips tests.
  • Missed spots: none. Each toLocale*String and Intl.* call left in web/src names 'en-US'/'en', or a locale from next-intl's useLocale(), passed directly or through numberFormat, dayLabel or suggestedDate. The one exception is lib/time-zone.ts's Intl.DateTimeFormat().resolvedOptions(). next-intl's useFormatter follows the UI's locale too.
  • Test. The new Improve tab case makes Date's locale methods default to tr-TR and checks the text on screen ("Generated Oct D, 2026, H:MM AM/PM" and "Sent Oct D, 2026 · AC-12"). It fails on the old code. The regexes allow any zone from −12 to +14, and \s accepts the narrow no-break space newer ICU puts before AM/PM. The existing budget tests are unchanged, and restoreAllMocks in afterEach puts the prototypes back.
  • Merge. Main has gained only #1547 (server) since the PR's base. The PR merges cleanly on c58edb8, and the merged web/ tree is the PR's own, so the lint, typecheck and format CI passes at 7cf8b8d stand. None of the open PRs adds a locale-less call, so merging this first doesn't break their lint.
  • Before the API deploy. Web only, with no API dependency. English browsers see the five dates above in the new format, and other browsers see numbers and dates in en-US. No live feature changes.

Follow-up:

  1. The rule's zero-argument Intl case (web/eslint.config.mjs:38, :43). The comment says Intl.DateTimeFormat() alone is allowed for reading the time zone, but:

    • new Intl.DateTimeFormat().resolvedOptions().timeZone, a common form of the same read, is flagged;
    • Intl.DateTimeFormat().format(date) is not flagged, though it formats in the browser's locale.

    One selector in place of the two fixes both. I checked it: it allows only a zero-argument formatter whose resolvedOptions() is read, and it still passes lib/time-zone.ts.

    {
      selector:
        ":matches(NewExpression, CallExpression)[callee.object.name='Intl'][arguments.length=0]:not(MemberExpression[property.name='resolvedOptions'] > .object)",
      message: "Name the locale: pass 'en-US'.",
    },

@gkhngyk
gkhngyk merged commit 751915d into ansvisor:main Oct 26, 2026
6 checks passed
gkhngyk pushed a commit that referenced this pull request Oct 26, 2026
…vedOptions, with or without new (#1549 review) (#1555)

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

2 participants