mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-12-01 13:34:52 +00:00
Enforce api.TranslationLanguage parity and regional OpenAPI locales (#4866)
This commit is contained in:
@@ -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.
|
||||
@@ -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],
|
||||
|
||||
@@ -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<string, LanguageDefinition>;
|
||||
// Require every API translation language while still allowing locales added ahead of @gitbook/api.
|
||||
} satisfies Record<APITranslationLanguage, LanguageDefinition> & Record<string, LanguageDefinition>;
|
||||
|
||||
export type TranslationLocale = keyof typeof languageDefinitions;
|
||||
|
||||
|
||||
@@ -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<typeof resolveOpenAPIContext>[0]['icons'],
|
||||
renderCodeBlock: () => null,
|
||||
renderHeading: () => null,
|
||||
renderDocument: () => null,
|
||||
});
|
||||
|
||||
expect(resolved.translation).toBe(expected);
|
||||
});
|
||||
});
|
||||
@@ -15,7 +15,10 @@ export const translations = {
|
||||
en,
|
||||
de,
|
||||
es,
|
||||
'es-mx': es,
|
||||
'es-419': es,
|
||||
fr,
|
||||
'fr-ca': fr,
|
||||
ja,
|
||||
nl,
|
||||
no,
|
||||
|
||||
Reference in New Issue
Block a user