Skip to content

refactor(i18n): pre-build lookup map for translations - #4696

Merged
cixzhang merged 1 commit into
meta:mainfrom
ngolin:main
Aug 29, 2026
Merged

cixzhang merged 1 commit into
meta:mainfrom
ngolin:main

Conversation

@ngolin

@ngolin ngolin commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Memoize the translation lookup map and expose it via context instead of resolving keys on each call. This removes per-call chain resolution and the useCallback wrapper, improving performance for frequent translations.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 4, 2026
@vercel

vercel Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx (canary) Ready Ready Preview Aug 29, 2026 3:25am
astryx Ready Ready Preview Aug 29, 2026 3:25am

Request Review

@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge needs:design-review Affects visuals — Design should review labels Aug 4, 2026
@rubyycheung rubyycheung removed the needs:design-review Affects visuals — Design should review label Aug 7, 2026
@github-actions github-actions Bot added the needs:design-review Affects visuals — Design should review label Aug 12, 2026

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

Thanks for looking at this — walking the chain per key is genuinely repetitive and worth attacking.

The blocker is the lookup order. The file documents overrides (exact locale, then parent) as beating every shipped catalog entry; the new map fills per tag, overrides then messages, so a shipped pt-BR string now wins over a pt override. An app that overrides a string for Portuguese loses it silently the first time the shipped pt-BR catalog gains that key — nothing throws, the wording just reverts.

getResolve('pt-BR', {'pt-BR': {k: {defaultMessage: 'shipped'}}}, {pt: {k: 'override'}})('k')
// main: 'override'   this branch: 'shipped'

Two passes over the chain — every override first, then every catalog — restores it.

Separate and not blocking: the module-level getResolve('en', {}) builds all 305 en keys at import time, for every consumer of core whether they render a translated string or not.

If you'd rather talk it through with someone, we're in Discord.

[Reviewed by Robohands]

Comment thread packages/core/src/i18n/resolve.ts Resolved
@cixzhang

Copy link
Copy Markdown
Contributor

Checking in on locale lookup ordering: parent-locale overrides still need to beat exact shipped catalog strings. Would you like to continue, have us carry it, or close this for now?

@ngolin

ngolin commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Checking in on locale lookup ordering: parent-locale overrides still need to beat exact shipped catalog strings. Would you like to continue, have us carry it, or close this for now?

Yes. I will make a quick fix and continue. Thank you for the feedback.

Memoize the translation lookup map and expose it via context instead of
resolving keys on each call. This removes per-call chain resolution and
the `useCallback` wrapper, improving performance for frequent translations.
@ngolin

ngolin commented Aug 29, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for looking at this — walking the chain per key is genuinely repetitive and worth attacking.

The blocker is the lookup order. The file documents overrides (exact locale, then parent) as beating every shipped catalog entry; the new map fills per tag, overrides then messages, so a shipped pt-BR string now wins over a pt override. An app that overrides a string for Portuguese loses it silently the first time the shipped pt-BR catalog gains that key — nothing throws, the wording just reverts.

getResolve('pt-BR', {'pt-BR': {k: {defaultMessage: 'shipped'}}}, {pt: {k: 'override'}})('k')
// main: 'override'   this branch: 'shipped'

Two passes over the chain — every override first, then every catalog — restores it.

Separate and not blocking: the module-level getResolve('en', {}) builds all 305 en keys at import time, for every consumer of core whether they render a translated string or not.

If you'd rather talk it through with someone, we're in Discord.

[Reviewed by Robohands]

Thanks for the detailed review. Both issues are addressed.

Issue 1 — Override precedence (order bug)

getLookup now walks the locale chain in two passes — all overrides first (exact → parent), then all catalogs (exact → parent) — instead of interleaving them per tag. Your repro now resolves to the override:

getResolve('pt-BR', {'pt-BR': {k: {defaultMessage: 'shipped'}}}, {pt: {k: 'override'}})('k')
// 'override' ✓

This repro is locked in by a regression test in resolve.test.ts: "override `pt` should take precedence over message `pt-BR`".

Issue 2 — eager en catalog build at import time

EN_CATALOG is no longer materialized inside getResolve('en', {}) at module load. getResolve stays cheap, so every core consumer pays almost nothing at import regardless of whether they ever render a translated string. EN_CATALOG is used only as the final fallback inside resolve:

