Skip to content

Commit b814678

Browse files
authored
Merge pull request #8 from jtpio/translator
Localize user-facing strings with `ITranslator`
2 parents bf80a2c + b2c752a commit b814678

File tree

6 files changed

+74
-31
lines changed

6 files changed

+74
-31
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"@jupyterlab/coreutils": "^6.0.0",
6767
"@jupyterlab/notebook": "^4.0.0",
6868
"@jupyterlab/services": "^7.0.0",
69+
"@jupyterlab/translation": "^4.0.0",
6970
"@jupyterlab/ui-components": "^4.0.0",
7071
"@lumino/widgets": "^2.0.0",
7172
"codemirror": "^6.0.2",

src/diff/codemirror.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export async function createCodeMirrorDiffWidget(
9191
cellFooterTracker,
9292
originalSource,
9393
newSource,
94+
trans,
9495
showActionButtons = true,
9596
openDiff = true
9697
} = options;
@@ -101,7 +102,8 @@ export async function createCodeMirrorDiffWidget(
101102
cell,
102103
cellFooterTracker,
103104
showActionButtons,
104-
openDiff
105+
openDiff,
106+
trans
105107
});
106108

107109
diffWidget.addClass('jupyterlab-cell-diff');

src/diff/nbdime.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ export async function createNBDimeDiffWidget(
4848
originalSource,
4949
newSource,
5050
diffData,
51+
trans,
5152
showActionButtons = true,
5253
openDiff = true
5354
} = options;
5455

5556
if (!diffData || !diffData.diff) {
56-
throw new Error('NBDime strategy requires diff data');
57+
throw new Error(trans.__('NBDime strategy requires diff data'));
5758
}
5859

5960
const diff = createPatchStringDiffModel(
@@ -68,7 +69,8 @@ export async function createNBDimeDiffWidget(
6869
originalSource,
6970
newSource,
7071
showActionButtons,
71-
openDiff
72+
openDiff,
73+
trans
7274
});
7375

7476
diffWidget.addClass('jupyterlab-cell-diff');

src/plugin.ts

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ import {
44
} from '@jupyterlab/application';
55
import { ICellModel } from '@jupyterlab/cells';
66
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
7+
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
78
import { ICellFooterTracker } from 'jupyterlab-cell-input-footer';
89

910
import { requestAPI } from './handler';
1011
import { IDiffWidgetOptions } from './widget';
1112
import { createNBDimeDiffWidget } from './diff/nbdime';
1213
import { createCodeMirrorDiffWidget } from './diff/codemirror';
1314

