Skip to content

feat(web-core): implement locale support and fix pluralize#1427

Merged
gspencergoog merged 6 commits into
google:mainfrom
gspencergoog:fix_pluralize
May 14, 2026
Merged

feat(web-core): implement locale support and fix pluralize#1427
gspencergoog merged 6 commits into
google:mainfrom
gspencergoog:fix_pluralize

Conversation

@gspencergoog
Copy link
Copy Markdown
Collaborator

@gspencergoog gspencergoog commented May 13, 2026

Description

This PR adds locale support to DataContext and SurfaceModel in web-core, and updates pluralize, formatNumber, and formatCurrency to use the resolved locale instead of hardcoding 'en-US'. It also includes removing .passthrough() from PluralizeApi and adding tests for Welsh locale.

Changes

  • surface-model.ts: Added optional locale parameter to SurfaceModel constructor.
  • data-context.ts: Added locale getter to DataContext that delegates to surface.locale.
  • basic_functions.ts: Updated PluralizeImplementation, FormatNumberImplementation, and FormatCurrencyImplementation to use context.locale.
  • basic_functions_api.ts: Removed .passthrough() from PluralizeApi schema for stricter validation.
  • basic_functions.test.ts: Updated createTestDataContext to support passing a locale, and added tests for pluralize with the Welsh locale (cy) covering all 6 categories.
  • CHANGELOG.md: Added entries under "Unreleased".

Impact & Risks

  • Low Risk: The locale parameter is optional and defaults to undefined. If not provided, the Intl APIs will fall back to the runtime's default locale, maintaining current behavior for existing usage.

Testing

  • Added new unit tests for Welsh pluralization in basic_functions.test.ts.
  • Verified all changes by running npm run test in renderers/web_core. All 258 tests passed.

Fixes #1385

- Add optional `locale` to `SurfaceModel` and `DataContext`.
- Update `pluralize`, `formatNumber`, and `formatCurrency` to use context locale instead of hardcoded 'en-US'.
- Remove `.passthrough()` from `PluralizeApi` schema.
- Add tests for pluralize with Welsh locale covering all categories.
gemini-code-assist[bot]

This comment was marked as resolved.

- Cache `Intl.NumberFormat` and `Intl.PluralRules` instances to improve efficiency.
- Add try-catch block to `FormatNumberImplementation` to prevent crashes on invalid locales, falling back to `toFixed` or `String`.
- Add try-catch block to `PluralizeImplementation` for safety.
gemini-code-assist[bot]

This comment was marked as resolved.

…tions.ts

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Copy link
Copy Markdown
Collaborator

@ditman ditman left a comment

Choose a reason for hiding this comment

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

Minor nitpicks. We should have an issue (if it doesn't exist already) to track improving the types of the arguments to function calls?

Comment thread renderers/web_core/src/v0_9/state/surface-model.ts
Comment thread renderers/web_core/src/v0_9/rendering/data-context.ts
Comment thread renderers/web_core/CHANGELOG.md
- Add tests for values 0 and 1 falling back to 'other' when specific categories are missing.
@gspencergoog gspencergoog merged commit 83a395c into google:main May 14, 2026
23 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in A2UI May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

pluralize hard-codes en-US and renderers are inconsistent

2 participants