diff --git a/assets/js/search.js b/assets/js/search.js index f2767b4f12..a16bceb9d1 100644 --- a/assets/js/search.js +++ b/assets/js/search.js @@ -26,7 +26,44 @@ limitations under the License. } var query = $(this).val(); - var searchPage = '{{ "search/" | absURL }}?q=' + query; + //Get current language using multiple methods + function getCurrentLanguage() { + var htmlLang = document.documentElement.lang; + if (htmlLang) return htmlLang; + + // Method 2: Extract from URL path + var path = window.location.pathname; + var pathParts = path.split('/').filter(Boolean); + if (pathParts.length > 0 && pathParts[0].length === 2) { + + return pathParts[0]; + } + + //Method 3: Meta tag as as fallback + var metaLang = document.querySelector('meta[property="og:locale"]'); + if (metaLang && metaLang.content) return metaLang.content; + + // Fallback to default language from Hugo template + return '{{ .Site.Language.Lang}}'; + + } + + var currentLang = getCurrentLanguage(); + var defaultLang = '{{ .Site.Language.Lang }}'; + var basePath = '{{ "/" | absURL }}'; + + //Build the correct search based on language + + var searchPage; + if (currentLang && currentLang !== defaultLang) { + // For non-default languages: /fr/search?q==query + searchPage = basePath + currentLang + '/search?q=' + encodeURIComponent(query); + + } else { + // For default language: /search?q=query + searchPage = basePath + 'search?q=' + encodeURIComponent(query); + } + document.location = searchPage; return false; diff --git a/layouts/_partials/scripts.html b/layouts/_partials/scripts.html index 181af6566a..40f7f3374a 100644 --- a/layouts/_partials/scripts.html +++ b/layouts/_partials/scripts.html @@ -1,43 +1,44 @@ -{{ $needKaTeX := or .Params.math .Site.Params.katex.enable .Params.chem .Site.Params.chem (.Page.Store.Get "hasKaTeX") (.Page.Store.Get "hasmhchem") -}} +{{ $needKaTeX := or .Params.math .Site.Params.katex.enable .Params.chem .Site.Params.chem (.Page.Store.Get "hasKaTeX") +(.Page.Store.Get "hasmhchem") -}} {{ $needmhchem := or .Params.chem .Site.Params.katex.mhchem.enable (.Page.Store.Get "hasmhchem") -}} {{ if .Site.Params.markmap.enable -}} {{ end -}} {{ if .Site.Params.plantuml.enable -}} - + {{ end -}} {{ if $needKaTeX -}} - {{ partial "scripts/katex.html" (dict "mhchem" $needmhchem) -}} +{{ partial "scripts/katex.html" (dict "mhchem" $needmhchem) -}} {{ end -}} {{ $jsBs := resources.Get "vendor/bootstrap/dist/js/bootstrap.bundle.js" -}} {{ $jsBase := resources.Get "js/base.js" -}} -{{ $jsSearch := resources.Get "js/search.js" | resources.ExecuteAsTemplate "js/search.js" .Site.Home -}} +{{ $jsSearch := resources.Get "js/search.js" | resources.ExecuteAsTemplate "js/search.js" . -}} {{ $jsMarkmap := resources.Get "js/markmap.js" | resources.ExecuteAsTemplate "js/markmap.js" . -}} {{ $jsPlantuml := resources.Get "js/plantuml.js" | resources.ExecuteAsTemplate "js/plantuml.js" . -}} {{ $jsDrawio := resources.Get "js/drawio.js" | resources.ExecuteAsTemplate "js/drawio.js" . -}} {{ if .Site.Params.offlineSearch -}} - {{ $jsSearch = resources.Get "js/offline-search.js" -}} +{{ $jsSearch = resources.Get "js/offline-search.js" -}} {{ end -}} {{ $jsArray := slice $jsBs $jsBase $jsSearch $jsPlantuml $jsMarkmap $jsDrawio -}} @@ -47,31 +48,30 @@ {{ end -}} {{ if .Site.Params.ui.showLightDarkModeMenu -}} - {{ $jsArray = $jsArray | append (resources.Get "js/dark-mode.js") -}} +{{ $jsArray = $jsArray | append (resources.Get "js/dark-mode.js") -}} {{ end -}} {{ $js := $jsArray | resources.Concat "js/main.js" -}} {{ if hugo.IsProduction -}} - {{ $js := $js | minify | fingerprint -}} - +{{ $js := $js | minify | fingerprint -}} + {{ else -}} - + {{ end -}} {{ if .Site.Params.prism_syntax_highlighting -}} - + {{ else if ( not .Site.Params.disable_click2copy_chroma ) -}} - {{ $c2cJS := resources.Get "js/click-to-copy.js" -}} - {{ if hugo.IsProduction -}} - {{ $c2cJS = $c2cJS | minify | fingerprint -}} - {{ end -}} - +{{ $c2cJS := resources.Get "js/click-to-copy.js" -}} +{{ if hugo.IsProduction -}} +{{ $c2cJS = $c2cJS | minify | fingerprint -}} +{{ end -}} + {{ end -}} {{ if and .Site.Params.search (isset .Site.Params.search "algolia") -}} - {{ template "algolia/scripts" .Site.Params.search.algolia -}} +{{ template "algolia/scripts" .Site.Params.search.algolia -}} {{ end -}} {{ partial "hooks/body-end.html" . -}} @@ -79,16 +79,16 @@ {{ define "algolia/scripts" -}} + crossorigin="anonymous"> -{{ end -}} +{{ end -}} \ No newline at end of file