Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add language dropdown #1606

Merged
merged 66 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
c8920c4
feat: add language dropdown
evemartin Apr 24, 2024
dd2a149
feat: improve language dropdown
evemartin Apr 25, 2024
8be1d3f
feat: add language dropdown test
evemartin Apr 25, 2024
f85ef27
remove test file to debug
evemartin Apr 25, 2024
c3e8198
debug tests
evemartin Apr 25, 2024
e66876a
debug tests
evemartin Apr 25, 2024
9d1d944
debug tests
evemartin Apr 25, 2024
eb25d9b
debug tests
evemartin Apr 25, 2024
cae1170
debug tests
evemartin Apr 25, 2024
5aea941
debug tests
evemartin Apr 25, 2024
83787a5
debug tests
evemartin Apr 25, 2024
348762a
Simplify callback
faucomte97 Apr 25, 2024
973189f
Merge
faucomte97 Apr 25, 2024
230a82d
debug tests
evemartin Apr 25, 2024
1c3a124
debug tests
evemartin Apr 25, 2024
0a0ce27
testing
evemartin Apr 26, 2024
1295f1f
testing
evemartin Apr 26, 2024
87c74d6
debug tests
evemartin Apr 26, 2024
f85daba
debug tests
evemartin Apr 26, 2024
c6026cf
add dropdown with dummy onchange function
evemartin Apr 26, 2024
732ee68
add loadLanguage functions for updating language
evemartin Apr 26, 2024
301bddc
add other languages
evemartin Apr 26, 2024
7a932aa
debug tests
evemartin Apr 26, 2024
5cda0dd
added loadLanguages file to html
evemartin Apr 26, 2024
0746fd6
test removal of Blockly.Msg references
evemartin Apr 26, 2024
358046b
test addition of en.js script tag
evemartin Apr 26, 2024
a770784
test removal of custom block labels
evemartin Apr 26, 2024
f352c87
test removal of extra css
evemartin Apr 26, 2024
5f9c9c5
debug tests, remove css
evemartin Apr 26, 2024
c134e22
add back removed things except for css
evemartin Apr 26, 2024
fb56ff0
test addition of en.js tag
evemartin Apr 26, 2024
8be873d
test removal of custom block labels
evemartin Apr 26, 2024
8e0f9a2
testing addition of loadLanguage at startup, removal of en.js script
evemartin Apr 26, 2024
9e3a412
test addition of en.js script
evemartin Apr 26, 2024
05de185
testing small changes
evemartin Apr 26, 2024
0e0e971
test reloading workspace instead of containing everything in callback
evemartin Apr 26, 2024
1210dc7
test if reloading the workspace works if nothing else happens
evemartin Apr 26, 2024
e7721a4
more tests to see if just reloading will work
evemartin Apr 26, 2024
778b91a
debug tests, reload
evemartin Apr 26, 2024
76ce255
add back what I changed
evemartin Apr 26, 2024
d47b9f5
removing any reloading
evemartin Apr 26, 2024
8bc3be8
testing out reload again
evemartin Apr 26, 2024
4be15f9
testing if navigator is causing bug
evemartin Apr 26, 2024
2e4c9e6
try calling loadLanguage without reloading
evemartin Apr 26, 2024
ea07969
adding some logging
evemartin Apr 26, 2024
3e6a3a3
moving loadLanguage function
evemartin Apr 26, 2024
a54c20a
try moving function again
evemartin Apr 26, 2024
dc12131
fix logic negate error
evemartin Apr 26, 2024
0794ada
add back other things now that logic negate issue fixed
evemartin Apr 29, 2024
5d53fc1
add back some removed css
evemartin Apr 29, 2024
b7ace27
update css and some text
evemartin Apr 29, 2024
f82449c
merge in testing
evemartin Apr 29, 2024
ad176ed
test removing test
evemartin Apr 29, 2024
185d315
debug tests
evemartin Apr 29, 2024
5907fab
debug tests
evemartin Apr 29, 2024
6b67a5b
debug tests
evemartin Apr 29, 2024
d1afd62
Merge branch 'master' into feat--add-language-dropdown
faucomte97 Apr 29, 2024
afa9cae
address PR comments
evemartin May 1, 2024
7d3218a
address PR comment - show dropdown symbol on language dropdown
evemartin May 1, 2024
cf02c4b
begin adding translations
evemartin May 7, 2024
5a2f248
add black to pipfile
evemartin May 7, 2024
72ca56e
add remaining translations
evemartin May 7, 2024
c34c130
comment out untranslated languages
evemartin May 7, 2024
99d08fb
add hindi
evemartin May 7, 2024
b8020bb
Merge branch 'master' into feat--add-language-dropdown
evemartin May 8, 2024
4bff6e0
address PR comments
evemartin May 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions game/end_to_end_tests/base_game_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,12 @@ def go_to_homepage(self):
def go_to_level(self, level_name):
path = reverse("play_default_level", kwargs={"levelName": str(level_name)})
self._go_to_path(path)
self.selenium.execute_script("ocargo.animation.FAST_ANIMATION_DURATION = 1;")

