Skip to content

Commit 18b99a3

Browse files
committed
added dynamic imports for language modules
1 parent 6464960 commit 18b99a3

File tree

4 files changed

+139
-40
lines changed

4 files changed

+139
-40
lines changed

src/index.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ const plugin: JupyterFrontEndPlugin<IBlocklyManager> = {
110110
// Registering the widget factory
111111
app.docRegistry.addWidgetFactory(widgetFactory);
112112

113+
function getSetting(setting: ISettingRegistry.ISettings): string {
114+
// Read the settings and convert to the correct type
115+
let currentLocale: string = setting.get('locale').composite as string;
116+
return currentLocale;
117+
}
118+
119+
// Wait for the application to be restored and
120+
// for the settings for this plugin to be loaded
121+
settings
122+
.load(PLUGIN_ID)
123+
.then(setting => {
124+
// Read the settings
125+
let currentLocale = getSetting(setting);
126+
127+
// Listen for our plugin setting changes using Signal
128+
setting.changed.connect(getSetting);
129+
130+
// Get new language and call the function that modifies the language name accordingly.
131+
// Also, make the transformation to have the name of the language package as in Blockly.
132+
let language = currentLocale[currentLocale.length - 2].toUpperCase() + currentLocale[currentLocale.length - 1].toLowerCase();
133+
console.log(`Current Language : '${language}'`);
134+
135+
// Transmitting the current language to the manager.
136+
widgetFactory.manager.setlanguage(language);
137+
});
138+
113139
commands.addCommand(command, {
114140
label: args =>
115141
args['isPalette'] ? 'New Blockly Editor' : 'Blockly Editor',
@@ -153,31 +179,6 @@ const plugin: JupyterFrontEndPlugin<IBlocklyManager> = {
153179
category: PALETTE_CATEGORY
154180
});
155181
}
156-
function getSetting(setting: ISettingRegistry.ISettings): string {
157-
// Read the settings and convert to the correct type
158-
let currentLocale: string = setting.get('locale').composite as string;
159-
return currentLocale;
160-
}
161-
162-
// Wait for the application to be restored and
163-
// for the settings for this plugin to be loaded
164-
settings
165-
.load(PLUGIN_ID)
166-
.then(setting => {
167-
// Read the settings
168-
let currentLocale = getSetting(setting);
169-
170-
// Listen for our plugin setting changes using Signal
171-
setting.changed.connect(getSetting);
172-
173-
// Get new language and call the function that modifies the language name accordingly.
174-
// Also, make the transformation to have the name of the language package as in Blockly.
175-
let language = currentLocale[currentLocale.length - 2].toUpperCase() + currentLocale[currentLocale.length - 1].toLowerCase();
176-
console.log(`Current Language : '${language}'`);
177-
178-
// Transmitting the current language to the manager.
179-
widgetFactory.manager.setlanguage(language);
180-
});
181182

182183
return widgetFactory.manager;
183184
}

src/layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class BlocklyLayout extends PanelLayout {
148148
// let categories: string;
149149

150150
// Loading the ITranslator
151-
// const trans = this._translator.load('jupyterlab');
151+
// const trans = this._translator.load('jupyterlab-blockly');
152152

153153
// categories = trans.__('Category');
154154
}

src/manager.ts

Lines changed: 111 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as Blockly from 'blockly';
55

66
import BlocklyPy from 'blockly/python';
77
import * as En from 'blockly/msg/en';
8-
import * as Fr from 'blockly/msg/fr';
98

109
import { IBlocklyManager } from './token';
1110
import { TOOLBOX } from './utils';
@@ -26,7 +25,7 @@ export class BlocklyManager implements IBlocklyManager {
2625
this._toolbox = TOOLBOX;
2726
this._activeGenerator = BlocklyPy;
2827
this._generators = new Map<string, Blockly.Generator>();
29-
//this._language = 'En'; // By default we choose English.
28+
this._language = 'En'; // By default we choose English.
3029

3130
this._changed = new Signal<BlocklyManager, void>(this);
3231
}
@@ -47,11 +46,11 @@ export class BlocklyManager implements IBlocklyManager {
4746
return this._changed;
4847
}
4948

50-
set language(language: string){
49+
set language(language: string) {
5150
this._language = language;
5251
}
5352

54-
get language(): string{
53+
get language(): string {
5554
return this._language;
5655
}
5756

@@ -70,16 +69,115 @@ export class BlocklyManager implements IBlocklyManager {
7069
setlanguage(language: string): void {
7170
this.language = language;
7271

73-
// Set Blockly Language to English.
74-
if (language == 'En') {
75-
// @ts-ignore
76-
Blockly.setLocale(En);
77-
console.log('Setting Blockly language to English.');
72+
this.language = language;
73+
Private.importLanguageModule(language);
74+
}
75+
}
76+
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;
78175
}
79-
else if (language == 'Fr') {
176+
177+
module.then(lang => {
80178
// @ts-ignore
81-
Blockly.setLocale(Fr);
82-
console.log('Setting Blockly language to French.');
83-
}
179+
Blockly.setLocale(lang);
180+
});
84181
}
85182
}
183+

src/widget.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class BlocklyEditor extends DocumentWidget<BlocklyPanel, DocumentModel> {
3030
(this.content.layout as BlocklyLayout).run();
3131
};
3232
const button = new ToolbarButton({
33-
label: 'Run Code',
33+
label: '',
3434
icon: runIcon,
3535
className: 'jp-blockly-button',
3636
onClick: runCode,

0 commit comments

Comments
 (0)