fix(theme): make i18n strings extractable by write-translations#1499
Open
navrocky wants to merge 1 commit into
Open
fix(theme): make i18n strings extractable by write-translations#1499navrocky wants to merge 1 commit into
navrocky wants to merge 1 commit into
Conversation
Every UI string in the theme was wrapped with `translate()` / `<Translate>`,
but the `id` was referenced through constants imported from `translationIds.ts`.
Docusaurus' `write-translations` only performs static analysis (@docusaurus/babel):
it evaluates each call's argument with Babel's `path.evaluate()` and keeps it only
when the result is `confident`. An id read from an imported constant is a
cross-module member expression that Babel cannot statically resolve, so every call
was skipped (with a warning) and nothing was written to the locale `code.json`.
Consuming projects therefore got an empty translation template even though the
English defaults rendered fine at runtime.
Changes:
- Inline all translation ids as string literals at the call site — the approach
Docusaurus theme-classic uses and its i18n guide mandates ("Text labels must be
static"): https://docusaurus.io/docs/i18n/tutorial#translate-your-react-code
- Remove the `translationIds.ts` dictionary; keep an explanatory note in its place
so the removal is discoverable and the pattern isn't reintroduced.
- Wrap strings that were not localized at all: Authorization (SecuritySchemes),
Export / OpenAPI Spec (Export), Add item (ParamArrayFormItem).
- Replace the dynamic `translate({ id: someVar })` in SecuritySchemes.renderRest
with static per-key calls so description/scheme/bearerFormat/openIdConnectUrl
are extracted too.
`write-translations` now statically extracts all `theme.openapi.*` ids.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Every UI string in the theme was wrapped with
translate()/<Translate>, but theidwas referenced through constants imported fromtranslationIds.ts. Docusaurus'write-translationsonly performs static analysis (@docusaurus/babel): it evaluates each call's argument with Babel'spath.evaluate()and keeps it only when the result isconfident. An id read from an imported constant is a cross-module member expression that Babel cannot statically resolve, so every call was skipped (with a warning) and nothing was written to the localecode.json. Consuming projects therefore got an empty translation template even though the English defaults rendered fine at runtime.Changes:
translationIds.tsdictionary; keep an explanatory note in its place so the removal is discoverable and the pattern isn't reintroduced.translate({ id: someVar })in SecuritySchemes.renderRest with static per-key calls so description/scheme/bearerFormat/openIdConnectUrl are extracted too.write-translationsnow statically extracts alltheme.openapi.*ids.Part of #319
Improve of #1210