@@ -140,6 +140,8 @@ const BlocklyComponent = React.forwardRef<BlocklyComponentType | null, BlocklyCo
140140 if ( ! workspaceRef . current ) {
141141 return ;
142142 }
143+ // Save workspace state
144+ const workspaceXml = Blockly . Xml . workspaceToDom ( workspaceRef . current ) ;
143145
144146 // Set new locale
145147 switch ( i18n . language ) {
@@ -153,30 +155,33 @@ const BlocklyComponent = React.forwardRef<BlocklyComponentType | null, BlocklyCo
153155 Blockly . setLocale ( En as any ) ;
154156 break ;
155157 }
156-
157158 // Apply custom tokens
158159 Blockly . setLocale ( customTokens ( t ) ) ;
159160
160- // Save workspace state
161- const workspaceXml = Blockly . Xml . workspaceToDom ( workspaceRef . current ) ;
162-
163161 // Clear the workspace
164162 workspaceRef . current . clear ( ) ;
165163
166- // Restore workspace with new locale (blocks will be recreated with new text)
167- if ( workspaceXml . hasChildNodes ( ) ) {
168- Blockly . Xml . domToWorkspace ( workspaceXml , workspaceRef . current ) ;
169- }
170-
171- // Refresh the toolbox
172- const toolbox = workspaceRef . current . getToolbox ( ) ;
173- if ( toolbox ) {
174- // Force toolbox to rebuild with new locale
175- toolbox . refreshSelection ( ) ;
164+ // Force complete toolbox rebuild by calling onWorkspaceRecreated AFTER locale is set
165+ if ( props . onWorkspaceRecreated ) {
166+ props . onWorkspaceRecreated ( workspaceRef . current ) ;
176167 }
177-
178- // Trigger workspace refresh
179- Blockly . svgResize ( workspaceRef . current ) ;
168+
169+ // Small delay to ensure toolbox is rebuilt before restoring blocks
170+ setTimeout ( ( ) => {
171+ if ( workspaceRef . current && workspaceXml . hasChildNodes ( ) ) {
172+ Blockly . Xml . domToWorkspace ( workspaceXml , workspaceRef . current ) ;
173+ }
174+
175+ // Final refresh
176+ const toolbox = workspaceRef . current ! . getToolbox ( ) ;
177+ if ( toolbox && toolbox . refreshSelection ) {
178+ toolbox . refreshSelection ( ) ;
179+ }
180+
181+ if ( workspaceRef . current ) {
182+ Blockly . svgResize ( workspaceRef . current ) ;
183+ }
184+ } , 10 ) ;
180185 } ;
181186
182187 /** Initializes the Blockly workspace. */
0 commit comments