return GamePage(self.selenium)

def go_to_custom_level(self, level):
path = reverse("play_custom_level", kwargs={"levelId": str(level.id)})
self._go_to_path(path)
self.selenium.execute_script("ocargo.animation.FAST_ANIMATION_DURATION = 1;")

return GamePage(self.selenium)

Expand All @@ -106,7 +104,6 @@ def go_to_level_editor(self):
def go_to_episode(self, episodeId):
path = reverse("start_episode", kwargs={"episodeId": str(episodeId)})
self._go_to_path(path)
self.selenium.execute_script("ocargo.animation.FAST_ANIMATION_DURATION = 1;")

return GamePage(self.selenium)

Expand Down
4 changes: 3 additions & 1 deletion game/end_to_end_tests/game_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ class GamePage(BasePage):
def __init__(self, browser):
super(GamePage, self).__init__(browser)

assert self.on_correct_page("game_page")
self.browser.execute_script("ocargo.animation.FAST_ANIMATION_DURATION = 1;")

assert self.on_correct_page("game_page")

self._dismiss_initial_dialog()

def _dismiss_initial_dialog(self):
Expand Down
14 changes: 14 additions & 0 deletions game/end_to_end_tests/test_language_dropdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from game.end_to_end_tests.base_game_test import BaseGameTest

from selenium.webdriver.common.by import By

class TestLanguageDropdown(BaseGameTest):
def test_level_language_dropdown(self):
page = self.go_to_level(1)
assert page.element_exists_by_id("language_dropdown")

self.selenium.find_element(By.ID, "language_dropdown").click()
self.selenium.find_element(By.ID, f"language_dropdown_fr").click()

text_count = len(self.selenium.find_elements(By.XPATH, ("//*[contains(text(),'Move forwards')]")))
assert text_count == 0
10 changes: 10 additions & 0 deletions game/static/game/css/game_screen.css
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,16 @@ div.no-print div.tab label {
background: rgba(255, 255, 255, 0.25);
}

.tab select:hover {
background: rgba(255, 255, 255, 0.25);
}

.tab select {
color: black;
border: none;
font-weight: bold;
}

#console {
z-index: 100;
display: flex;
Expand Down
5 changes: 4 additions & 1 deletion game/static/game/js/blockly/msg/js/en-gb.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,7 @@ Blockly.Msg["VARIABLES_HUE"] = "330";
Blockly.Msg["TEXTS_HUE"] = "160";
Blockly.Msg["PROCEDURES_HUE"] = "290";
Blockly.Msg["COLOUR_HUE"] = "20";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";

Blockly.Msg["MOVE_FORWARDS_TITLE"] = "mouve fourwards";
Blockly.Msg["MOVE_FORWARDS_TOOLTIP"] = "Move the van forwards but British";
5 changes: 4 additions & 1 deletion game/static/game/js/blockly/msg/js/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,7 @@ Blockly.Msg["VARIABLES_HUE"] = "330";
Blockly.Msg["TEXTS_HUE"] = "160";
Blockly.Msg["PROCEDURES_HUE"] = "290";
Blockly.Msg["COLOUR_HUE"] = "20";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";

Blockly.Msg["MOVE_FORWARDS_TITLE"] = "move forwards";
Blockly.Msg["MOVE_FORWARDS_TOOLTIP"] = "Move the van forwards";
5 changes: 4 additions & 1 deletion game/static/game/js/blockly/msg/js/fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,7 @@ Blockly.Msg["VARIABLES_HUE"] = "330";
Blockly.Msg["TEXTS_HUE"] = "160";
Blockly.Msg["PROCEDURES_HUE"] = "290";
Blockly.Msg["COLOUR_HUE"] = "20";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";
Blockly.Msg["VARIABLES_DYNAMIC_HUE"] = "310";

