Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5e4c993
perf(i18n): let a component carry only its own English strings
vjeux Oct 11, 2026
dcb9666
chore(i18n): declare the raw-context path's retirement
vjeux Oct 12, 2026
3a4142e
feat(cli): migrate a raw InternationalizationContext value to the pro…
vjeux Oct 12, 2026
0ac8edf
docs(i18n): scope the breaking entry to the hooks this revision moves…
vjeux Oct 12, 2026
7df8e75
fix(cli): the context codemod edits only what it can prove and rewrit…
vjeux Oct 12, 2026
e3f5f14
fix(cli): the context codemod resolves block scopes
vjeux Oct 12, 2026
58003b9
fix(cli): the context codemod imports the provider under a free name …
vjeux Oct 12, 2026
17e3421
fix(cli): the context codemod checks the provider name it writes; tri…
vjeux Oct 12, 2026
db49087
fix(cli): type the codemod's provider name as a string
vjeux Oct 12, 2026
80d6ebf
fix(cli): the context codemod maps only a variable's required fields;…
vjeux Oct 12, 2026
d99cf19
docs(cli): say what the context codemod maps for a variable
vjeux Oct 12, 2026
d359e1b
test(cli): the context codemod leaves the consumer shape in the wild …
vjeux Oct 12, 2026
011b8ae
fix(cli): the context codemod's TODO names the local that shadows the…
vjeux Oct 12, 2026
33cfa85
fix(cli): the context codemod leaves an empty literal behind the TODO…
vjeux Oct 12, 2026
ed99b8d
fix(cli): the context codemod sees private-method parameters, static …
vjeux Oct 12, 2026
fab7ed1
fix(cli): the context codemod sees an exported namespace member as a …
vjeux Oct 12, 2026
627acf0
fix(cli): the context codemod sees import-equals and ambient declarat…
vjeux Oct 12, 2026
c44090d
fix(cli): the context codemod counts a nested namespace as a binding
vjeux Oct 12, 2026
25a4e58
fix(cli): the context codemod sees a dotted namespace as binding its …
vjeux Oct 12, 2026
88c4340
docs: importing InternationalizationContext also carries the whole ca…
vjeux Oct 12, 2026
b0588ff
fix(cli): the context codemod declines a spread that follows value
vjeux Oct 22, 2026
df7c3f5
chore(cli): the context codemod moves to its own stacked pull request
vjeux Oct 23, 2026
8da012c
docs(i18n): the breaking entry carries the migration as the changelog…
vjeux Oct 23, 2026
0de5c8e
fix(build): the Vite locale alias takes one path segment, so a slice …
vjeux Oct 23, 2026
f9267e0
test(i18n): resolve the locale exports through the package's self-ref…
vjeux Oct 25, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/i18n-cli-guide-caller-facing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/cli': patch
---

[docs] The i18n, RTL and testing guide keeps to what holds for a caller: `useTranslator()` resolves every `@astryx.*` key; how a core component reads its strings moves to the contributing guide.

@vjeux
7 changes: 7 additions & 0 deletions .changeset/i18n-en-namespace-slices.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': minor
---

[breaking] An app that sets `InternationalizationContext` directly — rendering `<InternationalizationContext value={…}>` (or its `.Provider`) instead of `InternationalizationProvider` — no longer drives `useLocale`, `useDirection` and `useCollator`, nor the components that read the locale or direction through them (the date, date-time, date-range and number inputs, Calendar, Chat, PowerSearch, Table sorting and filtering, Timestamp). Its strings still follow such a value through `useTranslator()`, as does the locale of `DateTimeInput`'s touch field, until the components move to their own slices, which stops that too. Migration: mount `InternationalizationProvider`; its `locale`, `messages` and `overrides` take the value's fields of the same name, and its `dir` takes the value's `direction`. The provider has no `translate` input: an app that supplied its own `translate` expresses those strings as `messages` (a catalog per locale) or `overrides` (sparse, per locale) and the provider resolves them in the documented order. Run `astryx upgrade`: its `migrate-internationalization-context` transform rewrites a variable value to the provider's `locale`, `dir` and `messages` with a TODO for its `overrides` and `translate`, and an inline object literal key by key with a TODO for `translate`; any other value, and any element that spreads props, is left as written behind a TODO saying why. The provider now writes an internal runtime context that those hooks read. Reading the public context is unchanged, so a component that reads it with `useContext` and re-mounts `InternationalizationProvider` with a derived value keeps working: `locale`, `direction`, `messages`, `overrides` and `translate`, which still resolves every `@astryx.*` key against the whole shipped English catalog, with or without a provider; `useTranslator()` is unchanged. Groundwork for components carrying only their own English strings: the English catalog is also emitted one namespace at a time, the resolver takes its English fallback per call, and an internal `useComponentTranslator(slice)` lets a core component render with its own slice. The slices are not importable.

