From 64219d2643421bd4755385d7c04b03bdc8add93d Mon Sep 17 00:00:00 2001 From: Dusan Vuckovic Date: Mon, 18 Sep 2023 11:34:13 +0100 Subject: [PATCH] Maintenance: Add language switcher to the page header. --- _static/theme/language_switcher.js | 72 ++++++++++++++++++++++++++++++ _static/theme/theme_overrides.css | 6 +++ conf.py | 4 ++ 3 files changed, 82 insertions(+) create mode 100644 _static/theme/language_switcher.js diff --git a/_static/theme/language_switcher.js b/_static/theme/language_switcher.js new file mode 100644 index 00000000..ad57c4f6 --- /dev/null +++ b/_static/theme/language_switcher.js @@ -0,0 +1,72 @@ +const CONTRIBUTE_URL = 'https://docs.zammad.org/en/latest/contributing/start.html#translation' + +// Initialize the switcher only when RTD panel gets loaded. +// If the menu has exactly the 6 sections, the first one on top is the language chooser. +// We must check it in this way since the label may be translated based on the browser locale. +$(document).ready(() => { + const intervalId = setInterval(() => { + if ($('.rst-other-versions dt').length !== 6) return + clearInterval(intervalId); + initLanguageSwitcher() + }, 100) +}) + +const initLanguageSwitcher = () => { + const languages = [] + + const languagesList = $('.rst-other-versions dt').eq(0) + const languagesLabel = languagesList.text() + + languagesList.siblings('dd') + .each((_index, item) => { + const languageId = $(item).text().trim() + const languageFlag = `${languageFlags[languageId]} ` || '' + const languageName = languageNames[languageId] || languageId + + languages.push({ + id: languageId, + label: `${languageFlag}${languageName}`, + url: $(item).find('a').attr('href'), + isCurrent: $(item).hasClass('rtd-current-item'), + }) + }) + + if (!languages.length) return + + const switcherLanguageOptions = languages.map((language) => + $('') + .attr('label', languagesLabel) + .html(switcherLanguageOptions) + + const switcherContributeOption = $('') + .attr('label', 'Missing language?') + .html(switcherContributeOption) + + const switcherSelection = $('