Skip to content

Commit b0e660e

Browse files
committed
feat: basic UI labels configuration
This can be used for translations later
1 parent fdcac30 commit b0e660e

File tree

6 files changed

+62
-12
lines changed

6 files changed

+62
-12
lines changed

src/components/Fields/EnumValues.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as React from 'react';
22
import { ExampleValue, FieldLabel } from '../../common-elements/fields';
33

4+
import { l } from '../../services/Labels';
5+
46
export interface EnumValuesProps {
57
values: string[];
68
type: string;
@@ -16,12 +18,11 @@ export class EnumValues extends React.PureComponent<EnumValuesProps> {
1618
return (
1719
<div>
1820
<FieldLabel>
19-
{type === 'array' ? 'Items' : ''} {values.length === 1 ? 'Value' : 'Enum'}:
20-
</FieldLabel>{' '}
21+
{type === 'array' ? l('enumArray') : ''}{' '}
22+
{values.length === 1 ? l('enumSingleValue') : l('enum')}:
23+
</FieldLabel>
2124
{values.map((value, idx) => (
22-
<ExampleValue key={idx}>
23-
{JSON.stringify(value)}
24-
</ExampleValue>
25+
<ExampleValue key={idx}>{JSON.stringify(value)}</ExampleValue>
2526
))}
2627
</div>
2728
);

src/components/Fields/FieldDetails.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { FieldDetail } from './FieldDetail';
1919

2020
import { Badge } from '../../common-elements/';
2121

22+
import { l } from '../../services/Labels';
23+
2224
export class FieldDetails extends React.PureComponent<FieldProps> {
2325
render() {
2426
const { showExamples, field, renderDiscriminatorSwitch } = this.props;
@@ -40,18 +42,18 @@ export class FieldDetails extends React.PureComponent<FieldProps> {
4042
)}
4143
{schema.title && <TypeTitle> ({schema.title}) </TypeTitle>}
4244
<ConstraintsView constraints={schema.constraints} />
43-
{schema.nullable && <NullableLabel> Nullable </NullableLabel>}
45+
{schema.nullable && <NullableLabel> {l('nullable')} </NullableLabel>}
4446
{schema.pattern && <PatternLabel>{schema.pattern}</PatternLabel>}
45-
{schema.isCircular && <RecursiveLabel> Recursive </RecursiveLabel>}
47+
{schema.isCircular && <RecursiveLabel> {l('recursive')} </RecursiveLabel>}
4648
</div>
4749
{deprecated && (
4850
<div>
49-
<Badge type="warning"> Deprecated </Badge>
51+
<Badge type="warning"> {l('deprecated')} </Badge>
5052
</div>
5153
)}
52-
<FieldDetail label={'Default:'} value={schema.default} />
54+
<FieldDetail label={l('default') + ':'} value={schema.default} />
5355
{!renderDiscriminatorSwitch && <EnumValues type={schema.type} values={schema.enum} />}{' '}
54-
{showExamples && <FieldDetail label={'Example:'} value={example} />}
56+
{showExamples && <FieldDetail label={l('example') + ':'} value={example} />}
5557
{<Extensions extensions={{ ...field.extensions, ...schema.extensions }} />}
5658
<div>
5759
<Markdown compact={true} source={description} />

src/components/Schema/Schema.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { ArraySchema } from './ArraySchema';
1010
import { ObjectSchema } from './ObjectSchema';
1111
import { OneOfSchema } from './OneOfSchema';
1212

13+
import { l } from '../../services/Labels';
14+
1315
export interface SchemaOptions {
1416
showTitle?: boolean;
1517
skipReadOnly?: boolean;
@@ -34,7 +36,7 @@ export class Schema extends React.Component<Partial<SchemaProps>> {
3436
<div>
3537
<TypeName>{schema.displayType}</TypeName>
3638
{schema.title && <TypeTitle> {schema.title} </TypeTitle>}
37-
<RecursiveLabel> Recursive </RecursiveLabel>
39+
<RecursiveLabel> {l('recursive')} </RecursiveLabel>
3840
</div>
3941
);
4042
}

src/services/Labels.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export interface LabelsConfig {
2+
enum: string;
3+
enumSingleValue: string;
4+
enumArray: string;
5+
default: string;
6+
deprecated: string;
7+
example: string;
8+
nullable: string;
9+
recursive: string;
10+
arrayOf: string;
11+
}
12+
13+
export type LabelsConfigRaw = Partial<LabelsConfig>;
14+
15+
const labels: LabelsConfig = {
16+
enum: 'Enum',
17+
enumSingleValue: 'Value',
18+
enumArray: 'Items',
19+
default: 'Default',
20+
deprecated: 'Deprecated',
21+
example: 'Example',
22+
nullable: 'Nullable',
23+
recursive: 'Recursive',
24+
arrayOf: 'Array of ',
25+
};
26+
27+
export function setRedocLabels(_labels?: LabelsConfigRaw) {
28+
Object.assign(labels, _labels);
29+
}
30+
31+
export function l(key: keyof LabelsConfig, idx?: number): string {
32+
const label = labels[key];
33+
if (idx !== undefined) {
34+
return label[idx];
35+
}
36+
return label;
37+
}

src/services/RedocNormalizedOptions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import defaultTheme, { ResolvedThemeInterface, resolveTheme, ThemeInterface } fr
22
import { querySelector } from '../utils/dom';
33
import { isNumeric, mergeObjects } from '../utils/helpers';
44

5+
import { LabelsConfigRaw, setRedocLabels } from './Labels';
56
import { MDXComponentMeta } from './MarkdownRenderer';
67

78
export interface RedocRawOptions {
@@ -25,6 +26,8 @@ export interface RedocRawOptions {
2526
unstable_ignoreMimeParameters?: boolean;
2627

2728
allowedMdComponents?: Dict<MDXComponentMeta>;
29+
30+
labels?: LabelsConfigRaw;
2831
}
2932

3033
function argValueToBoolean(val?: string | boolean): boolean {
@@ -136,6 +139,9 @@ export class RedocNormalizedOptions {
136139

137140
this.theme.extensionsHook = hook as any;
138141

142+
// do not support dynamic labels changes. Labels should be configured before
143+
setRedocLabels(raw.labels);
144+
139145
this.scrollYOffset = RedocNormalizedOptions.normalizeScrollYOffset(raw.scrollYOffset);
140146
this.hideHostname = RedocNormalizedOptions.normalizeHideHostname(raw.hideHostname);
141147
this.expandResponses = RedocNormalizedOptions.normalizeExpandResponses(raw.expandResponses);

src/services/models/Schema.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
sortByRequired,
2020
} from '../../utils/';
2121

22+
import { l } from '../Labels';
23+
2224
// TODO: refactor this model, maybe use getters instead of copying all the values
2325
export class SchemaModel {
2426
pointer: string;
@@ -148,7 +150,7 @@ export class SchemaModel {
148150
this.items = new SchemaModel(parser, schema.items, this.pointer + '/items', this.options);
149151
this.displayType = pluralizeType(this.items.displayType);
150152
this.displayFormat = this.items.format;
151-
this.typePrefix = this.items.typePrefix + 'Array of ';
153+
this.typePrefix = this.items.typePrefix + l('arrayOf');
152154
this.title = this.title || this.items.title;
153155
this.isPrimitive = this.items.isPrimitive;
154156
if (this.example === undefined && this.items.example !== undefined) {

0 commit comments

Comments
 (0)