Skip to content

Commit b0b3189

Browse files
authored
Merge pull request #112 from kgoo124/localization-datagrid
Localization for datagrid example
2 parents ab5aefa + ca282f2 commit b0b3189

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

basics/datagrid/README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ First you need to import `StackedPanel`, `DataGrid`
1414
and `DataModel` classes from lumino:
1515

1616
```ts
17-
// src/index.ts#L10-L12
17+
// src/index.ts#L16-L18
1818

1919
import { DataGrid, DataModel } from '@lumino/datagrid';
2020

@@ -35,14 +35,17 @@ the main area of JupyterLab as seen in the above screenshot.
3535
With these three classes, you can create your own widget, called `DataGridPanel` :
3636

3737
```ts
38-
// src/index.ts#L49-L63
38+
// src/index.ts#L57-L77
3939

4040
class DataGridPanel extends StackedPanel {
41-
constructor() {
41+
constructor(translator?: ITranslator) {
4242
super();
43+
this._translator = translator || nullTranslator;
44+
this._trans = this._translator.load('jupyterlab');
45+
4346
this.addClass('jp-example-view');
4447
this.id = 'datagrid-example';
45-
this.title.label = 'Datagrid Example View';
48+
this.title.label = this._trans.__('Datagrid Example View');
4649
this.title.closable = true;
4750

4851
const model = new LargeDataModel();
@@ -51,19 +54,24 @@ class DataGridPanel extends StackedPanel {
5154

5255
this.addWidget(grid);
5356
}
57+
58+
private _translator: ITranslator;
59+
private _trans: TranslationBundle;
5460
}
5561
```
5662

5763
Your widget is derived from `StackedPanel` to inherit its behavior. Then
5864
some properties for the panel. Then the `DataGrid` widget and its associated model are created.
5965
Finally the grid is inserted inside the panel.
6066

67+
Note that the private variables `_translator` and `_trans` are used for translating labels and other pieces of text that are displayed to the user.
68+
6169
The `DataModel` class is not used directly as it is an abstract class.
6270
Therefore in this example a class `LargeDataModel` is derived from it
6371
to implement its abstract methods:
6472

6573
```ts
66-
// src/index.ts#L65-L74
74+
// src/index.ts#L79-L88
6775

6876
class LargeDataModel extends DataModel {
6977
rowCount(region: DataModel.RowRegion): number {
@@ -108,7 +116,7 @@ values of the datagrid. In this case it simply displays the row and
108116
column index in each cell, and adds a letter prefix in the header regions:
109117
110118
```ts
111-
// src/index.ts#L74-L85
119+
// src/index.ts#L88-L99
112120

113121
data(region: DataModel.CellRegion, row: number, column: number): any {
114122
if (region === 'row-header') {

basics/datagrid/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"dependencies": {
4545
"@jupyterlab/application": "~3.0.0-beta.4",
4646
"@jupyterlab/mainmenu": "~3.0.0-beta.4",
47+
"@jupyterlab/translation": "^3.0.0-beta.4",
4748
"@lumino/algorithm": "^1.3.3",
4849
"@lumino/coreutils": "^1.5.3",
4950
"@lumino/datagrid": "^0.5.2",

basics/datagrid/src/index.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import { ICommandPalette } from '@jupyterlab/apputils';
77

88
import { IMainMenu } from '@jupyterlab/mainmenu';
99

10+
import {
11+
ITranslator,
12+
nullTranslator,
13+
TranslationBundle
14+
} from '@jupyterlab/translation';
15+
1016
import { DataGrid, DataModel } from '@lumino/datagrid';
1117

1218
import { Menu, StackedPanel } from '@lumino/widgets';
@@ -17,18 +23,20 @@ import { Menu, StackedPanel } from '@lumino/widgets';
1723
const extension: JupyterFrontEndPlugin<void> = {
1824
id: 'datagrid',
1925
autoStart: true,
20-
requires: [ICommandPalette, IMainMenu],
26+
requires: [ICommandPalette, IMainMenu, ITranslator],
2127
activate: (
2228
app: JupyterFrontEnd,
2329
palette: ICommandPalette,
24-
mainMenu: IMainMenu
30+
mainMenu: IMainMenu,
31+
translator: ITranslator
2532
) => {
2633
const { commands, shell } = app;
34+
const trans = translator.load('jupyterlab');
2735

2836
const command = 'examples:datagrid';
2937
commands.addCommand(command, {
30-
label: 'Open a Datagrid',
31-
caption: 'Open a Datagrid Panel',
38+
label: trans.__('Open a Datagrid'),
39+
caption: trans.__('Open a Datagrid Panel'),
3240
execute: () => {
3341
const widget = new DataGridPanel();
3442
shell.add(widget, 'main');
@@ -38,7 +46,7 @@ const extension: JupyterFrontEndPlugin<void> = {
3846

3947
const exampleMenu = new Menu({ commands });
4048

41-
exampleMenu.title.label = 'DataGrid Example';
49+
exampleMenu.title.label = trans.__('DataGrid Example');
4250
mainMenu.addMenu(exampleMenu, { rank: 80 });
4351
exampleMenu.addItem({ command });
4452
}
@@ -47,11 +55,14 @@ const extension: JupyterFrontEndPlugin<void> = {
4755
export default extension;
4856

4957
class DataGridPanel extends StackedPanel {
50-
constructor() {
58+
constructor(translator?: ITranslator) {
5159
super();
60+
this._translator = translator || nullTranslator;
61+
this._trans = this._translator.load('jupyterlab');
62+
5263
this.addClass('jp-example-view');
5364
this.id = 'datagrid-example';
54-
this.title.label = 'Datagrid Example View';
65+
this.title.label = this._trans.__('Datagrid Example View');
5566
this.title.closable = true;
5667

5768
const model = new LargeDataModel();
@@ -60,6 +71,9 @@ class DataGridPanel extends StackedPanel {
6071

6172
this.addWidget(grid);
6273
}
74+
75+
private _translator: ITranslator;
76+
private _trans: TranslationBundle;
6377
}
6478

6579
class LargeDataModel extends DataModel {

0 commit comments

Comments
 (0)