const result = lookup[key] ?? EN_CATALOG[key]?.defaultMessage;

Thanks again for the thorough review — happy to discuss further if anything else comes up.

@cixzhang
cixzhang enabled auto-merge (squash) August 29, 2026 15:02
@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Aug 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.8KB 1.2KB

Accessibility Audit

Status: No accessibility violations detected.

Visual Regression

Status: Skipped — Broad stable scope is deferred to the daily release gate. It covers 1964 trusted baseline shots instead of recapturing them for this PR. View the report


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@cixzhang
cixzhang merged commit 8359f1a into meta:main Aug 29, 2026
25 checks passed
rubyycheung added a commit that referenced this pull request Aug 30, 2026
* Improve DropdownMenu responsive fit and presentation guidance

* fix DropdownMenu scroll region keyboard access

* docs clarify compact submenu presentations

* fix DropdownMenu anchor alignment and submenu examples

* fix DropdownMenu submenu spacing

* docs minimize DropdownMenu guidance diff

* fix(DropdownMenu): preserve intrinsic width values

* feat: add adaptive menu presentations

* chore: version packages for v0.5.1 (#5661)

* refactor(i18n): pre-build lookup map for translations (#4696)

* fix(popover): improve responsive fit and focus

* Preserve Popover compatibility when constraining overflow

* Add real-viewport Popover overflow stories

* Refine Popover overflow story

* Restore Popover story dismissal behavior

* docs clarify Popover presentation guidance

* docs render Popover evidence in real viewports

* fix(Popover): address accessibility review

* fix DropdownMenu controlled close navigation

* fix(Popover): observe overflow only while open

* fix(Popover): align mobile presentation behavior

* docs(Popover): tighten bottom sheet spacing

* fix(menu): align mobile bottom sheets

* fix(Popover): hide container focus outline

* fix(ContextMenu): prevent touch selection

* docs(ContextMenu): add bottom sheet example

* fix(MoreMenu): preserve touch feedback

* docs(MoreMenu): add bottom sheet example

* fix(ContextMenu): reliably suppress touch selection

* fix(ContextMenu): improve mobile doc examples

* fix(MoreMenu): reduce bottom sheet padding

* refactor(menu): share adaptive sheet infrastructure

* fix(menu): preserve focus without touch rings

* fix(BottomSheet): keep browser edge surface-colored

* fix(ContextMenu): drill into sheet submenus

* Add adaptive Selector and MultiSelector presentations (#5685)

* feat: add adaptive selector presentations

* fix: refine selector bottom sheet interactions

* refactor(selector): use shared adaptive policy

* fix(selector): preserve focus without touch rings

* fix selectors bottom sheet focus target

---------

Co-authored-by: Cindy Zhang <[email protected]>
Co-authored-by: Li Hongzhang <[email protected]>
AKnassa added a commit to AKnassa/astryx that referenced this pull request Sep 26, 2026
Resolved conflicts (upstream meta#4696 replaced resolve() with a
getResolve(locale, messages, overrides) closure stored on the context as
`translate`):

- packages/core/src/i18n/resolve.ts: took upstream's getResolve/getLookup
  shape and moved the translator delegation into the returned closure;
  getResolve gains an optional fourth `translator` argument.
- packages/core/src/i18n/InternationalizationProvider.tsx: build
  `translate: getResolve(locale, messages ?? {}, overrides, translator)`
  and keep `translator` in the context value and the useMemo deps.
- packages/core/src/i18n/InternationalizationContext.ts: keep both the
  PR's `translator?` field and upstream's `translate` field.
- packages/core/src/i18n/useTranslator.ts: took upstream's
  `return ctx.translate` (the PR's useCallback change no longer applies).

Merge fallout: translator.test.tsx called the removed `resolve` export, so
it now goes through a local getResolve shim like resolve.test.ts's, and
the dep-array comments point at the provider memo instead of useCallback.

This branch was successfully deployed

2 active deployments
Preview — 31719d8f Deployed Aug 29, 2026 by vercel[bot]
canary — 31719d8f Deployed Aug 29, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team) needs:design-review Affects visuals — Design should review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants