From b6a835b8ee835e4be61e35944b3602442847b465 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 9 Oct 2023 13:14:44 +0200 Subject: [PATCH] Respect the language in non-live search When searching "live", thanks to the `lang` attribute of the `` element, Pagefind restricts the search to the current language. That is, when looking at a French translation of a manual page, or at a page in the French version of the ProGit2 book, typing a search term into the search box will look only at the French pages of the site. However, when pressing the Enter key, the browser navigates to /search/results, whose `` element is marked up with `lang=en`. This is correct: the language of that page is English. Nevertheless, we would want to see the same results (but this time with excerpts), i.e. we want the search results page to respect the language of the page from which the search originated. Signed-off-by: Johannes Schindelin --- static/js/application.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/static/js/application.js b/static/js/application.js index 3bce43441f..7fff83de83 100644 --- a/static/js/application.js +++ b/static/js/application.js @@ -245,8 +245,9 @@ var Search = { var link = $('#search-results a')[Search.selectedIndex]; var url = $(link).attr('href'); if(!url) { - var term = $('#search-text').val(); - url = "/search/results?search=" + term; + const term = $('#search-text').val(); + const language = document.querySelector("html")?.getAttribute("lang"); + url = `search/results?search=${term}${language && `&language=${language}`}`; } window.location.href = url; selectedIndex = 0; @@ -285,6 +286,10 @@ var Search = { } (async () => { Search.pagefind = await import(`${baseURLPrefix}pagefind/pagefind.js`); + const options = {} + const language = this.getQueryValue('language'); + if (language) options.language = language; + await Search.pagefind.options(options); await Search.pagefind.init(); await callback(); })().catch(console.log);