Skip to content

Commit

Permalink
add loadLanguage functions for updating language
Browse files Browse the repository at this point in the history
  • Loading branch information
evemartin committed Apr 26, 2024
1 parent c6026cf commit 732ee68
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
6 changes: 6 additions & 0 deletions game/static/game/js/blocklyControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ ocargo.BlocklyControl.prototype.prepare = function (blocks) {
}
};

ocargo.BlocklyControl.prototype.updateBlockLanguage = function (language_code) {
loadLanguage("/static/game/js/blockly/msg/js/", language_code, function() {
reloadWorkspace(Blockly.mainWorkspace);
});
};

ocargo.BlocklyControl.prototype.redrawBlockly = function () {
Blockly.svgResize(Blockly.mainWorkspace);
};
Expand Down
2 changes: 1 addition & 1 deletion game/static/game/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ function setMutedCookie(mute) {
}

function gameUpdateBlockLanguage(language_code) {
console.log(language_code);
ocargo.blocklyControl.updateBlockLanguage(language_code)
}

$(document).ready(function () {
Expand Down
21 changes: 21 additions & 0 deletions game/static/game/js/loadLanguages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function loadLanguage(path, langStr, callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/javascript");
xobj.open('GET', path + langStr + '.js', true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
eval(xobj.responseText);
callback();
} else if (xobj.status == "404") {
loadLanguage(path, "en", callback);
}
};
xobj.send(null);
};

function reloadWorkspace(workspace) {
var blocklyDom = Blockly.Xml.workspaceToDom(workspace);
workspace.clear();
Blockly.Xml.domToWorkspace(blocklyDom, workspace);
workspace.updateToolbox(BLOCKLY_XML);
};

0 comments on commit 732ee68

Please sign in to comment.