Skip to content

Commit

Permalink
Respect the language in non-live search
Browse files Browse the repository at this point in the history
When searching "live", thanks to the `lang` attribute of the `<html>`
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 `<html>` 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 <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Sep 11, 2024
1 parent 23433ad commit a688de3
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions static/js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,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;
Expand Down Expand Up @@ -295,6 +296,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);
Expand Down

0 comments on commit a688de3

Please sign in to comment.