Repository navigation
fix(book): localize numbers and the token unit in the context window demo - #1408
Sandeepsharmawagle wants to merge 2 commits into
Conversation
…demo
`ContextWindowDemo` formatted every number with `toLocaleString()` and
no locale, and hard-coded the English word "tokens". The book is
translated into 17 languages, but this demo always mixed in English:
a Russian page showed "Контекстное окно: 8,000 tokens".
Because the browser's locale was used, the server-rendered text
(en-US, "8,000") also differed from what a German browser rendered
("8.000"), causing a hydration mismatch.
Format numbers with the page locale from `useLocale()` and use the
existing `book.interactive.tokens` translation.
Renders `ContextWindowDemo` with the real Russian and German book translations and checks that numbers and the token unit follow the page locale rather than the runtime default.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
master3314
left a comment
There was a problem hiding this comment.
Approved in CodeRabbit Change Stack
|
Approving |
Description
The book's Context Window Visualizer (
ContextWindowDemo, used in chapter 1 in every language) had two locale bugs:book.interactive.tokensalready exists and is translated in all 17 locales (for example "токенов" in Russian). It now usest("tokens").toLocaleString()was called without a locale, so numbers matched the visitor's browser instead of the book's language. The server render (en-US, "8,000") also differed from a German browser's client render ("8.000"), which causes a hydration mismatch. Numbers now use the page locale fromuseLocale(), like the other demos in this file already do for their content.Only
ContextWindowDemochanges; no translations or other components are touched.Type of Change
Testing
context-window-demo.test.tsxrenders the demo with the real Russian and German book messages. Both tests fail on the currentmainand pass with this change.npm run lint: 0 errors (no new warnings)npm test: 104 files, 1405 tests passingThis PR fixes locale handling in
ContextWindowDemoinsrc/components/book/elements/demos.tsx. The component now reads the page locale fromuseLocale()and uses it to format numbers. It also translates the token label witht("tokens")instead of hard-coded English.A new test file,
src/__tests__/components/context-window-demo.test.tsx, checks Russian and German rendering with the book's locale messages. The author reports that the new tests fail onmainand pass with this change. The author also reports lint and test runs, but these were not verified here.