Skip to content

Commit 5cf924b

Browse files
authored
Merge pull request #116 from kgoo124/localization-signals
Localization for signals example
2 parents 4cb5863 + 2de3b43 commit 5cf924b

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

basics/signals/README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ JupyterLab's Lumino engine uses the `ISignal` interface and the
2121

2222
The basic concept is as follows:
2323

24-
First, a widget (`button.ts`), in this case the one that contains
24+
First, a widget (`button.tsx`), in this case the one that contains
2525
some visual elements such as a button, defines a `_stateChanged` signal:
2626

2727
```ts
@@ -44,7 +44,7 @@ Another widget, in this case the panel (`panel.ts`) that boxes several different
4444
subscribes to the `stateChanged` signal and links some function to it:
4545

4646
```ts
47-
// src/panel.ts#L22-L22
47+
// src/panel.ts#L29-L29
4848

4949
this._widget.stateChanged.connect(this._logMessage, this);
5050
```
@@ -141,13 +141,14 @@ The `panel.ts` class defines an extension panel that displays the
141141
This is done in the constructor.
142142

143143
```ts
144-
// src/panel.ts#L13-L23
144+
// src/panel.ts#L19-L30
145145

146-
constructor() {
147146
super();
147+
this._translator = translator || nullTranslator;
148+
this._trans = this._translator.load('jupyterlab');
148149
this.addClass(PANEL_CLASS);
149150
this.id = 'SignalExamplePanel';
150-
this.title.label = 'Signal Example View';
151+
this.title.label = this._trans.__('Signal Example View');
151152
this.title.closable = true;
152153

153154
this._widget = new ButtonWidget();
@@ -160,7 +161,7 @@ Subscription to a signal is done using the `connect` method of the
160161
`stateChanged` attribute.
161162

162163
```ts
163-
// src/panel.ts#L22-L22
164+
// src/panel.ts#L29-L29
164165

165166
this._widget.stateChanged.connect(this._logMessage, this);
166167
```
@@ -178,7 +179,7 @@ The `_logMessage` function receives as parameters the emitter (of type `ButtonWi
178179
and the count (of type `ICount`) sent by the signal emitter.
179180

180181
```ts
181-
// src/panel.ts#L25-L25
182+
// src/panel.ts#L32-L32
182183

183184
private _logMessage(emitter: ButtonWidget, count: ICount): void {
184185
```
@@ -187,7 +188,7 @@ In our case, that function writes `Button has been clicked ... times.` text
187188
to the browser console and in an alert when the big red button is clicked.
188189
189190
```ts
190-
// src/panel.ts#L25-L29
191+
// src/panel.ts#L32-L36
191192

192193
private _logMessage(emitter: ButtonWidget, count: ICount): void {
193194
console.log('Hey, a Signal has been received from', emitter);

basics/signals/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@
4242
"watch:src": "tsc -w"
4343
},
4444
"dependencies": {
45-
"@jupyterlab/application": "~3.0.0-beta.4",
46-
"@jupyterlab/launcher": "~3.0.0-beta.4",
47-
"@jupyterlab/mainmenu": "~3.0.0-beta.4",
45+
"@jupyterlab/application": "~3.0.0-beta.8",
46+
"@jupyterlab/launcher": "~3.0.0-beta.8",
47+
"@jupyterlab/mainmenu": "~3.0.0-beta.8",
48+
"@jupyterlab/translation": "^3.0.0-beta.8",
4849
"@lumino/algorithm": "^1.3.3",
4950
"@lumino/coreutils": "^1.5.3",
5051
"@lumino/datagrid": "^0.3.1",
@@ -71,4 +72,4 @@
7172
"extension": true,
7273
"outputDir": "jupyterlab_examples_signals/static"
7374
}
74-
}
75+
}

basics/signals/src/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { ILauncher } from '@jupyterlab/launcher';
99

1010
import { IMainMenu } from '@jupyterlab/mainmenu';
1111

12+
import { ITranslator } from '@jupyterlab/translation';
13+
1214
import { Menu } from '@lumino/widgets';
1315

1416
import { SignalExamplePanel } from './panel';
@@ -27,8 +29,8 @@ const extension: JupyterFrontEndPlugin<void> = {
2729
id: 'signals',
2830
autoStart: true,
2931
optional: [ILauncher],
30-
requires: [ICommandPalette, IMainMenu],
31-
activate: activate
32+
requires: [ICommandPalette, IMainMenu, ITranslator],
33+
activate
3234
};
3335

3436
/**
@@ -37,17 +39,20 @@ const extension: JupyterFrontEndPlugin<void> = {
3739
* @param app Jupyter Font End
3840
* @param palette Jupyter Commands Palette
3941
* @param mainMenu Jupyter Menu
42+
* @param translator Jupyter Translator
4043
* @param launcher [optional] Jupyter Launcher
4144
*/
4245
function activate(
4346
app: JupyterFrontEnd,
4447
palette: ICommandPalette,
4548
mainMenu: IMainMenu,
49+
translator: ITranslator,
4650
launcher: ILauncher | null
4751
): void {
4852
const manager = app.serviceManager;
4953
const { commands, shell } = app;
5054
const category = 'Extension Examples';
55+
const trans = translator.load('jupyterlab');
5156

5257
// Add launcher
5358
if (launcher) {
@@ -65,21 +70,21 @@ function activate(
6570
function createPanel(): Promise<SignalExamplePanel> {
6671
let panel: SignalExamplePanel;
6772
return manager.ready.then(() => {
68-
panel = new SignalExamplePanel();
73+
panel = new SignalExamplePanel(translator);
6974
shell.add(panel, 'main');
7075
return panel;
7176
});
7277
}
7378

7479
// Add menu tab
7580
const signalMenu = new Menu({ commands });
76-
signalMenu.title.label = 'Signal Example';
81+
signalMenu.title.label = trans.__('Signal Example');
7782
mainMenu.addMenu(signalMenu);
7883

7984
// Add commands to registry
8085
commands.addCommand(CommandIDs.create, {
81-
label: 'Open the Signal Example Panel',
82-
caption: 'Open the Signal Example Panel',
86+
label: trans.__('Open the Signal Example Panel'),
87+
caption: trans.__('Open the Signal Example Panel'),
8388
execute: createPanel
8489
});
8590

basics/signals/src/panel.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import {
2+
ITranslator,
3+
nullTranslator,
4+
TranslationBundle
5+
} from '@jupyterlab/translation';
16
import { StackedPanel } from '@lumino/widgets';
27

38
import { ButtonWidget, ICount } from './button';
@@ -10,11 +15,13 @@ const PANEL_CLASS = 'jp-tutorial-view';
1015
* A panel which contains a console and the ability to add other children.
1116
*/
1217
export class SignalExamplePanel extends StackedPanel {
13-
constructor() {
18+
constructor(translator?: ITranslator) {
1419
super();
20+
this._translator = translator || nullTranslator;
21+
this._trans = this._translator.load('jupyterlab');
1522
this.addClass(PANEL_CLASS);
1623
this.id = 'SignalExamplePanel';
17-
this.title.label = 'Signal Example View';
24+
this.title.label = this._trans.__('Signal Example View');
1825
this.title.closable = true;
1926

2027
this._widget = new ButtonWidget();
@@ -33,4 +40,7 @@ export class SignalExamplePanel extends StackedPanel {
3340
}
3441

3542
private _widget: ButtonWidget;
43+
44+
private _translator: ITranslator;
45+
private _trans: TranslationBundle;
3646
}

0 commit comments

Comments
 (0)