feat(i18n): add internationalization plugin with locale auto-detection and built-in translations#10800
Open
mmorel-35 wants to merge 4 commits intoswagger-api:masterfrom
Open
feat(i18n): add internationalization plugin with locale auto-detection and built-in translations#10800mmorel-35 wants to merge 4 commits intoswagger-api:masterfrom
mmorel-35 wants to merge 4 commits intoswagger-api:masterfrom
Conversation
159a156 to
ab85481
Compare
…n and built-in translations Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
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.
Adds a lightweight i18n system to Swagger UI via a new Redux plugin. All user-facing strings become translatable at runtime with zero breaking changes — existing behavior is identical when no
localeis configured.Description
src/core/plugins/i18n/— newI18nPluginregistered early in the base presetactions.js/reducers.js/selectors.js— standard Redux slice for{ locale, messages }statefn.js— puretranslate(localeMsgs, fallbackMsgs, key, vars?)+fallbackTfor component-level English fallback without the systemlocales/en.js— canonical English catalog (~98 keys, dot-namespaced:"button.execute","aria.collapse_operation", etc.)locales/index.js— re-exports all 9 built-in locale catalogs as a single keyed objectindex.js—afterLoadloads English, auto-detects or applies configured locale, auto-loads the matching built-in locale, injectst(key, vars?)intorootInjectssrc/core/config/defaults.js— single new option:locale: null(BCP 47 tag;null= readnavigator.languages[0], normalize to base code, fall back to"en")40 components updated — all user-visible strings replaced with
t("key")calls; every component that acceptstas a prop now declaresstatic defaultProps = { t: fallbackT }(class components) or usest = fallbackTas a default parameter (functional components), so all components degrade gracefully to English in isolated unit tests or standalone renders without the Redux systemsrc/core/components/param-body.jsx— correctedstatic defaultProptypo tostatic defaultProps(the React-recognized name) and updated the internal reference fromParamBody.defaultProp.consumestoParamBody.defaultProps.consumes, ensuring default props are correctly applied when props are omitteddocs/usage/i18n.md— stale partialresponse.*key table replaced with a canonical reference tolocales/en.js; docs accurately describe the lightweight key/value lookup without implying unsupportedIntlformatting/pluralization APIsThe plugin auto-loads the matching built-in locale on startup — no
loadMessagescall is needed for supported languages:i18n / JavaScript quality improvements
key in obj/k in varsguards infn.jsandindex.jsreplaced withObject.prototype.hasOwnProperty.call()to prevent inheritedObject.prototypeproperties (toString,constructor, etc.) from matching translation keystranslate()always returnsString(raw), eliminating a path that returned a raw non-string valuereducers.js—fromJS()(deep recursive conversion) replaced withMap()(correct shallow conversion for flat{ key: string }catalogs)"fr-CA"→"fr") for consistent matchingTopBarpropTypes — consolidated into a singlestatic propTypesblock to prevent the external assignment from silently overwritinglayoutActions/authActionsvalidationresponse.jsx—<code>Accept</code>inline element preserved in JSX using prefix/suffix i18n keys (response.controls_accept_header_prefix/_suffix) so the code styling is not lost in translationMotivation and Context
Hard-coded English strings throughout Swagger UI make it impossible for non-English users to get a localized experience. This change introduces a zero-dependency, zero-breaking-change i18n layer that supports runtime locale switching and ships with 9 built-in translations.
How Has This Been Tested?
npm run test:unit— all tests passtest/unit/core/plugins/i18n/test suite coveringtranslate(),fallbackT(), prototype edge-cases, and locale key-set completeness for all 9 built-in catalogslocale: "fr","de","zh"confirming auto-load and correct string renderingScreenshots (if appropriate):
Checklist
My PR contains...
src/is unmodified: changes to documentation, CI, metadata, etc.)package.json)My changes...
Documentation
Automated tests
Related to #2488