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

Allows users to change language when not signed-in. thus, implementing proper language redirection as in Kolibri #3721

Merged
merged 7 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { availableLanguages, currentLanguage, sortLanguages } from '../i18n';

import client from 'shared/client';

export default {
Expand Down
27 changes: 24 additions & 3 deletions contentcuration/contentcuration/frontend/shared/views/AppBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,30 @@
</VList>
</Menu>
</template>
<VBtn v-else href="/accounts" flat>
{{ $tr('logIn') }}
</VBtn>
<template v-else>
<Menu>
<template #activator="{ on }">
<VBtn flat style="text-transform: none;" v-on="on">
<Icon>person</Icon>
<Icon>arrow_drop_down</Icon>
</VBtn>
</template>
<VList>
<VListTile :href="'/accounts/'">
<VListTileAction>
<Icon>exit_to_app</Icon>
</VListTileAction>
<VListTileTitle v-text="$tr('logIn')" />
</VListTile>
<VListTile @click="showLanguageModal = true">
<VListTileAction>
<Icon>language</Icon>
</VListTileAction>
<VListTileTitle v-text="$tr('changeLanguage')" />
</VListTile>
</VList>
</Menu>
</template>
</VToolbarItems>

<template v-if="$slots.tabs" #extension>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<VListTileContent class="subheading">
<VListTileTitle>{{ $tr('administrationLink') }}</VListTileTitle>
</VListTileContent>

</VListTile>
<VListTile :href="settingsLink" @click="trackClick('Settings')">
<VListTileAction>
Expand All @@ -41,6 +42,14 @@
<VListTileTitle>{{ $tr('settingsLink') }}</VListTileTitle>
</VListTileContent>
</VListTile>
<VListTile @click="showLanguageModal = true">
<VListTileAction>
<Icon>language</Icon>
</VListTileAction>
<VListTileContent class="subheading">
<VListTileTitle v-text="$tr('changeLanguage')" />
</VListTileContent>
</VListTile>
<VListTile :href="helpLink" target="_blank" @click="trackClick('Help')">
<VListTileAction>
<Icon>open_in_new</Icon>
Expand Down Expand Up @@ -73,8 +82,15 @@
/>
</p>
</VContainer>

</VNavigationDrawer>

<LanguageSwitcherModal
v-if="showLanguageModal"
:style="{ color: $themeTokens.text }"
@cancel="showLanguageModal = false"
/>

</div>

</template>
Expand All @@ -84,18 +100,25 @@

import { mapActions, mapState } from 'vuex';
import KolibriLogo from './KolibriLogo';
import LanguageSwitcherModal from 'shared/languageSwitcher/LanguageSwitcherModal';

export default {
name: 'MainNavigationDrawer',
components: {
KolibriLogo,
LanguageSwitcherModal,
},
props: {
value: {
type: Boolean,
default: false,
},
},
data() {
return {
showLanguageModal: false,
};
},
computed: {
...mapState({
user: state => state.session.currentUser,
Expand Down Expand Up @@ -131,6 +154,7 @@
channelsLink: 'Channels',
administrationLink: 'Administration',
settingsLink: 'Settings',
changeLanguage: 'Change language',
helpLink: 'Help and support',
logoutLink: 'Sign out',
copyright: '© {year} Learning Equality',
Expand Down
2 changes: 2 additions & 0 deletions contentcuration/contentcuration/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,5 @@ def gettext(s):


DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

LANGUAGE_COOKIE_AGE = 3600 * 24 * 14
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{% load js_reverse %}

{% js_reverse_inline %}

{% autoescape off %}
{{ webpack_service_worker }}
Expand Down
150 changes: 150 additions & 0 deletions contentcuration/contentcuration/tests/test_setlanguage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import json

from django.conf import settings
from django.http import HttpResponseNotAllowed
from django.test import override_settings
from django.test import TestCase
from django.urls import reverse
from django.urls import translate_url
from django.utils.translation import get_language
from django.utils.translation import LANGUAGE_SESSION_KEY


@override_settings(LANGUAGE_CODE="en")
class I18NTests(TestCase):
"""
Tests set_language view in kolibri/core/views.py
Copied from https://github.com/django/django/blob/stable/1.11.x/tests/view_tests/tests/test_i18n.py
"""

def set_post_data(self, lang_code, next_url=""):
post_data = {
"language": lang_code,
"next": next_url,
}
return json.dumps(post_data)

def _get_inactive_language_code(self):
"""Return language code for a language which is not activated."""
current_language = get_language()
return [
code for code, name in settings.LANGUAGES if not code == current_language
][0]

def test_setlang(self):
"""
The set_language view can be used to change the session language.
"""
lang_code = self._get_inactive_language_code()
response = self.client.post(reverse("set_language"), self.set_post_data(lang_code), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content.decode("utf-8"),
translate_url(reverse("base"), lang_code),
)
self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code)

def test_setlang_next_valid(self):
"""
The set_language view can be used to change the session language.
The user is redirected to the "next" argument.
"""
lang_code = self._get_inactive_language_code()
next_url = reverse("channels")
response = self.client.post(reverse("set_language"), self.set_post_data(lang_code, next_url), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content.decode("utf-8"),
translate_url(reverse("channels"), lang_code),
)
self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code)

def test_setlang_next_invalid(self):
"""
The set_language view can be used to change the session language.
The user is redirected to base redirect if the "next" argument is invalid.
"""
lang_code = self._get_inactive_language_code()
next_url = "/not/a/real/url"
response = self.client.post(reverse("set_language"), self.set_post_data(lang_code, next_url), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content.decode("utf-8"),
translate_url(reverse("base"), lang_code),
)
self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code)

def test_setlang_null(self):
"""
Test language code set to null which shoul direct to default language "en"
"""
lang_code = self._get_inactive_language_code()
response = self.client.post(reverse("set_language"), self.set_post_data(lang_code), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content.decode("utf-8"),
translate_url(reverse("base"), lang_code),
)
self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code)
lang_code = None
response = self.client.post(reverse("set_language"), self.set_post_data(lang_code), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content.decode("utf-8"),
translate_url(reverse("base"), "en"),
)
self.assertFalse(LANGUAGE_SESSION_KEY in self.client.session)

def test_setlang_null_next_valid(self):
"""
The set_language view can be used to change the session language.
The user is redirected to the "next" argument.
"""
lang_code = self._get_inactive_language_code()
response = self.client.post(reverse("set_language"), self.set_post_data(lang_code), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content.decode("utf-8"),
translate_url(reverse("base"), lang_code),
)
self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code)
next_url = reverse("channels")
lang_code = None
response = self.client.post(reverse("set_language"), self.set_post_data(lang_code, next_url), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content.decode("utf-8"),
translate_url(reverse("channels"), "en"),
)
self.assertFalse(LANGUAGE_SESSION_KEY in self.client.session)

def test_setlang_null_next_invalid(self):
"""
The set_language view can be used to change the session language.
The user is redirected to user redirect if the "next" argument is invalid.
"""
lang_code = self._get_inactive_language_code()
response = self.client.post(reverse("set_language"), self.set_post_data(lang_code), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content.decode("utf-8"),
translate_url(reverse("base"), lang_code),
)
self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code)
next_url = "/not/a/real/url"
lang_code = None
response = self.client.post(reverse("set_language"), self.set_post_data(lang_code, next_url), content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content.decode("utf-8"),
translate_url(reverse("base"), "en"),
)
self.assertFalse(LANGUAGE_SESSION_KEY in self.client.session)

def test_setlang_get(self):
"""
The set_language view is forbidden to be accessed via GET
"""
lang_code = self._get_inactive_language_code()
response = self.client.get(reverse("set_language"), params=self.set_post_data(lang_code), content_type='application/json')
self.assertEqual(type(response), HttpResponseNotAllowed)
Loading