diff --git a/assets/js/index.js b/assets/js/index.js index f194054..3b98a24 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -6,6 +6,7 @@ import advancedFormat from "dayjs/plugin/advancedFormat"; import times from "./theme/times"; import styles from "./theme/styles"; import highlight from "./theme/highlight"; +import languageSwitch from "./theme/languageSwitch"; import alerts from "./utils/alerts"; import tooltips from "./utils/tooltips"; @@ -22,6 +23,7 @@ CTFd.init(window.init); styles(); times(); highlight(); + languageSwitch(); alerts(); tooltips(); diff --git a/assets/js/theme/languageSwitch.js b/assets/js/theme/languageSwitch.js new file mode 100644 index 0000000..6b2ea26 --- /dev/null +++ b/assets/js/theme/languageSwitch.js @@ -0,0 +1,26 @@ +export default () => { + const form = document.getElementById('language-switch') + form.addEventListener('submit', async e => { + // Do not open API page + e.preventDefault() + + // Submit form + let data = new URLSearchParams(new FormData(form)); + const response = await fetch(form.action, { + method: 'post', + body: data + }); + if (!response.ok) { + throw new Error(`Language change failed: ${response.status}`); + } + + // Reload with new language + window.location.reload() + }) + + document.querySelectorAll("#language-switch input[type='radio']").forEach(el => { + el.addEventListener('change', e => { + form.requestSubmit() + }) + }); +}; diff --git a/templates/components/navbar.html b/templates/components/navbar.html index 4db3aea..14de095 100644 --- a/templates/components/navbar.html +++ b/templates/components/navbar.html @@ -171,6 +171,27 @@ {% endif %} + + +