Blockly.Msg["MOVE_FORWARDS_TITLE"] = "avancer";
Blockly.Msg["MOVE_FORWARDS_TOOLTIP"] = "Faire avancer la camionnette";
9 changes: 8 additions & 1 deletion game/static/game/js/blocklyControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ocargo.BlocklyControl = function () {
this.blocklyCustomisations.setupLimitedBlocks();
this.blocklyDiv = document.getElementById("blockly_holder");
this.toolbox = document.getElementById("blockly_toolbox");

Blockly.inject(this.blocklyDiv, {
path: "/static/game/js/blockly/",
toolbox: BLOCKLY_XML,
Expand Down Expand Up @@ -52,6 +53,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 Expand Up @@ -434,4 +441,4 @@ ocargo.BlockHandler.prototype.deselectCurrent = function () {
this.setBlockSelected(this.selectedBlock, false);
this.selectedBlock = null;
}
};
};
4 changes: 2 additions & 2 deletions game/static/game/js/blocklyCustomBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function initCustomBlocksDescription() {
init: function () {
this.setColour(160);
this.appendDummyInput()
.appendField(gettext("move forwards"))
.appendField(Blockly.Msg.MOVE_FORWARDS_TITLE)
.appendField(
new Blockly.FieldImage(
ocargo.Drawing.imageDir + "actions/forward.svg",
Expand All @@ -49,7 +49,7 @@ function initCustomBlocksDescription() {
);
this.setPreviousStatement(true, "Action");
this.setNextStatement(true, "Action");
this.setTooltip(gettext("Move the van forwards"));
this.setTooltip(Blockly.Msg.MOVE_FORWARDS_TOOLTIP);
},
};

Expand Down
96 changes: 54 additions & 42 deletions game/static/game/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,30 @@ ocargo.Game = function () {
}

ocargo.Game.prototype.setup = function () {
loadLanguage("/static/game/js/blockly/msg/js/", navigator.language.toLowerCase(), function() {
reloadWorkspace(Blockly.mainWorkspace);
});

if(new Date().getMonth() === 11) {
$("#paper").css('background-color', '#eef7ff')
}

if (NIGHT_MODE_FEATURE_ENABLED) {
if (NIGHT_MODE) {
$('#paper').css('background-color', 'black')
}
if (NIGHT_MODE_FEATURE_ENABLED) {
if (NIGHT_MODE) {
$('#paper').css('background-color', 'black')
}

if (!ANONYMOUS) {
$('#nightmode_tab').show()
if (!ANONYMOUS) {
$('#nightmode_tab').show()
}
}
}


// Function being called when there is a change in game level.
ocargo.Game.prototype.onLevelChange = function() {
const currentLevelId = LEVEL_ID;
localStorage.setItem('currentEpisode', EPISODE);
}
// Function being called when there is a change in game level.
ocargo.Game.prototype.onLevelChange = function() {
const currentLevelId = LEVEL_ID;
localStorage.setItem('currentEpisode', EPISODE);
}

restoreCmsLogin()
initCustomBlocks()
Expand All @@ -38,20 +42,21 @@ ocargo.Game.prototype.onLevelChange = function() {
ocargo.pythonControl = new ocargo.PythonControl()
ocargo.blocklyCompiler = new ocargo.BlocklyCompiler()
ocargo.model = new ocargo.Model(
PATH,
ORIGIN,
DESTINATIONS,
TRAFFIC_LIGHTS,
COWS,
MAX_FUEL
PATH,
ORIGIN,
DESTINATIONS,
TRAFFIC_LIGHTS,
COWS,
MAX_FUEL
)

this.drawing = new ocargo.Drawing(ocargo.model.startingPosition())
this.drawing.preloadRoadTiles()
ocargo.animation = new ocargo.Animation(ocargo.model, DECOR, this.drawing)
this.saving = new ocargo.Saving()
this.sharing = new ocargo.Sharing(
() => parseInt(LEVEL_ID),
() => true
() => parseInt(LEVEL_ID),
() => true
)

// Setup the blockly workspace
Expand Down Expand Up @@ -79,20 +84,20 @@ ocargo.Game.prototype.onLevelChange = function() {
// Setup blockly to python
Blockly.Python.init(Blockly.getMainWorkspace())
window.addEventListener(
'unload',
function (event) {
ocargo.pythonControl.teardown()
ocargo.blocklyControl.teardown()
}.bind(this)
'unload',
function (event) {
ocargo.pythonControl.teardown()
ocargo.blocklyControl.teardown()
}.bind(this)
)

var loggedOutWarning = ''
// Check if logged on
if (USER_STATUS == 'UNTRACKED') {
loggedOutWarning =
'<br><span class="popup__text--warning">' +
gettext("You are not logged in. Your progress won't be saved.") +
'</span>'
'<br><span class="popup__text--warning">' +
gettext("You are not logged in. Your progress won't be saved.") +
'</span>'
}
// Start the popup
var title = gettext('Try solving this one...')
Expand All @@ -113,11 +118,11 @@ ocargo.Game.prototype.onLevelChange = function() {
var message
if (NIGHT_MODE) {
message =
'<br>' +
gettext(
'In Night Mode you can only see a very short distance. ' +
"We've given you more blocks to help you find your way!"
)
'<br>' +
gettext(
'In Night Mode you can only see a very short distance. ' +
"We've given you more blocks to help you find your way!"
)
} else {
message = loggedOutWarning
}
Expand All @@ -127,15 +132,15 @@ ocargo.Game.prototype.onLevelChange = function() {
const showMascot = BLOCKLY_ENABLED && !PYTHON_VIEW_ENABLED && LEVEL_NAME <= 80; // show mascot on Blockly-only levels that are not above 80

ocargo.Drawing.startPopup(
title,
LESSON,
message,
showMascot,
[
ocargo.button.dismissButtonHtml("prev_button", gettext("Previous level")),
ocargo.button.dismissButtonHtml('play_button', gettext('Play')),
ocargo.button.dismissButtonHtml("next_button", gettext("Next level"))
]
title,
LESSON,
message,
showMascot,
[
ocargo.button.dismissButtonHtml("prev_button", gettext("Previous level")),
ocargo.button.dismissButtonHtml('play_button', gettext('Play')),
ocargo.button.dismissButtonHtml("next_button", gettext("Next level"))
]
)
}

Expand Down Expand Up @@ -624,6 +629,7 @@ ocargo.Game.prototype.isInPythonWorkspace = function () {

ocargo.Game.prototype._setupTabs = function () {
this.tabs = []

this.tabs.blockly = new ocargo.Tab(
$('#blockly_radio'),
$('#blockly_radio + label'),
Expand Down Expand Up @@ -1387,8 +1393,14 @@ function setMutedCookie(mute) {
}
}

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

$(document).ready(function () {
ocargo.game = new ocargo.Game()
ocargo.game.setup()
ocargo.game.mute(ocargo.game.isMuted)
})


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);
};
22 changes: 21 additions & 1 deletion game/templates/game/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<script defer type="text/javascript" src="{% static 'game/js/blocklyCompiler.js' %}"></script>
<script defer type="text/javascript" src="{% static 'game/js/blocklyCustomBlocks.js' %}"></script>
<script defer type="text/javascript" src="{% static 'game/js/blocklyCustomisations.js' %}"></script>
<script defer type="text/javascript" src="{% static 'game/js/loadLanguages.js' %}"></script>
<script defer type="text/javascript" src="{% static 'game/js/blocklyMessages.js' %}"></script>
<script defer type="text/javascript" src="{% static 'game/js/model.js' %}"></script>
<script defer type="text/javascript" src="{% static 'game/js/pathFinder.js' %}"></script>
Expand Down Expand Up @@ -143,6 +144,7 @@

{% block content %}
{{ block.super }}

<div style='position: fixed; top: 0; right: 0; background: black; z-index: 99'>
<div id='fps' style='font-size: 2em; color: white'></div>
</div>
Expand All @@ -158,6 +160,16 @@
<span>{% trans "Blockly" %}</span>
</label>
</div>
<div id="language_tab" class="tab">
<select name="language_dropdown" class="tab selectable bg--{{level.difficulty}}" id="language_dropdown" onchange="gameUpdateBlockLanguage(this.value)">
{% for language_code, language in available_language_dict.items %}
<option id="language_dropdown_{{language_code}}" value="{{language_code}}">{{language}}</option>
{% endfor %}
</select>
</div>

<div class="tab_break"></div>

<div id="python_tab" class="tab selectable {% if level.pythonEnabled %}{% else %}hidden{% endif %}">
<input type="radio" name="tabs" id="python_radio">
<label for="python_radio">
Expand Down Expand Up @@ -487,4 +499,12 @@ <h2 class="title"><img class="modal_image" src="{% static 'game/image/icons/shar
</div>
</div>

{% endblock %}
<script>
document.getElementById("language_dropdown").value = navigator.language.toLowerCase();

if (document.getElementById("language_dropdown").value == "") {
document.getElementById("language_dropdown").value = "en";
}
</script>

{% endblock %}
Loading
Loading