CaiJimmy / hugo-theme-stack

Card-style Hugo theme designed for bloggers
https://stack.jimmycai.com
GNU General Public License v3.0
4.89k stars 1.6k forks source link

fix(sidebar): hide translations selector if only one translation available #950

Closed jamesmcmurry closed 7 months ago

jamesmcmurry commented 7 months ago

Hide the left sidebar translations option if only one translation is defined in the site configuration

ottok commented 7 months ago

I have not tested this but idea looks good to me!

ottok commented 7 months ago

I am seeing this bug on my site as well:

image

I confirm that the PR fixes it, but probably best to polish it following Jimmy's feedback.

ottok commented 7 months ago

Note that #925 is targeting the same section of code, so once either one is merged, the other one needs to be rebased.

andreagrandi commented 7 months ago

I was about to propose a similar pull request 😯 I was also seeing the same widget shown here https://github.com/CaiJimmy/hugo-theme-stack/pull/950#issuecomment-1962230394 even if I don't have any translated content in my whole website.

I did something different by the way (note: I'm using this theme as hugo module):

            {{ if (default false .Site.Params.languageSelector.toggle) }}
                {{- $currentLanguageCode := .Language.Lang -}}
                {{ with .Site.Home.AllTranslations }}
                    <li id="i18n-switch">  
                        {{ partial "helper/icon" "language" }}
                        <select name="language" onchange="window.location.href = this.selectedOptions[0].value">
                            {{ range . }}
                                <option value="{{ .Permalink }}" {{ if eq .Language.Lang $currentLanguageCode }}selected{{ end }}>{{ .Language.LanguageName }}</option>
                            {{ end }}
                        </select>
                    </li>
                {{ end }}
            {{ end }}

basically I added this check {{ if (default false .Site.Params.languageSelector.toggle) }} at the beginning, so the widget is hidden, unless you explicitly add:

languageSelector:
    toggle: true

to your params.

Note: this works, but I would hugely prefer if it was something fixed upstream (like this PR) so I don't have to maintain my own customisation.

CaiJimmy commented 7 months ago

I'm going to merge this for now, since this issue will affect all people using the latest Hugo. The type casting can be fixed later.