Skip to content

Commit 920576c

Browse files
committed
feat: translate additional labels
1 parent 02a29ef commit 920576c

File tree

11 files changed

+144
-79
lines changed

11 files changed

+144
-79
lines changed

packages/docusaurus-plugin-openapi-docs/src/markdown/createRequestHeader.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@
77

88
import { create } from "./utils";
99

10-
export function createRequestHeader(header: string) {
10+
export function createRequestHeader() {
1111
return [
12-
create(
13-
"Heading",
14-
{
15-
children: header,
16-
id: header.replace(" ", "-").toLowerCase(),
17-
as: "h2",
18-
className: "openapi-tabs__heading",
19-
},
20-
{ inline: true }
21-
),
22-
`\n\n`,
12+
create("Heading", {
13+
id: "request",
14+
as: "h2",
15+
className: "openapi-tabs__heading",
16+
children: [
17+
"{translate({ id: 'theme.openapi.request.title', message: 'Request' })}",
18+
],
19+
}),
20+
"\n\n",
2321
];
2422
}

packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ function createDetailsNode(
375375
() => [
376376
create("span", {
377377
className: "openapi-schema__required",
378-
children: "required",
378+
children:
379+
"{translate({ id: 'theme.openapi.schemaItem.required', message: 'required' })}",
379380
}),
380381
]
381382
),
@@ -530,7 +531,8 @@ function createPropertyDiscriminator(
530531
guard(required, () => [
531532
create("span", {
532533
className: "openapi-schema__required",
533-
children: "required",
534+
children:
535+
"{translate({ id: 'theme.openapi.schemaItem.required', message: 'required' })}",
534536
}),
535537
]),
536538
],

packages/docusaurus-plugin-openapi-docs/src/markdown/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export function createApiPageMD({
7272
`import StatusCodes from "@theme/StatusCodes";\n`,
7373
`import OperationTabs from "@theme/OperationTabs";\n`,
7474
`import TabItem from "@theme/TabItem";\n`,
75-
`import Heading from "@theme/Heading";\n\n`,
75+
`import Heading from "@theme/Heading";\n`,
76+
`import { translate } from "@docusaurus/Translate";\n\n`,
7677
createHeading(title),
7778
createMethodEndpoint(method, path),
7879
infoPath && createAuthorization(infoPath),
@@ -81,7 +82,7 @@ export function createApiPageMD({
8182
: undefined,
8283
createDeprecationNotice({ deprecated, description: deprecatedDescription }),
8384
createDescription(description),
84-
requestBody || parameters ? createRequestHeader("Request") : undefined,
85+
requestBody || parameters ? createRequestHeader() : undefined,
8586
createParamsDetails({ parameters }),
8687
createRequestBodyDetails({
8788
title: "Body",

packages/docusaurus-theme-openapi-docs/src/markdown/schema.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
* ========================================================================== */
77

8+
import { translate } from "@docusaurus/Translate";
9+
import { OPENAPI_SCHEMA_ITEM } from "../theme/translationIds";
810
import { SchemaObject } from "../types";
911

1012
function prettyName(schema: SchemaObject, circular?: boolean) {
@@ -72,7 +74,10 @@ export function getQualifierMessage(schema?: SchemaObject): string | undefined {
7274
return getQualifierMessage(schema.items);
7375
}
7476

75-
let message = "**Possible values:** ";
77+
let message = `**${translate({
78+
id: OPENAPI_SCHEMA_ITEM.POSSIBLE_VALUES,
79+
message: "Possible values:",
80+
})}** `;
7681

7782
let qualifierGroups = [];
7883

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Authorization/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ function Authorization() {
3434
return (
3535
<div>
3636
{optionKeys.length > 1 && (
37-
<FormItem label="Security Scheme">
37+
<FormItem
38+
label={translate({
39+
id: OPENAPI_AUTH.SECURITY_SCHEME,
40+
message: "Security Scheme",
41+
})}
42+
>
3843
<FormSelect
3944
options={optionKeys}
4045
value={selected}

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/SecuritySchemes/index.tsx

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,41 @@ function SecuritySchemes(props: any) {
2525
}
2626

2727
const selectedAuth = options[selected];
28+
29+
const keyTranslations: Record<string, { id: string; message: string }> = {
30+
description: {
31+
id: OPENAPI_SECURITY_SCHEMES.DESCRIPTION,
32+
message: "description:",
33+
},
34+
scheme: {
35+
id: OPENAPI_SECURITY_SCHEMES.SCHEME,
36+
message: "scheme:",
37+
},
38+
bearerFormat: {
39+
id: OPENAPI_SECURITY_SCHEMES.BEARER_FORMAT,
40+
message: "bearerFormat:",
41+
},
42+
openIdConnectUrl: {
43+
id: OPENAPI_SECURITY_SCHEMES.OPEN_ID_CONNECT_URL,
44+
message: "openIdConnectUrl:",
45+
},
46+
};
47+
48+
const renderRest = (rest: Record<string, any>) =>
49+
Object.keys(rest).map((k) => {
50+
const translation = keyTranslations[k];
51+
const label = translation
52+
? translate({ id: translation.id, message: translation.message })
53+
: `${k}:`;
54+
return (
55+
<span key={k}>
56+
<strong>{label} </strong>
57+
{typeof rest[k] === "object"
58+
? JSON.stringify(rest[k], null, 2)
59+
: String(rest[k])}
60+
</span>
61+
);
62+
});
2863
return (
2964
<details className="openapi-security__details" open={false}>
3065
<summary className="openapi-security__summary-container">
@@ -81,16 +116,7 @@ function SecuritySchemes(props: any) {
81116
</code>
82117
</span>
83118
)}
84-
{Object.keys(rest).map((k, i) => {
85-
return (
86-
<span key={k}>
87-
<strong>{k}: </strong>
88-
{typeof rest[k] === "object"
89-
? JSON.stringify(rest[k], null, 2)
90-
: String(rest[k])}
91-
</span>
92-
);
93-
})}
119+
{renderRest(rest)}
94120
</pre>
95121
</React.Fragment>
96122
);
@@ -137,16 +163,7 @@ function SecuritySchemes(props: any) {
137163
</code>
138164
</span>
139165
)}
140-
{Object.keys(rest).map((k, i) => {
141-
return (
142-
<span key={k}>
143-
<strong>{k}: </strong>
144-
{typeof rest[k] === "object"
145-
? JSON.stringify(rest[k], null, 2)
146-
: String(rest[k])}
147-
</span>
148-
);
149-
})}
166+
{renderRest(rest)}
150167
</pre>
151168
</React.Fragment>
152169
);
@@ -234,16 +251,7 @@ function SecuritySchemes(props: any) {
234251
</code>
235252
</span>
236253
)}
237-
{Object.keys(rest).map((k, i) => {
238-
return (
239-
<span key={k}>
240-
<strong>{k}: </strong>
241-
{typeof rest[k] === "object"
242-
? JSON.stringify(rest[k], null, 2)
243-
: String(rest[k])}
244-
</span>
245-
);
246-
})}
254+
{renderRest(rest)}
247255
</pre>
248256
</React.Fragment>
249257
);
@@ -291,16 +299,7 @@ function SecuritySchemes(props: any) {
291299
</code>
292300
</span>
293301
)}
294-
{Object.keys(rest).map((k, i) => {
295-
return (
296-
<span key={k}>
297-
<strong>{k}: </strong>
298-
{typeof rest[k] === "object"
299-
? JSON.stringify(rest[k], null, 2)
300-
: String(rest[k])}
301-
</span>
302-
);
303-
})}
302+
{renderRest(rest)}
304303
{flows && (
305304
<span>
306305
<code>
@@ -361,16 +360,7 @@ function SecuritySchemes(props: any) {
361360
</code>
362361
</span>
363362
)}
364-
{Object.keys(rest).map((k, i) => {
365-
return (
366-
<span key={k}>
367-
<strong>{k}: </strong>
368-
{typeof rest[k] === "object"
369-
? JSON.stringify(rest[k], null, 2)
370-
: String(rest[k])}
371-
</span>
372-
);
373-
})}
363+
{renderRest(rest)}
374364
</pre>
375365
</React.Fragment>
376366
);

packages/docusaurus-theme-openapi-docs/src/theme/ParamsItem/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ export interface Props {
4242

4343
const getEnumDescriptionMarkdown = (enumDescriptions?: [string, string][]) => {
4444
if (enumDescriptions?.length) {
45-
return `| Enum Value | Description |
45+
const enumValue = translate({
46+
id: OPENAPI_SCHEMA_ITEM.ENUM_VALUE,
47+
message: "Enum Value",
48+
});
49+
const description = translate({
50+
id: OPENAPI_SCHEMA_ITEM.ENUM_DESCRIPTION,
51+
message: "Description",
52+
});
53+
return `| ${enumValue} | ${description} |
4654
| ---- | ----- |
4755
${enumDescriptions
4856
.map((desc) => {

packages/docusaurus-theme-openapi-docs/src/theme/RequestSchema/index.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import React from "react";
99

1010
import { translate } from "@docusaurus/Translate";
11-
import { OPENAPI_SCHEMA_ITEM } from "@theme/translationIds";
11+
import { OPENAPI_REQUEST, OPENAPI_SCHEMA_ITEM } from "@theme/translationIds";
1212

1313
import BrowserOnly from "@docusaurus/BrowserOnly";
1414
import Details from "@theme/Details";
@@ -67,7 +67,10 @@ const RequestSchemaComponent: React.FC<Props> = ({ title, body, style }) => {
6767
<>
6868
<summary>
6969
<h3 className="openapi-markdown__details-summary-header-body">
70-
{title}
70+
{translate({
71+
id: OPENAPI_REQUEST.BODY_TITLE,
72+
message: title,
73+
})}
7174
{body.required === true && (
7275
<span className="openapi-schema__required">
7376
{translate({
@@ -120,7 +123,10 @@ const RequestSchemaComponent: React.FC<Props> = ({ title, body, style }) => {
120123
<>
121124
<summary>
122125
<h3 className="openapi-markdown__details-summary-header-body">
123-
{title}
126+
{translate({
127+
id: OPENAPI_REQUEST.BODY_TITLE,
128+
message: title,
129+
})}
124130
{firstBody.type === "array" && (
125131
<span style={{ opacity: "0.6" }}> array</span>
126132
)}

packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import React from "react";
99

10+
import { translate } from "@docusaurus/Translate";
11+
import { OPENAPI_SCHEMA_ITEM } from "@theme/translationIds";
12+
1013
import { ClosingArrayBracket, OpeningArrayBracket } from "@theme/ArrayBrackets";
1114
import Details from "@theme/Details";
1215
import DiscriminatorTabs from "@theme/DiscriminatorTabs";
@@ -89,12 +92,29 @@ const Summary: React.FC<SummaryProps> = ({
8992
{(isRequired || deprecated || nullable) && (
9093
<span className="openapi-schema__divider" />
9194
)}
92-
{nullable && <span className="openapi-schema__nullable">nullable</span>}
95+
{nullable && (
96+
<span className="openapi-schema__nullable">
97+
{translate({
98+
id: OPENAPI_SCHEMA_ITEM.NULLABLE,
99+
message: "nullable",
100+
})}
101+
</span>
102+
)}
93103
{isRequired && (
94-
<span className="openapi-schema__required">required</span>
104+
<span className="openapi-schema__required">
105+
{translate({
106+
id: OPENAPI_SCHEMA_ITEM.REQUIRED,
107+
message: "required",
108+
})}
109+
</span>
95110
)}
96111
{deprecated && (
97-
<span className="openapi-schema__deprecated">deprecated</span>
112+
<span className="openapi-schema__deprecated">
113+
{translate({
114+
id: OPENAPI_SCHEMA_ITEM.DEPRECATED,
115+
message: "deprecated",
116+
})}
117+
</span>
98118
)}
99119
</span>
100120
</summary>
@@ -108,14 +128,17 @@ interface SchemaProps {
108128
}
109129

110130
const AnyOneOf: React.FC<SchemaProps> = ({ schema, schemaType }) => {
111-
const type = schema.oneOf ? "oneOf" : "anyOf";
131+
const key = schema.oneOf ? "oneOf" : "anyOf";
132+
const type = schema.oneOf
133+
? translate({ id: OPENAPI_SCHEMA_ITEM.ONE_OF, message: "oneOf" })
134+
: translate({ id: OPENAPI_SCHEMA_ITEM.ANY_OF, message: "anyOf" });
112135
return (
113136
<>
114137
<span className="badge badge--info" style={{ marginBottom: "1rem" }}>
115138
{type}
116139
</span>
117140
<SchemaTabs>
118-
{schema[type]?.map((anyOneSchema: any, index: number) => {
141+
{schema[key]?.map((anyOneSchema: any, index: number) => {
119142
const label = anyOneSchema.title || anyOneSchema.type;
120143
return (
121144
// @ts-ignore
@@ -254,7 +277,12 @@ const PropertyDiscriminator: React.FC<SchemaEdgeProps> = ({
254277
)}
255278
{required && <span className="openapi-schema__divider"></span>}
256279
{required && (
257-
<span className="openapi-schema__required">required</span>
280+
<span className="openapi-schema__required">
281+
{translate({
282+
id: OPENAPI_SCHEMA_ITEM.REQUIRED,
283+
message: "required",
284+
})}
285+
</span>
258286
)}
259287
</span>
260288
<div style={{ marginLeft: "1rem" }}>
@@ -843,6 +871,9 @@ const SchemaNode: React.FC<SchemaProps> = ({ schema, schemaType }) => {
843871

844872
return (
845873
<div>
874+
<span className="badge badge--info" style={{ marginBottom: "1rem" }}>
875+
{translate({ id: OPENAPI_SCHEMA_ITEM.ALL_OF, message: "allOf" })}
876+
</span>
846877
{mergedSchemas.oneOf && (
847878
<AnyOneOf schema={mergedSchemas} schemaType={schemaType} />
848879
)}

packages/docusaurus-theme-openapi-docs/src/theme/SchemaItem/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ const transformEnumDescriptions = (
3838

3939
const getEnumDescriptionMarkdown = (enumDescriptions?: [string, string][]) => {
4040
if (enumDescriptions?.length) {
41-
return `| Enum Value | Description |
41+
const enumValue = translate({
42+
id: OPENAPI_SCHEMA_ITEM.ENUM_VALUE,
43+
message: "Enum Value",
44+
});
45+
const description = translate({
46+
id: OPENAPI_SCHEMA_ITEM.ENUM_DESCRIPTION,
47+
message: "Description",
48+
});
49+
return `| ${enumValue} | ${description} |
4250
| ---- | ----- |
4351
${enumDescriptions
4452
.map((desc) => {

0 commit comments

Comments
 (0)