From eba7e15358c085246c6530816df7e0532314b05b Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Thu, 19 Jan 2023 15:56:05 +0400 Subject: [PATCH] ZIM viewer i18n via userlang query parameter ZIM viewer is now internally internationalized but the UI language can only be set by providing the `userlang` query parameter in the URL: Example: /viewer?userlang=fr#wikipedia_en_climate_change_mini_2021-03/A/index ^^^^^^^^^^^^ --- static/skin/viewer.js | 41 ++++++++++++++++++++++++++++++++++++----- static/viewer.html | 2 ++ test/server.cpp | 6 ++++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/static/skin/viewer.js b/static/skin/viewer.js index d4eeed8b8..94546f916 100644 --- a/static/skin/viewer.js +++ b/static/skin/viewer.js @@ -2,10 +2,14 @@ // // user url: identifier of the page that has to be displayed in the viewer // and that is used as the hash component of the viewer URL. For -// book resources the address url is {book}/{resource} . +// book resources the user url is {book}/{resource} . // // iframe url: the URL to be loaded in the viewer iframe. +let viewerState = { + uiLanguage: 'en', +}; + function userUrl2IframeUrl(url) { if ( url == '' ) { return blankPageUrl; @@ -68,14 +72,24 @@ function makeJSLink(jsCodeString, linkText, linkAttr="") { function suggestionsApiURL() { - return `${root}/suggest?content=${encodeURIComponent(currentBook)}`; + const uriEncodedBookName = encodeURIComponent(currentBook); + const userLang = viewerState.uiLanguage; + return `${root}/suggest?userlang=${userLang}&content=${uriEncodedBookName}`; +} + +function setTitle(element, text) { + if ( element ) { + element.title = text; + if ( element.hasAttribute("aria-label") ) { + element.setAttribute("aria-label", text); + } + } } function setCurrentBook(book, title) { currentBook = book; currentBookTitle = title; - homeButton.title = `Go to the main page of '${title}'`; - homeButton.setAttribute("aria-label", homeButton.title); + setTitle(homeButton, $t("home-button-text", {BOOK_TITLE: title})); homeButton.innerHTML = ``; bookUIGroup.style.display = 'inline'; updateSearchBoxForBookChange(); @@ -153,7 +167,7 @@ function updateSearchBoxForBookChange() { const searchbox = document.getElementById('kiwixsearchbox'); const kiwixSearchFormWrapper = document.querySelector('.kiwix_searchform'); if ( currentBookTitle ) { - searchbox.title = `Search '${currentBookTitle}'`; + searchbox.title = $t("searchbox-tooltip", {BOOK_TITLE : currentBookTitle}); searchbox.placeholder = searchbox.title; searchbox.setAttribute("aria-label", searchbox.title); kiwixSearchFormWrapper.style.display = 'inline'; @@ -402,6 +416,10 @@ function setupViewer() { return; } + const lang = getUserLanguage(); + setUserLanguage(lang, finishViewerSetupOnceTranslationsAreLoaded); + viewerState.uiLanguage = lang; + kiwixToolBarWrapper.style.display = 'block'; if ( ! viewerSettings.libraryButtonEnabled ) { document.getElementById("kiwix_serve_taskbar_library_button").remove(); @@ -417,9 +435,22 @@ function setupViewer() { if (document.body.clientWidth < 520) { setupAutoHidingOfTheToolbar(); } +} +function updateUIText() { currentBook = getBookFromUserUrl(location.hash.slice(1)); updateCurrentBook(currentBook); + + setTitle(document.getElementById("kiwix_serve_taskbar_library_button"), + $t("library-button-text")); + + setTitle(document.getElementById("kiwix_serve_taskbar_random_button"), + $t("random-page-button-text")); +} + +function finishViewerSetupOnceTranslationsAreLoaded() +{ + updateUIText(); handle_location_hash_change(); window.onhashchange = handle_location_hash_change; diff --git a/static/viewer.html b/static/viewer.html index 56eed1f9f..89b8cf84e 100644 --- a/static/viewer.html +++ b/static/viewer.html @@ -7,6 +7,8 @@ + + + + + const blankPageUrl = root + "/skin/blank.html?cacheid=6b1fa032";