Help use setLocale in code demo

101 views
Skip to first unread message

Seb Seb

unread,
Jul 24, 2023, 9:41:49 AM7/24/23
to Blockly
Hi,
I'm trying to create a multilingual blockly, like in code demo. But this demo is not up to date and use old code, not setLocale function.
But I don't success using a dropdown menu in setLocale function. I don't want to do something like
"if (languageMenu.options[languageMenu.selectedIndex].value == 'fr') Blockly.setLocale(Fr)
else if (languageMenu.options[languageMenu.selectedIndex].value == 'en') Blockly.setLocale(En)
else if..."
Thanks.

What I tried (but there's a type problem with 'string' and '{ [key: string]: string; }' types):
const µcB_changeLanguage = (): void => {
  const languageMenu: HTMLSelectElement = document.getElementById('languageMenu') as HTMLSelectElement;
  const newLang: any = encodeURIComponent(languageMenu.options[languageMenu.selectedIndex].value);
  const data = Blockly.serialization.workspaces.save(workspace);
  µcB.workspace.clear();
  Blockly.setLocale(newLang);
Blockly.serialization.workspaces.load(JSON.parse(data), workspace);
};

Seb Seb

unread,
Jul 25, 2023, 11:24:00 AM7/25/23
to Blockly
OK, what I succeed in (I hope it's a good idea) is to create a new type:
import * as ar from 'blockly/msg/ar';
import * as en from 'blockly/msg/en';
import * as es from 'blockly/msg/es';
import * as fr from 'blockly/msg/fr';
// Create a type for the Blockly workspace language map
type LanguageMapBlockly = {
  [key: string]: { [key: string]: string };
};
// Create table for alockly workspace language map
export const languagesMapBlockly: LanguageMapBlockly = {
  'ar': ar,
  'es': es,
  'en': en,
  'fr': fr,
};
and use it with the dropdown button:
  const newLang: string = dropdownMenu.options[dropdownMenu.selectedIndex].value;
  Blockly.setLocale(languagesMapBlockly[newLang]);

If someone as a better idea, feel free to help.

Maribeth Moffatt

unread,
Jul 25, 2023, 2:20:18 PM7/25/23
to Blockly
Hi, could you clarify what is your question or what is the problem you are having? I'm not sure if you are unable to actually change the language or you just need to figure out the TypeScript types to use. Any error messages you see or a more complete description of the problem would be helpful. Thanks!

Maribeth

Seb Seb

unread,
Jul 25, 2023, 2:37:03 PM7/25/23
to Blockly
Oups, really sorry. In fact I succeeded, this problem is solved. The code I gave works fine, and is more simple than in Blockly demo code.
But I have the 'toolbox not translatin pb' : https://groups.google.com/g/blockly/c/qsctT3TNj1k
Thanks for your help.
Reply all
Reply to author
Forward
0 new messages