Skip to content

fix: numbers typed are read in the viewer's locale, whole or not at all - #37

Open
pierreneter wants to merge 1 commit into
mainfrom
fix/locale-number-input
Open

pierreneter wants to merge 1 commit into
mainfrom
fix/locale-number-input

Conversation

@pierreneter

Copy link
Copy Markdown
Member

Fixes #17.

Numbers typed by a user are read in the viewer's locale, and the whole input must be a number: a numeric prefix is never accepted.

What changes

  • New locale prop on DocViewer and a locale embed attribute: a BCP 47 tag. Without it, input is read exactly as before (decimal point, comma grouping), and the browser's language is never used implicitly.
  • One parser in src/core/number-input.ts, pure and DOM-free. It takes the separators, group sizes, minus sign, percent sign and digits from Intl.NumberFormat(locale).formatToParts. It is used by:
    • spreadsheet cell input (parseInput): plain numbers, percentages, amounts with a currency sign;
    • the font size box;
    • Page setup margins and column spacing;
    • the Header & footer size.
  • Re-editing a number (formula bar, F2) shows the locale's decimal separator, so 1,5 under de-DE reads back as 1.5, not as text or 15.
  • Collaboration: the set operation carries the canonical locale tag, so every replica and compact read a typed value the same way.
  • Storage and formulas are unchanged: values are canonical OOXML numbers, derived format codes stay canonical, and formula text is not localized.

Rules

The full rules are in docs/features.md#numbers-typed-in-a-locale:

  • Only the locale's grouping character counts, in well-formed groups. Indian 3-then-2 grouping is read where the locale uses it. Space variants (U+0020, U+00A0, U+202F, U+2009) and both apostrophes (for de-CH) are accepted.
  • The other convention's separator is refused, not guessed: 1,5 in en-US, and 1.5 or 1,234.5 in de-DE.
  • A well-formed group is read as grouping: 1.234 in de-DE is 1234, mirroring 1,234 without a locale.
  • Locale digits (Arabic-Indic, Persian, Devanagari, …) are read, but not mixed with ASCII digits in one number.
  • Font size box without a locale: a decimal comma is accepted as in c491ea7 (12,5 → 12.5), since a size needs no grouping. Whole-input validation still applies, so 12pt, 1.2.5 and 1,234 are refused and the box shows the previous size.
  • Page setup: an emptied margin is now refused instead of silently becoming 0 cm.

Relation to #6

The prop uses the same name, type, tag validation and embed attribute as #6. To land #6 on top:

  • remove this branch's private LocaleContext provider in DocViewer;
  • make useLocale = () => useStrings().locale;
  • merge the two locale doc rows.

strings={de} then makes input German as well.

Not covered

  • The grid still shows its number format (1.5); only the editor shows 1,5.
  • Not read in the locale:
    • text pasted from other programs;
    • Find & Replace;
    • dates;
    • a percent sign written before the number (Turkish);
    • multi-letter currencies.
  • Formula-entry localization (out of scope per the issue).
  • Standalone views without DocViewer have no public way to set the input locale until feat(viewer): translatable UI texts: strings table, locale and strings props #6's StringsProvider lands.

Tests

Run in the Linux test VM with Bun 1.4.4. All results below are on the final commit.

New tests:

  • test/number-input.test.tsx:
    • about 80 parser cases across dot and comma locales, spaces, apostrophes, Indian grouping, Arabic digits, signs, exponents, invalid input and prefixes;
    • a round trip of numberText and the parser for about 3,000 values in each of 10 locales;
    • the size box rules;
    • renders of the size box and Page setup.
  • test/xlsx-locale.test.ts:
    • 101 default inputs whose results were captured from main, pinning today's behaviour;
    • de-DE input saved as <v>1.5</v>, reopened and compared;
    • Text-format and percent-format cells;
    • collaboration replay with mixed locales.
  • e2e:
    • xlsx.spec.ts: a cell and the font size box under de-DE and under the default;
    • docx-page.spec.ts: margins under de-DE;
    • embed.spec.ts: the locale attribute, including its removal.

Results:

Run Result
typecheck clean
bun test 1299 pass, 0 fail
e2e/xlsx.spec.ts 44 pass, 0 fail
e2e/docx.spec.ts 23 pass, 0 fail
e2e/docx-page.spec.ts 3 pass, 0 fail
e2e/embed.spec.ts 4 pass, 0 fail
build ok

The viewer, sdk, collab and docx-hf e2e specs passed on the previous revision of this commit.

The new de-DE, collaboration, size-box and embed tests fail on main.

🤖 Generated with Claude Code

…ll (#17)

`locale` (DocViewer prop, `<office-viewer locale>`, `mount()`, the third argument
of `xlsx.set`) is the BCP 47 tag the numbers a person types are read in: cells
(numbers, percentages, amounts), the font size box, the Page setup margins and
column spacing, the Header & footer size. One parser in the core
(src/core/number-input.ts) takes the separators, signs and digits from
Intl.NumberFormat.formatToParts: `1,5` and `1.234,5` are numbers in de-DE.

The whole text is one number or none: a separator that is neither the decimal
one nor a well-formed grouping makes the text no number (`1.5` in de-DE, `1,5`
in en-US), and a prefix is never read (`12pt` keeps the font size it had).
Grouping: the locale's character between groups of its sizes (3, or 3 then 2
in India), every space where it groups with a space, both apostrophes in Swiss
German.

Without a locale every input reads as before (a decimal point, commas by
thousands), whatever the browser's language; the font size box alone still
reads a decimal comma too (`12,5` is 12.5, as since c491ea7), a size needing no
grouping, but not `1,234` or `1,2,5`. Saved values are numbers and formulas are
not localized. A number edited again shows the locale's decimal separator,
which reads back as the same number. A shared cell edit carries the locale, so
every participant and `compact` read it alike.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

Support locale-aware numeric input without silently truncating decimal commas

1 participant