Skip to content

Commit 8ed1bda

Browse files
authored
Translate OpenAPI blocks (#3166)
1 parent d67699a commit 8ed1bda

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+873
-198
lines changed

.changeset/light-moons-press.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gitbook/react-openapi": patch
3+
"gitbook": patch
4+
---
5+
6+
Translate OpenAPI blocks

packages/gitbook/e2e/customers.spec.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ const testCases: TestsCase[] = [
1010
{ name: 'OpenAPI', url: '/snyk-api/reference/apps', run: waitForCookiesDialog },
1111
],
1212
},
13-
{
14-
name: 'Nexthink',
15-
contentBaseURL: 'https://docs.nexthink.com',
16-
tests: [
17-
{
18-
name: 'Home',
19-
url: '/',
20-
screenshot: { waitForTOCScrolling: false },
21-
run: waitForCookiesDialog,
22-
},
23-
],
24-
},
13+
// {
14+
// name: 'Nexthink',
15+
// contentBaseURL: 'https://docs.nexthink.com',
16+
// tests: [
17+
// {
18+
// name: 'Home',
19+
// url: '/',
20+
// screenshot: { waitForTOCScrolling: false },
21+
// run: waitForCookiesDialog,
22+
// },
23+
// ],
24+
// },
2525
{
2626
name: 'asiksupport-stg.dto.kemkes.go.id',
2727
contentBaseURL: 'https://asiksupport-stg.dto.kemkes.go.id',
@@ -157,11 +157,11 @@ const testCases: TestsCase[] = [
157157
contentBaseURL: 'https://wiki.redmodding.org',
158158
tests: [{ name: 'Home', url: '/' }],
159159
},
160-
{
161-
name: 'docs.cherry-ai.com',
162-
contentBaseURL: 'https://docs.cherry-ai.com',
163-
tests: [{ name: 'Home', url: '/', run: waitForCookiesDialog }],
164-
},
160+
// {
161+
// name: 'docs.cherry-ai.com',
162+
// contentBaseURL: 'https://docs.cherry-ai.com',
163+
// tests: [{ name: 'Home', url: '/', run: waitForCookiesDialog }],
164+
// },
165165
{
166166
name: 'docs.snyk.io',
167167
contentBaseURL: 'https://docs.snyk.io',

packages/gitbook/src/components/DocumentView/OpenAPI/OpenAPIOperation.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async function OpenAPIOperationBody(props: BlockProps<AnyOpenAPIOperationsBlock>
5151
return (
5252
<BaseOpenAPIOperation
5353
data={data}
54-
context={getOpenAPIContext({ props, specUrl })}
54+
context={getOpenAPIContext({ props, specUrl, context: context.contentContext })}
5555
className="openapi-block"
5656
/>
5757
);

packages/gitbook/src/components/DocumentView/OpenAPI/OpenAPISchemas.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async function OpenAPISchemasBody(props: BlockProps<OpenAPISchemasBlock>) {
5151
<BaseOpenAPISchemas
5252
schemas={data.schemas}
5353
grouped={block.data.grouped}
54-
context={getOpenAPIContext({ props, specUrl })}
54+
context={getOpenAPIContext({ props, specUrl, context: context.contentContext })}
5555
className="openapi-block"
5656
/>
5757
);

packages/gitbook/src/components/DocumentView/OpenAPI/OpenAPIWebhook.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async function OpenAPIWebhookBody(props: BlockProps<OpenAPIWebhookBlock>) {
5151
return (
5252
<BaseOpenAPIWebhook
5353
data={data}
54-
context={getOpenAPIContext({ props, specUrl })}
54+
context={getOpenAPIContext({ props, specUrl, context: context.contentContext })}
5555
className="openapi-block"
5656
/>
5757
);

packages/gitbook/src/components/DocumentView/OpenAPI/context.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { JSONDocument } from '@gitbook/api';
22
import { Icon } from '@gitbook/icons';
3-
import type { OpenAPIContext } from '@gitbook/react-openapi';
3+
import { type OpenAPIContextInput, checkIsValidLocale } from '@gitbook/react-openapi';
44

55
import { tcls } from '@/lib/tailwind';
66

@@ -11,21 +11,31 @@ import { Heading } from '../Heading';
1111

1212
import './scalar.css';
1313
import './style.css';
14+
import { DEFAULT_LOCALE, getCustomizationLocale } from '@/intl/server';
1415
import type {
1516
AnyOpenAPIOperationsBlock,
1617
OpenAPISchemasBlock,
1718
OpenAPIWebhookBlock,
1819
} from '@/lib/openapi/types';
20+
import type { GitBookAnyContext } from '@v2/lib/context';
1921

2022
/**
2123
* Get the OpenAPI context to render a block.
2224
*/
2325
export function getOpenAPIContext(args: {
2426
props: BlockProps<AnyOpenAPIOperationsBlock | OpenAPISchemasBlock | OpenAPIWebhookBlock>;
2527
specUrl: string;
26-
}): OpenAPIContext {
27-
const { props, specUrl } = args;
28+
context: GitBookAnyContext | undefined;
29+
}): OpenAPIContextInput {
30+
const { props, specUrl, context } = args;
2831
const { block } = props;
32+
33+
const customization = context && 'customization' in context ? context.customization : null;
34+
const customizationLocale = customization
35+
? getCustomizationLocale(customization)
36+
: DEFAULT_LOCALE;
37+
const locale = checkIsValidLocale(customizationLocale) ? customizationLocale : DEFAULT_LOCALE;
38+
2939
return {
3040
specUrl,
3141
icons: {
@@ -73,5 +83,6 @@ export function getOpenAPIContext(args: {
7383
defaultInteractiveOpened: props.context.mode === 'print',
7484
id: block.meta?.id,
7585
blockKey: block.key,
86+
locale,
7687
};
7788
}

packages/gitbook/src/components/DocumentView/OpenAPI/style.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,19 @@
229229
}
230230

231231
.openapi-schema-required {
232-
@apply text-warning-subtle text-[0.813rem];
232+
@apply text-warning-subtle text-[0.813rem] lowercase;
233233
}
234234

235235
.openapi-schema-optional {
236-
@apply text-info-subtle text-[0.813rem];
236+
@apply text-info-subtle text-[0.813rem] lowercase;
237237
}
238238

239239
.openapi-schema-readonly {
240-
@apply text-primary-subtle/9 text-[0.813rem];
240+
@apply text-primary-subtle/9 text-[0.813rem] lowercase;
241241
}
242242

243243
.openapi-schema-writeonly {
244-
@apply text-success dark:text-success-subtle/9 text-[0.813rem];
244+
@apply text-success dark:text-success-subtle/9 text-[0.813rem] lowercase;
245245
}
246246

247247
.openapi-schema-type {

packages/gitbook/src/intl/server.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@ import { type TranslationLanguage, languages } from './translations';
44

55
export * from './translate';
66

7+
export const DEFAULT_LOCALE = 'en';
8+
9+
/**
10+
* Get the locale of the customization.
11+
*/
12+
export function getCustomizationLocale(customization: SiteCustomizationSettings): string {
13+
return customization.internationalization.locale;
14+
}
15+
716
/**
817
* Create the translation context for a space to use in the server components.
918
*/
1019
export function getSpaceLanguage(customization: SiteCustomizationSettings): TranslationLanguage {
11-
const fallback = languages.en;
20+
const fallback = languages[DEFAULT_LOCALE];
1221

13-
const { locale } = customization.internationalization;
22+
const locale = getCustomizationLocale(customization);
1423

1524
let language = fallback;
1625
// @ts-ignore
17-
if (locale !== 'en' && languages[locale]) {
26+
if (locale !== DEFAULT_LOCALE && languages[locale]) {
1827
// @ts-ignore
1928
language = languages[locale];
2029
}

packages/gitbook/src/intl/translations/translations.test.ts

Whitespace-only changes.

packages/react-openapi/src/InteractiveSection.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export function InteractiveSection(props: {
102102
) : null}
103103
{header}
104104
</SectionHeaderContent>
105+
{/* biome-ignore lint/a11y/useKeyWithClickEvents: we prevent default here */}
105106
<div
106107
className={clsx(
107108
'openapi-section-header-controls',

0 commit comments

Comments
 (0)