You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c030c72
Browse filesBrowse the repository at this point in the historyBrowse files
perf(i18n): assemble the whole English catalog from its slices, so no string ships twice
An app that mounts InternationalizationProvider or calls useTranslator()
carries the whole catalog through the public context; with the components'
slices beside it, each rendered component's strings were in the bundle twice.
en.generated.ts is now the spread of the 78 slice modules, so a bundle that
reaches both carries every string once. A pinned test checks the assembled
catalog against the authored keys and messages.
The changeset says which apps gain the saving and how a provider-mounting app
moves. CONTRIBUTING, the CLI's i18n guide and the hardcoded-string lint
messages now send core authors to useComponentTranslator(<slice>), which the
catalog check requires, and name useTranslator() as the consumer-facing hook.
[perf] A component carries only its own English strings. The shipped English catalog is now emitted one namespace at a time, and every core component renders with its own slice instead of the whole 408-key catalog, so an app bundles the strings of the components it renders: two rows of buttons and links drop about 5 KB (gzip) from their first load. `useTranslator()` and `InternationalizationContext` are unchanged and still resolve every `@astryx.*` key; an `InternationalizationProvider`'s `messages` and `overrides` still win over the slice; the slices themselves are not importable. One undocumented path moves: a value handed straight to `InternationalizationContext` without the provider no longer reaches core components or `useLocale`, `useDirection` and `useCollator`, which read the provider's runtime context; mount `InternationalizationProvider`.
6
+
[perf] A component carries only its own English strings. The shipped English catalog is now emitted one namespace at a time, and every core component renders with its own slice instead of the whole 408-key catalog, so an app that renders components without mounting `InternationalizationProvider` or calling `useTranslator()` bundles only the strings of the components it renders: two rows of buttons and links drop about 5 KB (gzip) from their first load. An app that does mount the provider still carries the whole catalog, now assembled from the same slices so no string ships twice; it moves by under 1 KB either way (a few slice modules cost a little, many save a little). `useTranslator()` and `InternationalizationContext` are unchanged and still resolve every `@astryx.*` key; an `InternationalizationProvider`'s `messages` and `overrides` still win over the slice; the slices themselves are not importable. One undocumented path moves: a value handed straight to `InternationalizationContext` without the provider no longer reaches core components or `useLocale`, `useDirection` and `useCollator`, which read the provider's runtime context; mount `InternationalizationProvider`.
// Only the right side can be a fallback/default string; the left is a
@@ -247,21 +263,21 @@ const rule = {
247
263
type: 'problem',
248
264
docs: {
249
265
description:
250
-
'Disallow hardcoded user-facing strings; route through useTranslator() instead',
266
+
'Disallow hardcoded user-facing strings; route through a translator (useComponentTranslator(<slice>) in core, useTranslator() elsewhere) instead',
251
267
category: 'Best Practices',
252
268
recommended: true,
253
269
},
254
270
messages: {
255
271
hardcodedString:
256
272
'Hardcoded user-facing string {{value}} in `{{name}}`. '+
257
-
'Route through `t(\'<namespace>.<component>.<key>\')` via `useTranslator()` so it can be localized.',
273
+
"Route through `t('<namespace>.<component>.<key>')` via `useComponentTranslator(<slice>)` (in core; `useTranslator()` elsewhere) so it can be localized.",
258
274
hardcodedDefault:
259
275
'Hardcoded default {{value}} for prop `{{name}}`. '+
260
276
'Use the alias-and-resolve pattern: `{{name}}: {{name}}FromProps` in the destructure, then '+
261
-
'`const {{name}} = {{name}}FromProps ?? t(\'<namespace>.<component>.<key>\');` inside the body.',
277
+
"`const {{name}} = {{name}}FromProps ?? t('<namespace>.<component>.<key>');` inside the body.",
262
278
hardcodedCallArg:
263
279
'Hardcoded user-facing string {{value}} passed to `{{name}}()`. '+
264
-
'Route through `t(\'<namespace>.<component>.<key>\')` via `useTranslator()` so it can be localized.',
280
+
"Route through `t('<namespace>.<component>.<key>')` via `useComponentTranslator(<slice>)` (in core; `useTranslator()` elsewhere) so it can be localized.",
'Text direction (RTL), pseudo-locale testing, and contributor guidance for developers and translators.',
@@ -122,22 +126,23 @@ import pseudo from '@astryxdesign/core/locales/pseudo.generated.js';
122
126
},
123
127
{
124
128
type: 'prose',
125
-
text: 'Astryx component authors read strings with `useTranslator()` rather than hardcoding user-facing text.',
129
+
text: "Astryx component authors read strings with `useComponentTranslator(<slice>)` rather than hardcoding user-facing text. The slice is the component's own namespace of the English catalog, so an app bundles only the strings of the components it renders.",
126
130
},
127
131
{
128
132
type: 'code',
129
133
lang: 'tsx',
130
134
label: 'Read an astryx string',
131
-
code: `import {useTranslator} from '@astryxdesign/core/i18n';
135
+
code: `import {useComponentTranslator} from '../i18n/useComponentTranslator';
136
+
import saveButtonMessages from '../i18n/generated-locales/en/saveButton.generated';
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.",
145
+
text: "Astryx's own strings live in `packages/core/locales/en.json`, keyed `@astryx.<namespace>.<name>`. New user-facing strings must go through the translator; the `@astryx/no-hardcoded-i18n-string` ESLint rule enforces that, and `pnpm check:i18n-catalog` fails a key read outside the file's slice. `useTranslator()` from `@astryxdesign/core/i18n` is the consumer-facing hook and carries the whole catalog. See the AI contribution guide for the alias-and-resolve pattern used when adding new keys.",
`import ${identifier(namespace)} from './${EN_SLICES_DIR}/${namespace}.generated';`,
166
+
)
167
+
.join('\n');
168
+
constspreads=namespaces
169
+
.map(namespace=>`...${identifier(namespace)},`)
170
+
.join('\n');
171
+
constsource=`// Copyright (c) Meta Platforms, Inc. and affiliates.\n\n/**\n * @generated by scripts/generate-i18n-runtime.mjs from packages/core/locales/${EN_FILE}.\n * The whole English catalog: the spread of its namespace slices under ./${EN_SLICES_DIR}/,\n * so each string ships once however it is reached.\n * Translator descriptions remain in the authored JSON catalogs and must not be copied here.\n * Do not edit by hand. Run \`pnpm generate:i18n-runtime\` instead.\n */\n\n${imports}\n\nconst runtimeCatalog: Readonly<Record<string, string>> = {\n${spreads}\n};\n\nexport default runtimeCatalog;\n`;
0 commit comments