15+
/**
16+
* The translation namespace for the plugin.
17+
*/
18+
const TRANSLATION_NAMESPACE = 'jupyterlab-cell-diff';
19+
1420
/**
1521
* Find a notebook by path using the notebook tracker
1622
*/
@@ -56,43 +62,50 @@ const codeMirrorPlugin: JupyterFrontEndPlugin<void> = {
5662
id: 'jupyterlab-cell-diff:codemirror-plugin',
5763
description: 'Expose a command to show cell diffs using CodeMirror',
5864
requires: [ICellFooterTracker, INotebookTracker],
65+
optional: [ITranslator],
5966
autoStart: true,
6067
activate: async (
6168
app: JupyterFrontEnd,
6269
cellFooterTracker: ICellFooterTracker,
63-
notebookTracker: INotebookTracker
70+
notebookTracker: INotebookTracker,
71+
translator: ITranslator | null
6472
) => {
6573
const { commands } = app;
74+
const trans = (translator ?? nullTranslator).load(TRANSLATION_NAMESPACE);
6675

6776
commands.addCommand('jupyterlab-cell-diff:show-codemirror', {
68-
label: 'Show Cell Diff (CodeMirror)',
77+
label: trans.__('Show Cell Diff (CodeMirror)'),
6978
describedBy: {
7079
args: {
7180
type: 'object',
7281
properties: {
7382
cellId: {
7483
type: 'string',
75-
description: 'ID of the cell to show diff for'
84+
description: trans.__('ID of the cell to show diff for')
7685
},
7786
originalSource: {
7887
type: 'string',
79-
description: 'Original source code to compare against'
88+
description: trans.__('Original source code to compare against')
8089
},
8190
newSource: {
8291
type: 'string',
83-
description: 'New source code to compare with'
92+
description: trans.__('New source code to compare with')
8493
},
8594
showActionButtons: {
8695
type: 'boolean',
87-
description: 'Whether to show action buttons in the diff widget'
96+
description: trans.__(
97+
'Whether to show action buttons in the diff widget'
98+
)
8899
},
89100
notebookPath: {
90101
type: 'string',
91-
description: 'Path to the notebook containing the cell'
102+
description: trans.__('Path to the notebook containing the cell')
92103
},
93104
openDiff: {
94105
type: 'boolean',
95-
description: 'Whether to open the diff widget automatically'
106+
description: trans.__(
107+
'Whether to open the diff widget automatically'
108+
)
96109
}
97110
}
98111
}
@@ -115,14 +128,16 @@ const codeMirrorPlugin: JupyterFrontEndPlugin<void> = {
115128
const cell = findCell(currentNotebook, cellId);
116129
if (!cell) {
117130
console.error(
118-
'Missing required arguments: cellId (or no active cell found)'
131+
trans.__(
132+
'Missing required arguments: cellId (or no active cell found)'
133+
)
119134
);
120135
return;
121136
}
122137

123138
const footer = cellFooterTracker.getFooter(cell.id);
124139
if (!footer) {
125-
console.error(`Footer not found for cell ${cell.id}`);
140+
console.error(trans.__('Footer not found for cell %1', cell.id));
126141
return;
127142
}
128143

@@ -133,12 +148,13 @@ const codeMirrorPlugin: JupyterFrontEndPlugin<void> = {
133148
originalSource,
134149
newSource,
135150
showActionButtons,
136-
openDiff
151+
openDiff,
152+
trans
137153
};
138154

139155
await createCodeMirrorDiffWidget(options);
140156
} catch (error) {
141-
console.error('Failed to create diff widget:', error);
157+
console.error(trans.__('Failed to create diff widget: %1'), error);
142158
}
143159
}
144160
});
@@ -152,43 +168,50 @@ const nbdimePlugin: JupyterFrontEndPlugin<void> = {
152168
id: 'jupyterlab-cell-diff:nbdime-plugin',
153169
description: 'Expose a command to show cell diffs using NBDime',
154170
requires: [ICellFooterTracker, INotebookTracker],
171+
optional: [ITranslator],
155172
autoStart: true,
156173
activate: async (
157174
app: JupyterFrontEnd,
158175
cellFooterTracker: ICellFooterTracker,
159-
notebookTracker: INotebookTracker
176+
notebookTracker: INotebookTracker,
177+
translator: ITranslator | null
160178
) => {
161179
const { commands } = app;
180+
const trans = (translator ?? nullTranslator).load(TRANSLATION_NAMESPACE);
162181

163182
commands.addCommand('jupyterlab-cell-diff:show-nbdime', {
164-
label: 'Show Cell Diff (NBDime)',
183+
label: trans.__('Show Cell Diff (NBDime)'),
165184
describedBy: {
166185
args: {
167186
type: 'object',
168187
properties: {
169188
cellId: {
170189
type: 'string',
171-
description: 'ID of the cell to show diff for'
190+
description: trans.__('ID of the cell to show diff for')
172191
},
173192
originalSource: {
174193
type: 'string',
175-
description: 'Original source code to compare against'
194+
description: trans.__('Original source code to compare against')
176195
},
177196
newSource: {
178197
type: 'string',
179-
description: 'New source code to compare with'
198+
description: trans.__('New source code to compare with')
180199
},
181200
showActionButtons: {
182201
type: 'boolean',
183-
description: 'Whether to show action buttons in the diff widget'
202+
description: trans.__(
203+
'Whether to show action buttons in the diff widget'
204+
)
184205
},
185206
notebookPath: {
186207
type: 'string',
187-
description: 'Path to the notebook containing the cell'
208+
description: trans.__('Path to the notebook containing the cell')
188209
},
189210
openDiff: {
190211
type: 'boolean',
191-
description: 'Whether to open the diff widget automatically'
212+
description: trans.__(
213+
'Whether to open the diff widget automatically'
214+
)
192215
}
193216
}
194217
}
@@ -211,7 +234,9 @@ const nbdimePlugin: JupyterFrontEndPlugin<void> = {
211234
const cell = findCell(currentNotebook, cellId);
212235
if (!cell) {
213236
console.error(
214-
'Missing required arguments: cellId (or no active cell found)'
237+
trans.__(
238+
'Missing required arguments: cellId (or no active cell found)'
239+
)
215240
);
216241
return;
217242
}
@@ -228,12 +253,15 @@ const nbdimePlugin: JupyterFrontEndPlugin<void> = {
228253
});
229254
diffData = (response as any).diff;
230255
} catch (error) {
231-
console.warn('Failed to fetch diff data from server:', error);
256+
console.warn(
257+
trans.__('Failed to fetch diff data from server: %1'),
258+
error
259+
);
232260
}
233261

234262
const footer = cellFooterTracker.getFooter(cell.id);
235263
if (!footer) {
236-
console.error(`Footer not found for cell ${cell.id}`);
264+
console.error(trans.__('Footer not found for cell %1', cell.id));
237265
return;
238266
}
239267

@@ -246,12 +274,13 @@ const nbdimePlugin: JupyterFrontEndPlugin<void> = {
246274
diffData: diffData ? { diff: diffData } : undefined,
247275
cellId: cell.id,
248276
showActionButtons,
249-
openDiff
277+
openDiff,
278+
trans
250279
};
251280

252281
await createNBDimeDiffWidget(options);
253282
} catch (error) {
254-
console.error('Failed to create diff widget:', error);
283+
console.error(trans.__('Failed to create diff widget: %1'), error);
255284
}
256285
}
257286
});

src/widget.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ICellModel } from '@jupyterlab/cells';
2+
import { TranslationBundle } from '@jupyterlab/translation';
23
import { checkIcon, ToolbarButton, undoIcon } from '@jupyterlab/ui-components';
34
import { Widget } from '@lumino/widgets';
45
import { ICellFooterTracker } from 'jupyterlab-cell-input-footer';
@@ -27,6 +28,11 @@ export interface IDiffWidgetOptions {
2728
*/
2829
newSource: string;
2930

31+
/**
32+
* The translation bundle
33+
*/
34+
trans: TranslationBundle;
35+
3036
/**
3137
* Additional diff data (for nbdime strategy)
3238
*/
@@ -61,6 +67,7 @@ export abstract class BaseDiffWidget extends Widget {
6167
this._cellFooterTracker = options.cellFooterTracker;
6268
this._originalSource = options.originalSource;
6369
this._newSource = options.newSource || options.cell.sharedModel.getSource();
70+
this._trans = options.trans;
6471
this._showActionButtons = options.showActionButtons ?? true;
6572
this._openDiff = options.openDiff ?? true;
6673
}
@@ -128,7 +135,7 @@ export abstract class BaseDiffWidget extends Widget {
128135
*/
129136
private _createButtons(footer: any): void {
130137
this._toggleButton = new ToolbarButton({
131-
label: 'Compare changes',
138+
label: this._trans.__('Compare changes'),
132139
enabled: true,
133140
className: 'jp-DiffView-toggle',
134141
onClick: () => {
@@ -141,15 +148,15 @@ export abstract class BaseDiffWidget extends Widget {
141148
if (this._showActionButtons) {
142149
const rejectButton = new ToolbarButton({
143150
icon: undoIcon,
144-
tooltip: 'Reject Changes',
151+
tooltip: this._trans.__('Reject Changes'),
145152
enabled: true,
146153
className: 'jp-DiffView-reject',
147154
onClick: () => this.onRejectClick()
148155
});
149156

150157
const acceptButton = new ToolbarButton({
151158
icon: checkIcon,
152-
tooltip: 'Accept Changes',
159+
tooltip: this._trans.__('Accept Changes'),
153160
enabled: true,
154161
className: 'jp-DiffView-accept',
155162
onClick: () => this.onAcceptClick()
@@ -191,4 +198,5 @@ export abstract class BaseDiffWidget extends Widget {
191198
protected _showActionButtons: boolean;
192199
protected _openDiff: boolean;
193200
protected _toggleButton: ToolbarButton | null = null;
201+
private _trans: TranslationBundle;
194202
}

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3714,6 +3714,7 @@ __metadata:
37143714
"@jupyterlab/coreutils": ^6.0.0
37153715
"@jupyterlab/notebook": ^4.0.0
37163716
"@jupyterlab/services": ^7.0.0
3717+
"@jupyterlab/translation": ^4.0.0
37173718
"@jupyterlab/ui-components": ^4.0.0
37183719
"@lumino/widgets": ^2.0.0
37193720
"@types/json-schema": ^7.0.11

0 commit comments

Comments
 (0)