Skip to content

Commit d56ccaf

Browse files
authored
Merge pull request #18 from DenisaCG/internalization
initial code for changing the language
2 parents 8140281 + af7e21e commit d56ccaf

File tree

5 files changed

+215
-5
lines changed

5 files changed

+215
-5
lines changed

src/factory.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
DocumentModel
55
} from '@jupyterlab/docregistry';
66
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
7+
// import { ITranslator } from '@jupyterlab/translation';
78

89
import { BlocklyEditor, BlocklyPanel } from './widget';
910
import { BlocklyManager } from './manager';
@@ -17,6 +18,8 @@ export class BlocklyEditorFactory extends ABCWidgetFactory<
1718
> {
1819
private _manager: BlocklyManager;
1920
private _rendermime: IRenderMimeRegistry;
21+
private _language: string;
22+
// private _translator: ITranslator;
2023

2124
/**
2225
* Constructor of BlocklyEditorFactory.
@@ -27,6 +30,8 @@ export class BlocklyEditorFactory extends ABCWidgetFactory<
2730
super(options);
2831
this._manager = new BlocklyManager();
2932
this._rendermime = options.rendermime;
33+
this._language = this._manager.language;
34+
// this._translator = options.translator;
3035
}
3136

3237
get manager(): BlocklyManager {
@@ -44,7 +49,12 @@ export class BlocklyEditorFactory extends ABCWidgetFactory<
4449
): BlocklyEditor {
4550
return new BlocklyEditor({
4651
context,
47-
content: new BlocklyPanel(context, this._manager, this._rendermime)
52+
content: new BlocklyPanel(
53+
context,
54+
this._manager,
55+
this._rendermime,
56+
this._language
57+
)
4858
});
4959
}
5060
}

src/index.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
88
import { ICommandPalette } from '@jupyterlab/apputils';
99
import { IFileBrowserFactory } from '@jupyterlab/filebrowser';
1010
import { ILauncher } from '@jupyterlab/launcher';
11+
import { ITranslator } from '@jupyterlab/translation';
12+
import { ISettingRegistry } from '@jupyterlab/settingregistry';
1113

1214
import { BlocklyEditorFactory } from './factory';
1315
import { IBlocklyManager } from './token';
@@ -26,20 +28,33 @@ namespace CommandIDs {
2628
export const createNew = 'blockly:create-new-blockly-file';
2729
}
2830

31+
/**
32+
* The id of the translation plugin.
33+
*/
34+
const PLUGIN_ID = '@jupyterlab/translation-extension:plugin';
35+
2936
/**
3037
* Initialization data for the jupyterlab-blocky extension.
3138
*/
3239
const plugin: JupyterFrontEndPlugin<IBlocklyManager> = {
3340
id: 'jupyterlab-blocky:plugin',
3441
autoStart: true,
35-
requires: [ILayoutRestorer, IRenderMimeRegistry, IFileBrowserFactory],
42+
requires: [
43+
ILayoutRestorer,
44+
IRenderMimeRegistry,
45+
IFileBrowserFactory,
46+
ISettingRegistry,
47+
ITranslator
48+
],
3649
optional: [ILauncher, ICommandPalette],
3750
provides: IBlocklyManager,
3851
activate: (
3952
app: JupyterFrontEnd,
4053
restorer: ILayoutRestorer,
4154
rendermime: IRenderMimeRegistry,
4255
browserFactory: IFileBrowserFactory,
56+
settings: ISettingRegistry,
57+
translator: ITranslator,
4358
launcher: ILauncher | null,
4459
palette: ICommandPalette | null
4560
): IBlocklyManager => {
@@ -82,7 +97,10 @@ const plugin: JupyterFrontEndPlugin<IBlocklyManager> = {
8297

8398
// The rendermime instance, necessary to render the outputs
8499
// after a code execution.
85-
rendermime: rendermime
100+
rendermime: rendermime,
101+
102+
// The translator instance, used for the internalization of the plugin.
103+
translator: translator
86104
});
87105

88106
// Add the widget to the tracker when it's created
@@ -99,6 +117,32 @@ const plugin: JupyterFrontEndPlugin<IBlocklyManager> = {
99117
// Registering the widget factory
100118
app.docRegistry.addWidgetFactory(widgetFactory);
101119

120+
function getSetting(setting: ISettingRegistry.ISettings): string {
121+
// Read the settings and convert to the correct type
122+
const currentLocale: string = setting.get('locale').composite as string;
123+
return currentLocale;
124+
}
125+
126+
// Wait for the application to be restored and
127+
// for the settings for this plugin to be loaded
128+
settings.load(PLUGIN_ID).then(setting => {
129+
// Read the settings
130+
const currentLocale = getSetting(setting);
131+
132+
// Listen for our plugin setting changes using Signal
133+
setting.changed.connect(getSetting);
134+
135+
// Get new language and call the function that modifies the language name accordingly.
136+
// Also, make the transformation to have the name of the language package as in Blockly.
137+
const language =
138+
currentLocale[currentLocale.length - 2].toUpperCase() +
139+
currentLocale[currentLocale.length - 1].toLowerCase();
140+
console.log(`Current Language : '${language}'`);
141+
142+
// Transmitting the current language to the manager.
143+
widgetFactory.manager.setlanguage(language);
144+
});
145+
102146
commands.addCommand(command, {
103147
label: args =>
104148
args['isPalette'] ? 'New Blockly Editor' : 'Blockly Editor',

src/layout.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { SimplifiedOutputArea, OutputAreaModel } from '@jupyterlab/outputarea';
22
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
33
import { ISessionContext } from '@jupyterlab/apputils';
4+
// import { ITranslator } from '@jupyterlab/translation';
45

56
import { Message } from '@lumino/messaging';
67
import { PartialJSONValue } from '@lumino/coreutils';
78
import { PanelLayout, Widget } from '@lumino/widgets';
89
import { IIterator, ArrayIterator } from '@lumino/algorithm';
10+
import { Signal } from '@lumino/signaling';
911

1012
import * as Blockly from 'blockly';
1113

@@ -20,6 +22,7 @@ export class BlocklyLayout extends PanelLayout {
2022
private _manager: BlocklyManager;
2123
private _workspace: Blockly.WorkspaceSvg;
2224
private _sessionContext: ISessionContext;
25+
// private _translator: ITranslator;
2326
private _outputArea: SimplifiedOutputArea;
2427

2528
/**
@@ -30,10 +33,12 @@ export class BlocklyLayout extends PanelLayout {
3033
manager: BlocklyManager,
3134
sessionContext: ISessionContext,
3235
rendermime: IRenderMimeRegistry
36+
// translator: ITranslator
3337
) {
3438
super();
3539
this._manager = manager;
3640
this._sessionContext = sessionContext;
41+
// this._translator = translator;
3742

3843
// Creating the container for the Blockly editor
3944
// and the output area to render the execution replies.
@@ -64,6 +69,8 @@ export class BlocklyLayout extends PanelLayout {
6469
* Dispose of the resources held by the widget.
6570
*/
6671
dispose(): void {
72+
this._manager.changed.disconnect(this._resizeWorkspace, this);
73+
Signal.clearData(this);
6774
this._workspace.dispose();
6875
super.dispose();
6976
}
@@ -137,6 +144,13 @@ export class BlocklyLayout extends PanelLayout {
137144
toolbox: this._manager.toolbox,
138145
theme: THEME
139146
});
147+
148+
// let categories: string;
149+
150+
// Loading the ITranslator
151+
// const trans = this._translator.load('jupyterlab-blockly');
152+
153+
// categories = trans.__('Category');
140154
}
141155

142156
private _resizeWorkspace(): void {

src/manager.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { JSONObject } from '@lumino/coreutils';
2+
import { ISignal, Signal } from '@lumino/signaling';
23

34
import * as Blockly from 'blockly';
45

56
import BlocklyPy from 'blockly/python';
7+
import * as En from 'blockly/msg/en';
68

79
import { IBlocklyManager } from './token';
810
import { TOOLBOX } from './utils';
@@ -11,6 +13,8 @@ export class BlocklyManager implements IBlocklyManager {
1113
private _toolbox: JSONObject;
1214
private _activeGenerator: Blockly.Generator;
1315
private _generators: Map<string, Blockly.Generator>;
16+
private _language: string;
17+
private _changed: Signal<BlocklyManager, void>;
1418

1519
/**
1620
* Constructor of BlocklyEditorFactory.
@@ -21,6 +25,8 @@ export class BlocklyManager implements IBlocklyManager {
2125
this._toolbox = TOOLBOX;
2226
this._activeGenerator = BlocklyPy;
2327
this._generators = new Map<string, Blockly.Generator>();
28+
this._language = 'En'; // By default we choose English.
29+
this._changed = new Signal<BlocklyManager, void>(this);
2430
}
2531

2632
get toolbox(): JSONObject {
@@ -35,6 +41,18 @@ export class BlocklyManager implements IBlocklyManager {
3541
return this._activeGenerator;
3642
}
3743

44+
get changed(): ISignal<BlocklyManager, void> {
45+
return this._changed;
46+
}
47+
48+
set language(language: string) {
49+
this._language = language;
50+
}
51+
52+
get language(): string {
53+
return this._language;
54+
}
55+
3856
registerToolbox(value: JSONObject): void {
3957
this._toolbox = value;
4058
}
@@ -46,4 +64,121 @@ export class BlocklyManager implements IBlocklyManager {
4664
registerGenerator(kernel: string, generator: Blockly.Generator): void {
4765
this._generators.set(kernel, generator);
4866
}
67+
68+
setlanguage(language: string): void {
69+
this.language = language;
70+
Private.importLanguageModule(language);
71+
}
72+
}
73+
74+
// Dynamically importing the language modules needed for each respective
75+
// user, in order to change the Blockly language in accordance to the
76+
// JL one.
77+
namespace Private {
78+
export async function importLanguageModule(language: string) {
79+
let module: Promise<any>;
80+
switch (language) {
81+
case 'En':
82+
module = import('blockly/msg/en');
83+
break;
84+
case 'Es':
85+
module = import('blockly/msg/es');
86+
break;
87+
case 'Fr':
88+
module = import('blockly/msg/fr');
89+
break;
90+
case 'Sa' || 'Ar':
91+
module = import('blockly/msg/ar');
92+
break;
93+
case 'Cz':
94+
module = import('blockly/msg/cs');
95+
break;
96+
case 'Dk':
97+
module = import('blockly/msg/da');
98+
break;
99+
case 'De':
100+
module = import('blockly/msg/de');
101+
break;
102+
case 'Gr':
103+
module = import('blockly/msg/el');
104+
break;
105+
case 'Ee':
106+
module = import('blockly/msg/et');
107+
break;
108+
case 'Fi':
109+
module = import('blockly/msg/fi');
110+
break;
111+
case 'Il':
112+
module = import('blockly/msg/he');
113+
break;
114+
case 'Hu':
115+
module = import('blockly/msg/hu');
116+
break;
117+
case 'Am':
118+
module = import('blockly/msg/hy');
119+
break;
120+
case 'Id':
121+
module = import('blockly/msg/id');
122+
break;
123+
case 'It':
124+
module = import('blockly/msg/it');
125+
break;
126+
case 'Jp':
127+
module = import('blockly/msg/ja');
128+
break;
129+
case 'Kr':
130+
module = import('blockly/msg/ko');
131+
break;
132+
case 'Lt':
133+
module = import('blockly/msg/lt');
134+
break;
135+
case 'Nl':
136+
module = import('blockly/msg/nl');
137+
break;
138+
case 'Pl':
139+
module = import('blockly/msg/pl');
140+
break;
141+
case 'Br':
142+
module = import('blockly/msg/pt');
143+
break;
144+
case 'Ro':
145+
module = import('blockly/msg/ro');
146+
break;
147+
case 'Ru':
148+
module = import('blockly/msg/ru');
149+
break;
150+
case 'Lk':
151+
module = import('blockly/msg/si');
152+
break;
153+
case 'Tr':
154+
module = import('blockly/msg/tr');
155+
break;
156+
case 'Ua':
157+
module = import('blockly/msg/uk');
158+
break;
159+
case 'Vn':
160+
module = import('blockly/msg/vi');
161+
break;
162+
case 'Tw':
163+
module = import('blockly/msg/zh-hant');
164+
break;
165+
case 'Cn':
166+
module = import('blockly/msg/zh-hans');
167+
break;
168+
// Complete with all the cases taken from: (last updates June 2022)
169+
// List of languages in blockly: https://github.com/google/blockly/tree/master/msg/js
170+
// List of languages in Lab: https://github.com/jupyterlab/language-packs/tree/master/language-packs
171+
default:
172+
console.warn('Language not found. Loading english');
173+
module = Promise.resolve(En);
174+
break;
175+
}
176+
177+
// Setting the current language in Blockly.
178+
module.then(lang => {
179+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
180+
// @ts-ignore
181+
Blockly.setLocale(lang);
182+
});
183+
}
49184
}

0 commit comments

Comments
 (0)