From e95d511017d839ddbd2a330af4caaaa8291f319f Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 25 Mar 2023 08:09:42 +0100 Subject: [PATCH] Translate messages from websockets Signed-off-by: Emmy D'Anello --- draw/consumers.py | 3 +++ draw/static/draw.js | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/draw/consumers.py b/draw/consumers.py index 18aca04..f7e8f2e 100644 --- a/draw/consumers.py +++ b/draw/consumers.py @@ -6,6 +6,7 @@ from random import randint from asgiref.sync import sync_to_async from channels.generic.websocket import AsyncJsonWebsocketConsumer from django.conf import settings +from django.utils import translation from django.utils.translation import gettext_lazy as _ from draw.models import Draw, Round, Pool, TeamDraw @@ -70,6 +71,8 @@ class DrawConsumer(AsyncJsonWebsocketConsumer): .prefetch_related('draw__current_round__current_pool__current_team').aget() match content['type']: + case 'set_language': + translation.activate(content['language']) case 'start_draw': await self.start_draw(**content) case 'abort': diff --git a/draw/static/draw.js b/draw/static/draw.js index e225b94..849015b 100644 --- a/draw/static/draw.js +++ b/draw/static/draw.js @@ -515,7 +515,12 @@ document.addEventListener('DOMContentLoaded', () => { console.error('Chat socket closed unexpectedly') }) - socket.addEventListener('open', e => {}) + socket.addEventListener('open', e => { + socket.send(JSON.stringify({ + 'type': 'set_language', + 'language': document.getElementsByName('language')[0].value, + })) + }) let format_form = document.getElementById('format-form-' + tournament.id) if (format_form !== null) {