A monorepo containing jupyter-blocks and jupyter-tidyblocks. Blockly extension for JupyterLab.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #18 from DenisaCG/internalization

initial code for changing the language

authored by

Carlos Herrero and committed by
GitHub
d56ccaff 8140281b

+215 -5
+11 -1
src/factory.ts
··· 4 4 DocumentModel 5 5 } from '@jupyterlab/docregistry'; 6 6 import { IRenderMimeRegistry } from '@jupyterlab/rendermime'; 7 + // import { ITranslator } from '@jupyterlab/translation'; 7 8 8 9 import { BlocklyEditor, BlocklyPanel } from './widget'; 9 10 import { BlocklyManager } from './manager'; ··· 17 18 > { 18 19 private _manager: BlocklyManager; 19 20 private _rendermime: IRenderMimeRegistry; 21 + private _language: string; 22 + // private _translator: ITranslator; 20 23 21 24 /** 22 25 * Constructor of BlocklyEditorFactory. ··· 27 30 super(options); 28 31 this._manager = new BlocklyManager(); 29 32 this._rendermime = options.rendermime; 33 + this._language = this._manager.language; 34 + // this._translator = options.translator; 30 35 } 31 36 32 37 get manager(): BlocklyManager { ··· 44 49 ): BlocklyEditor { 45 50 return new BlocklyEditor({ 46 51 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 + ) 48 58 }); 49 59 } 50 60 }
+46 -2
src/index.ts
··· 8 8 import { ICommandPalette } from '@jupyterlab/apputils'; 9 9 import { IFileBrowserFactory } from '@jupyterlab/filebrowser'; 10 10 import { ILauncher } from '@jupyterlab/launcher'; 11 + import { ITranslator } from '@jupyterlab/translation'; 12 + import { ISettingRegistry } from '@jupyterlab/settingregistry'; 11 13 12 14 import { BlocklyEditorFactory } from './factory'; 13 15 import { IBlocklyManager } from './token'; ··· 27 29 } 28 30 29 31 /** 32 + * The id of the translation plugin. 33 + */ 34 + const PLUGIN_ID = '@jupyterlab/translation-extension:plugin'; 35 + 36 + /** 30 37 * Initialization data for the jupyterlab-blocky extension. 31 38 */ 32 39 const plugin: JupyterFrontEndPlugin<IBlocklyManager> = { 33 40 id: 'jupyterlab-blocky:plugin', 34 41 autoStart: true, 35 - requires: [ILayoutRestorer, IRenderMimeRegistry, IFileBrowserFactory], 42 + requires: [ 43 + ILayoutRestorer, 44 + IRenderMimeRegistry, 45 + IFileBrowserFactory, 46 + ISettingRegistry, 47 + ITranslator 48 + ], 36 49 optional: [ILauncher, ICommandPalette], 37 50 provides: IBlocklyManager, 38 51 activate: ( ··· 40 53 restorer: ILayoutRestorer, 41 54 rendermime: IRenderMimeRegistry, 42 55 browserFactory: IFileBrowserFactory, 56 + settings: ISettingRegistry, 57 + translator: ITranslator, 43 58 launcher: ILauncher | null, 44 59 palette: ICommandPalette | null 45 60 ): IBlocklyManager => { ··· 82 97 83 98 // The rendermime instance, necessary to render the outputs 84 99 // 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 86 104 }); 87 105 88 106 // Add the widget to the tracker when it's created ··· 98 116 }); 99 117 // Registering the widget factory 100 118 app.docRegistry.addWidgetFactory(widgetFactory); 119 + 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 + }); 101 145 102 146 commands.addCommand(command, { 103 147 label: args =>
+14
src/layout.ts
··· 1 1 import { SimplifiedOutputArea, OutputAreaModel } from '@jupyterlab/outputarea'; 2 2 import { IRenderMimeRegistry } from '@jupyterlab/rendermime'; 3 3 import { ISessionContext } from '@jupyterlab/apputils'; 4 + // import { ITranslator } from '@jupyterlab/translation'; 4 5 5 6 import { Message } from '@lumino/messaging'; 6 7 import { PartialJSONValue } from '@lumino/coreutils'; 7 8 import { PanelLayout, Widget } from '@lumino/widgets'; 8 9 import { IIterator, ArrayIterator } from '@lumino/algorithm'; 10 + import { Signal } from '@lumino/signaling'; 9 11 10 12 import * as Blockly from 'blockly'; 11 13 ··· 20 22 private _manager: BlocklyManager; 21 23 private _workspace: Blockly.WorkspaceSvg; 22 24 private _sessionContext: ISessionContext; 25 + // private _translator: ITranslator; 23 26 private _outputArea: SimplifiedOutputArea; 24 27 25 28 /** ··· 30 33 manager: BlocklyManager, 31 34 sessionContext: ISessionContext, 32 35 rendermime: IRenderMimeRegistry 36 + // translator: ITranslator 33 37 ) { 34 38 super(); 35 39 this._manager = manager; 36 40 this._sessionContext = sessionContext; 41 + // this._translator = translator; 37 42 38 43 // Creating the container for the Blockly editor 39 44 // and the output area to render the execution replies. ··· 64 69 * Dispose of the resources held by the widget. 65 70 */ 66 71 dispose(): void { 72 + this._manager.changed.disconnect(this._resizeWorkspace, this); 73 + Signal.clearData(this); 67 74 this._workspace.dispose(); 68 75 super.dispose(); 69 76 } ··· 137 144 toolbox: this._manager.toolbox, 138 145 theme: THEME 139 146 }); 147 + 148 + // let categories: string; 149 + 150 + // Loading the ITranslator 151 + // const trans = this._translator.load('jupyterlab-blockly'); 152 + 153 + // categories = trans.__('Category'); 140 154 } 141 155 142 156 private _resizeWorkspace(): void {
+135
src/manager.ts
··· 1 1 import { JSONObject } from '@lumino/coreutils'; 2 + import { ISignal, Signal } from '@lumino/signaling'; 2 3 3 4 import * as Blockly from 'blockly'; 4 5 5 6 import BlocklyPy from 'blockly/python'; 7 + import * as En from 'blockly/msg/en'; 6 8 7 9 import { IBlocklyManager } from './token'; 8 10 import { TOOLBOX } from './utils'; ··· 11 13 private _toolbox: JSONObject; 12 14 private _activeGenerator: Blockly.Generator; 13 15 private _generators: Map<string, Blockly.Generator>; 16 + private _language: string; 17 + private _changed: Signal<BlocklyManager, void>; 14 18 15 19 /** 16 20 * Constructor of BlocklyEditorFactory. ··· 21 25 this._toolbox = TOOLBOX; 22 26 this._activeGenerator = BlocklyPy; 23 27 this._generators = new Map<string, Blockly.Generator>(); 28 + this._language = 'En'; // By default we choose English. 29 + this._changed = new Signal<BlocklyManager, void>(this); 24 30 } 25 31 26 32 get toolbox(): JSONObject { ··· 35 41 return this._activeGenerator; 36 42 } 37 43 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 + 38 56 registerToolbox(value: JSONObject): void { 39 57 this._toolbox = value; 40 58 } ··· 45 63 46 64 registerGenerator(kernel: string, generator: Blockly.Generator): void { 47 65 this._generators.set(kernel, generator); 66 + } 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 + }); 48 183 } 49 184 }
+9 -2
src/widget.ts
··· 6 6 import { ToolbarButton } from '@jupyterlab/apputils'; 7 7 import { IRenderMimeRegistry } from '@jupyterlab/rendermime'; 8 8 import { runIcon } from '@jupyterlab/ui-components'; 9 + // import { ITranslator } from '@jupyterlab/translation'; 9 10 10 11 import { Panel } from '@lumino/widgets'; 11 12 import { Signal } from '@lumino/signaling'; ··· 20 21 constructor(options: DocumentWidget.IOptions<BlocklyPanel, DocumentModel>) { 21 22 super(options); 22 23 24 + // Loading the ITranslator 25 + // const trans = this.translator.load('jupyterlab'); 26 + 23 27 // Create and add a button to the toolbar to execute 24 28 // the code. 25 29 const runCode = () => { 26 30 (this.content.layout as BlocklyLayout).run(); 27 31 }; 28 32 const button = new ToolbarButton({ 29 - label: 'Run Code', 33 + label: '', 30 34 icon: runIcon, 31 35 className: 'jp-blockly-button', 32 36 onClick: runCode, ··· 34 38 }); 35 39 button.addClass('jp-blockly-runButton'); 36 40 this.toolbar.addItem('run', button); 41 + // button.title.label = trans.__('Run Code'); 37 42 } 38 43 39 44 /** ··· 59 64 constructor( 60 65 context: DocumentRegistry.IContext<DocumentModel>, 61 66 manager: BlocklyManager, 62 - rendermime: IRenderMimeRegistry 67 + rendermime: IRenderMimeRegistry, 68 + language: string 69 + // translator: ITranslator 63 70 ) { 64 71 super({ 65 72 layout: new BlocklyLayout(manager, context.sessionContext, rendermime)