@vjeux
7 changes: 7 additions & 0 deletions .changeset/vite-locale-alias-one-segment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/build': patch
---

[fix] `astryxStylex()`'s alias for `@astryxdesign/core/locales/<locale>.generated.js` takes one path segment, so the per-namespace English slices under `locales/en/` — which the package's exports map keeps out of reach — no longer resolve through the source tree in a Vite app either.

@vjeux
37 changes: 26 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,24 +291,39 @@ export * from './MyComponent';

### User-facing strings and text direction

Components read user-facing strings with `useTranslator()` rather than
hardcoding them:
Components read user-facing strings with `useComponentTranslator(<slice>)`
rather than hardcoding them. The slice is the component's own namespace of the
English catalog, generated under `src/i18n/generated-locales/en/`, so an app
that neither mounts `InternationalizationProvider`, calls `useTranslator()` nor
imports `InternationalizationContext` (each carries the whole catalog) bundles
only the strings of the components it renders:

```tsx
import {useTranslator} from '@astryxdesign/core/i18n';
import {useComponentTranslator} from '../i18n/useComponentTranslator';
import saveButtonMessages from '../i18n/generated-locales/en/saveButton.generated';

function SaveButton() {
const t = useTranslator();
return <button>{t('@astryx.actions.save')}</button>;
const t = useComponentTranslator(saveButtonMessages);
return <button>{t('@astryx.saveButton.save')}</button>;
}
```

Astryx's own strings live in `packages/core/locales/en.json`. The
`@astryx/no-hardcoded-i18n-string` ESLint rule rejects a hardcoded one. For a
prop whose default is a translated string, alias the prop and resolve it in the
body: destructure `label: labelFromProps`, then
A file that reads several namespaces merges their slices once at module level
(`const messages = {...aMessages, ...bMessages}`) and passes that. Astryx's own
strings live in `packages/core/locales/en.json`, keyed
`@astryx.<namespace>.<name>`; `pnpm generate:i18n-runtime` emits the slices, and
`pnpm check:i18n-catalog` gains a slice-coverage rule with the component
sweep, failing a key read outside the file's slice. The
`@astryx/no-hardcoded-i18n-string` ESLint rule rejects a hardcoded string. For
a prop whose default is a translated string, alias the prop and resolve it in
the body: destructure `label: labelFromProps`, then
`const label = labelFromProps ?? t('@astryx.<component>.<key>')`.

`useTranslator()` from `@astryxdesign/core/i18n` is the consumer-facing hook:
it carries the whole English catalog, so a component inside `packages/core`
renders with its slice instead; packages that depend on core (`lab`,
`richtext`) use `useTranslator()`.

A component that responds to text direction resolves it from the DOM, not from
a render-time JavaScript read. Reach for the lightest tool that works, in this
order:
Expand Down Expand Up @@ -346,8 +361,8 @@ order:
flip for free; don't pass them a direction flag.
4. **`useDirection()` context, as a last resort.** Use it only when you need
the direction value during render and nothing above fits. It is SSR-safe
and returns `'ltr'` outside a provider (like `useTranslator`'s silent
fallback), but it is the one path that can mismatch on hydration when the
and returns `'ltr'` outside a provider (like a translator's silent
English fallback), but it is the one path that can mismatch on hydration when the
provider's direction disagrees with `<html dir>`. No Astryx component reads
direction from context at render time.

