Skip to content

Commit

Permalink
fixup! ✨(dashboard) update javascript language switcher and add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ssorin committed Nov 15, 2024
1 parent 3f20aef commit 0098eb7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
44 changes: 38 additions & 6 deletions src/dashboard/dashboard/static/dashboard/js/common.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
const language_selectors = document.querySelectorAll(".fr-translate__language")
/**
* Common JavaScript used in dashboard project
*/

language_selectors.forEach(el => el.addEventListener("click", event => {
console.log(language_selectors)
document.cookie = "django_language=" + el.lang + ";Path=\"/django-dsfr\";SameSite=Strict"
window.location.reload()
}));
'use strict';

const app = window.app = {
/** ### CALL FUNCTIONS ###*/
init: function () {
this.languageSwitcher();
},

/** ### FUNCTIONS ###*/

/**
* Language switcher
* Sets django_language cookie, with the current language choice chosen by the user.
*/
languageSwitcher: function () {
const language_selectors = document.querySelectorAll(".fr-translate__language")

language_selectors.forEach(el => el.addEventListener("click", event => {
document.cookie = "django_language=" + el.lang + ";Path=\"/django-dsfr\";SameSite=Strict"
window.location.reload()
}));
}
}


/**
*
* Wait for DOM ready and init() the app
*
*/
document.onreadystatechange = function () {
if (document.readyState === "interactive") {
app.init();
}
}
5 changes: 5 additions & 0 deletions src/dashboard/templates/blocks/language_selector.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{% load i18n %}

{% comment %}
The language selector need languageSwitcher() js function (in js/common.js)
{% endcomment %}

<nav role="navigation" class="fr-translate fr-nav">
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
Expand Down

0 comments on commit 0098eb7

Please sign in to comment.