Skip to content

fix(book): localize numbers and the token unit in the context window demo - #1408

Open
Sandeepsharmawagle wants to merge 2 commits into
f:mainfrom
Sandeepsharmawagle:fix/book-context-window-locale
Open

Sandeepsharmawagle wants to merge 2 commits into
f:mainfrom
Sandeepsharmawagle:fix/book-context-window-locale

Conversation

@Sandeepsharmawagle

@Sandeepsharmawagle Sandeepsharmawagle commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

The book's Context Window Visualizer (ContextWindowDemo, used in chapter 1 in every language) had two locale bugs:

  1. Hard-coded English unit. The word "tokens" was written into the JSX, although book.interactive.tokens already exists and is translated in all 17 locales (for example "токенов" in Russian). It now uses t("tokens").
  2. Numbers followed the browser, not the page. 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 from useLocale(), like the other demos in this file already do for their content.
Page Before After
/ru book Контекстное окно: 8,000 tokens Контекстное окно: 8 000 токенов
/de book Kontextfenster: 8,000 tokens Kontextfenster: 8.000 Tokens

Only ContextWindowDemo changes; no translations or other components are touched.

Type of Change

  • Bug fix
  • Documentation update
  • Other (please describe):

Testing

  • New context-window-demo.test.tsx renders the demo with the real Russian and German book messages. Both tests fail on the current main and pass with this change.
  • npm run lint: 0 errors (no new warnings)
  • npm test: 104 files, 1405 tests passing

This PR fixes locale handling in ContextWindowDemo in src/components/book/elements/demos.tsx. The component now reads the page locale from useLocale() and uses it to format numbers. It also translates the token label with t("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 on main and pass with this change. The author also reports lint and test runs, but these were not verified here.

…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.
@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

ContextWindowDemo now formats displayed token counts with the active locale and uses a translated token label. Tests check Russian rendering and German thousands formatting.

Changes: Context window localization

Layer / File(s) Summary
Locale-aware formatting and tests
src/components/book/elements/demos.tsx, src/__tests__/components/context-window-demo.test.tsx
ContextWindowDemo formats its displayed values using the active locale and translates the token label. Tests check Russian labels and values, and German thousands formatting.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to 24617

This change localizes the token label and number formatting in the context window demo. It is narrowly scoped and carries little risk. It is ready to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the two changes in the ContextWindowDemo: number formatting and the token unit label. Both match the summary and objectives: the hard-coded English 'tokens' is replaced with a translat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the two changes in the ContextWindowDemo: number formatting and the token unit label. Both match the summary and objectives: the hard-coded English 'tokens' is replaced with a translation, and numbers use the page locale instead of the browser default. The title is specific and follows the conventional-commit style. It does not list the new test file, which is acceptable because the title covers the primary change.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 64f7f674-be47-430a-963a-5cde0b06e550
Commits

Reviewing files that changed from the base of the PR and between c3e7767 and 2461700.

Files selected for processing: 2

  • src/__tests__/components/context-window-demo.test.tsx
  • src/components/book/elements/demos.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@master3314 master3314 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sandeepsharmawagle

Copy link
Copy Markdown
Contributor Author

Approving

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.

2 participants