frjo / hugo-theme-zen

A fast and clean Hugo base theme with css-grid and Hugo pipes support.
https://zen-demo.xdeb.org/
GNU General Public License v2.0
276 stars 80 forks source link

The search does not work on multi-lingual sites #97

Closed rainerhillebrand closed 11 months ago

rainerhillebrand commented 1 year ago

Dear Fredrik,

first of all thank you very much for your excellent theme!

I prepared a bilingual site based on your theme. When testing the search function I noticed that the search results are provided for the default language only. The root cause may be the fixed path '/searchindex.json' in buildIndex() in assets/js/search.js. This works fine for the default language. However, the search function on my site works only when I use '/es/searchindex.json' for the second language (which is Spanish).

Are you quite positive that the search function works on a multi-lingual site?

I partially solved this issue for my site with the following changes:

  1. scripts.html Replace
<script defer src="{{ $search.RelPermalink }}"></script>

by

<script defer src="{{ $search.RelPermalink }}" data-language="{{- with site.IsMultiLingual -}}
{{ site.Language.Lang }}
{{- end -}}"></script>
  1. search.js Replace
    fetch('/searchindex.json')

    by

    const uiLanguage=document.currentScript.dataset.language;
    var searchIndexFile = "/searchindex.json";
    if (uiLanguage.length > 0) {
    searchIndexFile = "/" + uiLanguage + searchIndexFile;
    }
    fetch(searchIndexFile)

My remaining problem is that the defaultContentLanguageInSubdir has the default value false. This means that {{ site.Language.Lang }} results in "de" but searchindex.json is not stored in '/de/searchindex.json'. I wanted to check whether defaultContentLanguageInSubdir is true or false (like LisaFC, see Check if default language content is in a language subdir?). However, I did not succeed,yet.

Best regards,

Rainer

frjo commented 1 year ago

I have built a number of multilingual sites but I do not believe I have ever enabled this search functionality on any of them.

Thanks for alerting me to this issue and providing som good data! I will investigate and report back in this issue. In might take a week or so before I have time to spend on it.

It is definitely a issue I want to fix in my theme.

frjo commented 11 months ago

@rainerhillebrand @calalloo Tank you both for alerting me to this issue and to remind me about it.

If you have time, please test this PR #106.

The search index was already getting generated for each languages. The bug was that the search javascript was not made to look for it. The PR hopefully fixes that.