Skip to content

Commit 48be97c

Browse files
authored
Require to explciitly provide default translation bundle to DefaultTranslation: (#165)
* Export default (fallback) bundle for `en` language: `DefaultTranslationBundle`.
1 parent c272b47 commit 48be97c

6 files changed

Lines changed: 31 additions & 28 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
3434
* Auto-collapse items after resize if the final size is less than `minSize` for that item;
3535
* Prevent toggle buttons on `WorkspaceLayoutRow` children from being partially hidden when corresponding item is collapsed.
3636
- Export `TranslationProvider` and `DefaultTranslation` to be able to use `useTranslation()` outside the workspace component:
37+
* Export default (fallback) bundle for `en` language: `DefaultTranslationBundle`.
3738
* Remove deprecated `Translation.formatIri()` method (use `DataLocaleProvider.formatIri()` instead).
3839
- Extend `CommandBatch.discard()` to accept `revert` option to be able to revert the batch without storing it first.
3940
- Always display ungroup buttons on `StandardGroup` when the element is single-selected.

examples/i18n.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ function I18nExample() {
3838
'toolbar_action': {
3939
'layout.label': 'Layout the graph',
4040
},
41-
}
41+
},
42+
Reactodia.DefaultTranslationBundle,
4243
],
4344
}),
4445
defaultLayout,

