From f648e55ed69dbc1babc80ed0646e24d65519e0d4 Mon Sep 17 00:00:00 2001 From: Peter White <1788320+peterwhite@users.noreply.github.com> Date: Tue, 1 Dec 2026 09:13:43 +0100 Subject: [PATCH] Enforce api.TranslationLanguage parity and regional OpenAPI locales (#4866) --- .changeset/sync-translation-languages.md | 6 ++++ .../src/intl/translations/index.test.ts | 32 +++++++++++++++++++ .../gitbook/src/intl/translations/index.ts | 5 ++- .../src/translations/index.test.ts | 28 ++++++++++++++++ .../react-openapi/src/translations/index.ts | 3 ++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 .changeset/sync-translation-languages.md create mode 100644 packages/react-openapi/src/translations/index.test.ts diff --git a/.changeset/sync-translation-languages.md b/.changeset/sync-translation-languages.md new file mode 100644 index 000000000..b29534ec2 --- /dev/null +++ b/.changeset/sync-translation-languages.md @@ -0,0 +1,6 @@ +--- +"@gitbook/react-openapi": patch +"gitbook": patch +--- + +Enforce `api.TranslationLanguage` parity in site UI translations and support `fr-ca`, `es-mx`, and `es-419` locales in OpenAPI blocks. diff --git a/packages/gitbook/src/intl/translations/index.test.ts b/packages/gitbook/src/intl/translations/index.test.ts index 4441ccc5a..95c95d9d9 100644 --- a/packages/gitbook/src/intl/translations/index.test.ts +++ b/packages/gitbook/src/intl/translations/index.test.ts @@ -1,9 +1,41 @@ import { describe, expect, it } from 'bun:test'; +import { TRANSLATION_LANGUAGE_ENUM } from '@gitbook/api'; +import { checkIsValidLocale } from '@gitbook/react-openapi'; + import { es } from './es'; import { fr } from './fr'; import { isAvailableLanguage, languages, loadLanguage } from './index'; +describe('API translation language parity', () => { + it('loads a valid translation bundle for every API translation language', async () => { + for (const locale of TRANSLATION_LANGUAGE_ENUM) { + expect(isAvailableLanguage(locale)).toBe(true); + + const language = await loadLanguage(locale); + expect(language).not.toBeNull(); + expect(language.locale).toBe(locale); + expect(language.language).toBe(languages[locale].language); + expect(language.flag).toBe(languages[locale].flag); + } + }); + + it('accepts every regional API translation language whose base language exists in react-openapi', () => { + for (const locale of TRANSLATION_LANGUAGE_ENUM) { + const baseLocale = locale.split('-')[0]; + // zh-tw is Traditional Chinese, whereas react-openapi's zh is Simplified Chinese. + if ( + baseLocale && + baseLocale !== locale && + locale !== 'zh-tw' && + checkIsValidLocale(baseLocale) + ) { + expect(checkIsValidLocale(locale)).toBe(true); + } + } + }); +}); + describe('regional language variants', () => { it.each([ ['fr-ca', fr], diff --git a/packages/gitbook/src/intl/translations/index.ts b/packages/gitbook/src/intl/translations/index.ts index 691f9eb69..3836e1c1d 100644 --- a/packages/gitbook/src/intl/translations/index.ts +++ b/packages/gitbook/src/intl/translations/index.ts @@ -1,3 +1,5 @@ +import type { TranslationLanguage as APITranslationLanguage } from '@gitbook/api'; + import { en } from './en'; import type { TranslationLanguage } from './types'; @@ -185,7 +187,8 @@ const languageDefinitions = { metadata: { locale: 'sl', language: 'Slovenščina', flag: '🇸🇮' }, load: () => import('./sl').then((module) => module.sl), }, -} satisfies Record; + // Require every API translation language while still allowing locales added ahead of @gitbook/api. +} satisfies Record & Record; export type TranslationLocale = keyof typeof languageDefinitions; diff --git a/packages/react-openapi/src/translations/index.test.ts b/packages/react-openapi/src/translations/index.test.ts new file mode 100644 index 000000000..07c6ba085 --- /dev/null +++ b/packages/react-openapi/src/translations/index.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from 'bun:test'; + +import { resolveOpenAPIContext } from '../context'; +import { es } from './es'; +import { fr } from './fr'; +import { checkIsValidLocale } from './index'; + +describe('regional language variants', () => { + it.each([ + ['fr-ca', fr], + ['es-mx', es], + ['es-419', es], + ] as const)('accepts %s and resolves its base translation', (locale, expected) => { + expect(checkIsValidLocale(locale)).toBe(true); + + const resolved = resolveOpenAPIContext({ + locale, + specUrl: null, + scalarRuntimeURL: '', + icons: {} as Parameters[0]['icons'], + renderCodeBlock: () => null, + renderHeading: () => null, + renderDocument: () => null, + }); + + expect(resolved.translation).toBe(expected); + }); +}); diff --git a/packages/react-openapi/src/translations/index.ts b/packages/react-openapi/src/translations/index.ts index 690b33187..a1c5d268c 100644 --- a/packages/react-openapi/src/translations/index.ts +++ b/packages/react-openapi/src/translations/index.ts @@ -15,7 +15,10 @@ export const translations = { en, de, es, + 'es-mx': es, + 'es-419': es, fr, + 'fr-ca': fr, ja, nl, no,