Expand Down
6 changes: 3 additions & 3 deletions internal/eslint-plugin-astryx/no-hardcoded-i18n-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,21 +247,21 @@ const rule = {
type: 'problem',
docs: {
description:
'Disallow hardcoded user-facing strings; route through useTranslator() instead',
'Disallow hardcoded user-facing strings; route through a translator (useComponentTranslator(<slice>) in core, useTranslator() elsewhere) instead',
category: 'Best Practices',
recommended: true,
},
messages: {
hardcodedString:
'Hardcoded user-facing string {{value}} in `{{name}}`. ' +
'Route through `t(\'<namespace>.<component>.<key>\')` via `useTranslator()` so it can be localized.',
'Route through `t(\'<namespace>.<component>.<key>\')` via `useComponentTranslator(<slice>)` (in core; `useTranslator()` elsewhere) so it can be localized.',
hardcodedDefault:
'Hardcoded default {{value}} for prop `{{name}}`. ' +
'Use the alias-and-resolve pattern: `{{name}}: {{name}}FromProps` in the destructure, then ' +
'`const {{name}} = {{name}}FromProps ?? t(\'<namespace>.<component>.<key>\');` inside the body.',
hardcodedCallArg:
'Hardcoded user-facing string {{value}} passed to `{{name}}()`. ' +
'Route through `t(\'<namespace>.<component>.<key>\')` via `useTranslator()` so it can be localized.',
'Route through `t(\'<namespace>.<component>.<key>\')` via `useComponentTranslator(<slice>)` (in core; `useTranslator()` elsewhere) so it can be localized.',
},
schema: [
{
Expand Down
21 changes: 21 additions & 0 deletions packages/build/src/vite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,27 @@ describe('astryxStylex locale aliases', () => {
path.join(corePackage, 'src/Button'),
);
});

it('does not route a per-namespace English slice to its source file', () => {
// `locales/en/<namespace>.generated.js` is the package's own: the exports
// map gives it a `null` target, and the alias for whole catalogs takes a
// single path segment, so the slice specifier never reaches
// `src/i18n/generated-locales/en/`.
const rootDir = path.join(tmpdir(), 'astryx-vite-alias');
const aliases = getInjectedConfig(rootDir).resolve.alias;
const corePackage = path.join(rootDir, 'node_modules/@astryxdesign/core');
const resolved = applyAlias(
aliases,
'@astryxdesign/core/locales/en/button.generated.js',
);
expect(resolved).not.toContain('generated-locales');
expect(resolved).not.toBe(
path.join(
corePackage,
'src/i18n/generated-locales/en/button.generated.ts',
),
);
});
});