src/diagram/locale.tsx

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,47 @@ import {
88

99
import * as Rdf from '../data/rdf/rdfModel';
1010

11+
/**
12+
* Default (fallback) translation bundle `en` language.
13+
*
14+
* @category Constants
15+
*/
1116
export const DefaultTranslationBundle: TranslationBundle = DefaultBundle;
1217

1318
/**
1419
* Default built-in implementation for i18n strings interpolation and other
1520
* methods from {@link Translation} interface.
21+
*
22+
* @category Core
1623
*/
17-
export class DefaultTranslation implements Translation {
24+
export class DefaultTranslation<K extends string = TranslationKey> implements Translation<K> {
1825
protected readonly bundles: ReadonlyArray<Partial<TranslationBundle>>;
1926

2027
private readonly _selectLabel: LabelLanguageSelector;
2128

2229
constructor(options?: {
2330
/**
24-
* Additional translation bundles for UI text strings in the workspace
31+
* Translation bundles for UI text strings in the workspace
2532
* in order from higher to lower priority.
2633
*
2734
* @default []
28-
* @see {@link useDefaultTranslation}
35+
* @see {@link DefaultTranslationBundle}
2936
*/
3037
bundles?: ReadonlyArray<Partial<TranslationBundle>>;
31-
/**
32-
* If set, disables translation fallback which (with default `en` language).
33-
*
34-
* @default true
35-
* @see {@link translations}
36-
*/
37-
useDefaultBundle?: boolean;
3838
/**
3939
* Overrides how a single label gets selected from multiple of them based on target language.
4040
*/
4141
selectLabel?: LabelLanguageSelector;
4242
}) {
4343
const {
4444
bundles = [],
45-
useDefaultBundle = true,
4645
selectLabel = defaultSelectLabel,
4746
} = options ?? {};
48-
const translationBundles: Partial<TranslationBundle>[] = [...bundles];
49-
if (useDefaultBundle) {
50-
translationBundles.push(DefaultTranslationBundle);
51-
}
52-
this.bundles = translationBundles;
47+
this.bundles = bundles;
5348
this._selectLabel = selectLabel;
5449
}
5550

56-
private getString(key: TranslationKey): string | undefined {
51+
private getString(key: K): string | undefined {
5752
const dotIndex = key.indexOf('.');
5853
if (!(dotIndex > 0 && dotIndex < key.length)) {
5954
throw new Error(`Reactodia: Invalid translation key: ${key}`);
@@ -69,12 +64,12 @@ export class DefaultTranslation implements Translation {
6964
return undefined;
7065
}
7166

72-
text(key: TranslationKey, placeholders?: Record<string, string | number | boolean>): string {
67+
text(key: K, placeholders?: Record<string, string | number | boolean>): string {
7368
return this.textOptional(key, placeholders) ?? key;
7469
}
7570

7671
textOptional(
77-
key: TranslationKey,
72+
key: K,
7873
placeholders?: Record<string, string | number | boolean>
7974
): string | undefined {
8075
const template = this.getString(key);
@@ -84,7 +79,7 @@ export class DefaultTranslation implements Translation {
8479
return formatPlaceholders(template, placeholders);
8580
}
8681

87-
template(key: TranslationKey, parts: Record<string, React.ReactNode>): React.ReactNode {
82+
template(key: K, parts: Record<string, React.ReactNode>): React.ReactNode {
8883
const template = this.getString(key) ?? key;
8984
return templatePlaceholders(template, parts);
9085
}

src/workspace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export {
102102
LinkVertices, type LinkVerticesProps,
103103
} from './diagram/linkLayer';
104104
export { DefaultLinkRouter, type DefaultLinkRouterOptions } from './diagram/linkRouter';
105-
export { DefaultTranslation } from './diagram/locale';
105+
export { DefaultTranslation, DefaultTranslationBundle } from './diagram/locale';
106106
export { type DiagramModel, type DiagramModelEvents, type GraphStructure } from './diagram/model';
107107
export { CanvasPlaceAt, type CanvasPlaceAtLayer } from './diagram/placeLayer';
108108
export {

src/workspace/workspaceProvider.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { CommandHistory, InMemoryHistory } from '../diagram/history';
1818
import {
1919
CalculatedLayout, LayoutFunction, LayoutTypeProvider, calculateLayout, applyLayout,
2020
} from '../diagram/layout';
21-
import { DefaultTranslation } from '../diagram/locale';
21+
import { DefaultTranslation, DefaultTranslationBundle } from '../diagram/locale';
2222
import { RenameLinkToLinkStateProvider, SharedCanvasState } from '../diagram/sharedCanvasState';
2323

2424
import { AnnotationElement, AnnotationLink } from '../editor/annotationCells';
@@ -52,7 +52,8 @@ export interface CreateWorkspaceParams {
5252
/**
5353
* Overrides default i18n (translation) implementation.
5454
*
55-
* By default, {@link DefaultTranslation} instance is used.
55+
* By default, {@link DefaultTranslation} instance with a single
56+
* {@link DefaultTranslationBundle} is used.
5657
*/
5758
translation?: Translation;
5859
/**
@@ -224,7 +225,9 @@ class RefCountedWorkspaceContext implements WorkspaceContext {
224225

225226
constructor(params: CreateWorkspaceParams) {
226227
const {
227-
translation = new DefaultTranslation(),
228+
translation = new DefaultTranslation({
229+
bundles: [DefaultTranslationBundle],
230+
}),
228231
history = new InMemoryHistory(),
229232
dialogSettingsProvider = new DefaultDialogSettingsProvider(),
230233
metadataProvider,

src/workspace/workspaceWrapper.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { TypeStyleResolver, RenameLinkProvider } from '../diagram/customization'
1212
import { Element } from '../diagram/elements';
1313
import { CommandHistory, InMemoryHistory } from '../diagram/history';
1414
import { LayoutFunction } from '../diagram/layout';
15-
import { DefaultTranslation } from '../diagram/locale';
15+
import { DefaultTranslation, DefaultTranslationBundle } from '../diagram/locale';
1616

1717
import { EntityElement } from '../editor/dataElements';
1818
import {
@@ -151,10 +151,13 @@ export class Workspace extends React.Component<WorkspaceProps> {
151151
onWorkspaceEvent,
152152
} = this.props;
153153

154+
let bundles = translations;
155+
if (useDefaultTranslation) {
156+
bundles = [...translations, DefaultTranslationBundle];
157+
}
154158
this._workspace = createWorkspace({
155159
translation: this.context ?? new DefaultTranslation({
156-
bundles: translations,
157-
useDefaultBundle: useDefaultTranslation,
160+
bundles,
158161
selectLabel: selectLabelLanguage,
159162
}),
160163
history,

0 commit comments

Comments
 (0)