11import { JSONObject } from '@lumino/coreutils' ;
2+ import { ISignal , Signal } from '@lumino/signaling' ;
23
34import * as Blockly from 'blockly' ;
45
56import BlocklyPy from 'blockly/python' ;
7+ import * as En from 'blockly/msg/en' ;
68
79import { IBlocklyManager } from './token' ;
810import { 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