describe('astryxStylex optimizeDeps package discovery', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/build/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,12 @@ export function astryxStylex(
resolve: {
alias: [
{
find: /^@astryxdesign\/core\/locales\/(.+)\.generated\.js$/,
// A whole catalog, `locales/<locale>.generated.js`. The capture
// takes one path segment only: the per-namespace English slices
// under `locales/en/<namespace>.generated.js` are the package's
// own (the exports map gives them a `null` target), and this
// alias must not reach them either.
find: /^@astryxdesign\/core\/locales\/([^/]+)\.generated\.js$/,
replacement: path.join(
coreSourceDir,
'i18n/generated-locales/$1.generated.ts',
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/assets/docs/tree/i18n-rtl-and-testing.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ import pseudo from '@astryxdesign/core/locales/pseudo.generated.js';
},
{
type: 'prose',
text: 'Astryx component authors read strings with `useTranslator()` rather than hardcoding user-facing text.',
text: "Astryx components read their strings from the English catalog rather than hardcoding them. Your own code reads any `@astryx.*` key with `useTranslator()`:",
},
{
type: 'code',
Expand All @@ -137,7 +137,7 @@ function SaveButton() {
},
{
type: 'prose',
text: "Astryx's own strings live in `packages/core/locales/en.json`. New user-facing strings must go through `useTranslator`; this is enforced by the `@astryx/no-hardcoded-i18n-string` ESLint rule. See the AI contribution guide for the alias-and-resolve pattern used when adding new keys.",
text: "Astryx's own strings live in `packages/core/locales/en.json`, keyed `@astryx.<namespace>.<name>`. The contributing guide covers how a core component reads its strings, the `@astryx/no-hardcoded-i18n-string` ESLint rule and the alias-and-resolve pattern used when adding new keys.",
},
{
type: 'prose',
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
"types": "./dist/i18n/generated-locales/*.generated.d.ts",
"default": "./dist/i18n/generated-locales/*.generated.js"
},
"./locales/en/*": null,

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.

This null keeps Node (and any resolver that honours the exports map) away from the slices, but astryxStylex() from @astryxdesign/build/vite aliases /^@astryxdesign\/core\/locales\/(.+)\.generated\.js$/ straight to src/i18n/generated-locales/$1.generated.ts (packages/build/src/vite.ts line 228), so @astryxdesign/core/locales/en/button.generated.js still resolves to the slice in a Vite app. Suggest narrowing that capture to ([^/]+) and adding a vite.test.ts case for a slice specifier.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch — a Vite app reaching the slices would have undone the null export quietly. Fixed in ef84cea: astryxStylex()'s whole-catalog alias now captures one path segment (locales/([^/]+).generated.js), so locales/en/button.generated.js no longer routes to src/i18n/generated-locales/; a new vite.test.ts case asserts the slice specifier resolves to nothing under generated-locales (it fails with the old (.+)). A [fix] changeset names @astryxdesign/build. The two test reds on 383b13c were a 30 s timeout in the PowerShell package-manager shim test, unrelated to this PR's files.

"./AlertDialog": {
"source": "./src/AlertDialog/index.ts",
"types": "./dist/AlertDialog/index.d.ts",
Expand Down
47 changes: 32 additions & 15 deletions packages/core/src/i18n/InternationalizationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,62 @@

/**
* @file InternationalizationContext.ts
* @input React createContext, i18n types
* @input React createContext, i18n types, the whole shipped English catalog
* @output Exports InternationalizationContext and InternationalizationContextValue
* @position Context definition for client-side locale + messages
* @position Context definition for client-side locale + messages: the public
* context a consumer reads. Its `translate` resolves every `@astryx.*` key,
* falling back to the shipped English catalog, so this module carries that
* catalog; the locale hooks inside this package read
* TranslationRuntimeContext instead, and a component moves to it (carrying
* only its own slice) as it adopts useComponentTranslator. Both contexts
* are written by InternationalizationProvider, which is how an app
* configures Astryx: a value rendered straight into this context reaches
* consumers of this context and nothing that reads the runtime context.
*
* Separated from InternationalizationProvider.tsx so components can consume
* the context without pulling in the full provider implementation.
* Follows the LinkContext.ts / ThemeContext.ts pattern.
*
* SYNC: When modified, update these files to stay in sync:
* - /packages/core/src/i18n/InternationalizationProvider.tsx
* - /packages/core/src/i18n/t.client.ts
* - /packages/core/src/i18n/useDirection.ts
* - /packages/core/src/i18n/useLocale.ts
* - /packages/core/src/i18n/useCollator.ts
* - /packages/core/src/i18n/getLocaleDirection.ts
* - /packages/core/src/i18n/index.ts
* - /packages/core/src/i18n/TranslationRuntimeContext.ts
* - /packages/core/src/i18n/useTranslator.ts
*/

import {createContext} from 'react';
import {getResolve} from './resolve';
import enCatalog from './generated-locales/en.generated';
import type {Locale, MessagesByLocale, Overrides} from './types';
import type {MessageResolver} from './TranslationRuntimeContext';
import {getResolve} from './resolve';

/** A translator over the whole shipped catalog: `translate(key, values?)`. */
export type Translate = (
key: string,
values?: Record<string, unknown>,
) => string;

export interface InternationalizationContextValue {
locale: Locale;
direction: 'ltr' | 'rtl';
messages: MessagesByLocale;
overrides?: Overrides;
translate: ReturnType<typeof getResolve>;
/**
* Resolves a key against the provider's catalogs and overrides, then the
* whole shipped English catalog.
*/
translate: Translate;
}

/** Bind the whole English catalog as a resolver's fallback. */
export function translateWithCatalog(resolve: MessageResolver): Translate {
return (key, values) => resolve(key, values, enCatalog);
}

/**
* Default value falls through to the shipped en catalog in resolve().
* A consumer that doesn't render a provider still gets English defaults.
*/
export const InternationalizationContext =
createContext<InternationalizationContextValue>({
locale: 'en',
direction: 'ltr',
messages: {},
translate: getResolve('en', {}),
translate: translateWithCatalog(getResolve('en', {})),
});
InternationalizationContext.displayName = 'InternationalizationContext';
32 changes: 25 additions & 7 deletions packages/core/src/i18n/InternationalizationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* @file InternationalizationProvider.tsx
* @input React, InternationalizationContext, i18n types
* @input React, InternationalizationContext, TranslationRuntimeContext, i18n types
* @output Exports InternationalizationProvider component and props type
* @position Provider component for astryx i18n locale + messages
*
Expand All @@ -20,7 +20,11 @@
*/

import {useMemo, type ReactNode} from 'react';
import {InternationalizationContext} from './InternationalizationContext';
import {
InternationalizationContext,
translateWithCatalog,
} from './InternationalizationContext';
import {TranslationRuntimeContext} from './TranslationRuntimeContext';
import {getLocaleDirection} from './getLocaleDirection';
import type {
Locale,
Expand Down Expand Up @@ -115,20 +119,34 @@ export function InternationalizationProvider({
children,
}: InternationalizationProviderProps) {
const direction = dir ?? getLocaleDirection(locale);
const value = useMemo(() => {
// One resolver, two readers: the components inside this package pass their
// own English slice to it; the public context binds the whole catalog.
const runtime = useMemo(() => {
const providedMessages = messages ?? {};
return {
locale,
direction,
messages: normalizeMessages(providedMessages),
overrides,
translate: getResolve(locale, providedMessages, overrides),
resolve: getResolve(locale, providedMessages, overrides),
};
}, [locale, direction, messages, overrides]);
const value = useMemo(
() => ({
locale: runtime.locale,
direction: runtime.direction,
messages: runtime.messages,
overrides: runtime.overrides,
translate: translateWithCatalog(runtime.resolve),
}),
[runtime],
);
return (
<InternationalizationContext value={value}>
{children}
</InternationalizationContext>
<TranslationRuntimeContext value={runtime}>
<InternationalizationContext value={value}>
{children}
</InternationalizationContext>
</TranslationRuntimeContext>
);
}

Expand Down
46 changes: 46 additions & 0 deletions packages/core/src/i18n/TranslationRuntimeContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

'use client';

/**
* @file TranslationRuntimeContext.ts
* @input React createContext; getResolve
* @output Exports TranslationRuntimeContext — the locale, direction, catalogs
* and the message resolver that the components inside this package read —
* and its value type
* @position Internal to the i18n runtime. The public InternationalizationContext
* is this value plus a `translate` bound to the whole English catalog; the
* provider writes both. This module imports no catalog, so a component that
* reads it (useComponentTranslator, useLocale, useDirection, useCollator)
* bundles none; the whole catalog rides only with the public context, the
* provider and useTranslator.
*
* SYNC: When modified, update these files to stay in sync:
* - /packages/core/src/i18n/InternationalizationContext.ts
* - /packages/core/src/i18n/InternationalizationProvider.tsx
* - /packages/core/src/i18n/useComponentTranslator.ts
*/

import {createContext} from 'react';
import {getResolve} from './resolve';
import type {Locale, MessagesByLocale, Overrides} from './types';

/** Resolves a key against the provider's catalogs, then the caller's English. */
export type MessageResolver = ReturnType<typeof getResolve>;

export interface TranslationRuntimeContextValue {
locale: Locale;
direction: 'ltr' | 'rtl';
messages: MessagesByLocale;
overrides?: Overrides;
resolve: MessageResolver;
}

export const TranslationRuntimeContext =
createContext<TranslationRuntimeContextValue>({
locale: 'en',
direction: 'ltr',
messages: {},
resolve: getResolve('en', {}),
});
TranslationRuntimeContext.displayName = 'TranslationRuntimeContext';
Loading
Loading