Added Support for custom country flags

This commit is contained in:
Pablo Marcos 2021-05-10 10:52:15 +02:00
parent 2fd1896327
commit 104836ba95
4 changed files with 22 additions and 18 deletions

View file

@ -1,14 +1,18 @@
{{/* if there is no custom flag provided, we define the flag with the country code */}}
{{ $languageCode:= .Lang }}
{{/* by default the language code and the country code are same */}}
{{ $countryCode:= $languageCode }}
{{/* language code and country code are not same for some countries. we need to fix them. */}}
{{ if eq $languageCode "en" }}
{{ $countryCode = "gb" }}
{{ else if eq $languageCode "bn" }}
{{ $countryCode = "bd" }}
{{ else if eq $languageCode "hi" }}
{{ $countryCode = "in" }}
{{/* if the user has selected a custom flag, display it */}}
{{ if isset .Site.Params "flag" }}
{{ $countryCode = .Param "flag" }}
{{ else }}
{{/* but some language codes dont have an equivalent country code. we need to fix them. */}}
{{ if eq $languageCode "en" }}
{{ $countryCode = "gb" }}
{{ else if eq $languageCode "bn" }}
{{ $countryCode = "bd" }}
{{ else if eq $languageCode "hi" }}
{{ $countryCode = "in" }}
{{ end }}
{{ end }}
{{/* return the country code */}}

View file

@ -5,17 +5,17 @@
<div class="dropdown languageSelector">
<a class="btn dropdown-toggle" href="#" id="languageSelector" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ $countryCode := partial "helpers/country-code.html" .Language }}
{{ $countryCode := partial "helpers/country-code.html" . }}
<img class="flag" src="https://www.countryflags.io/{{ $countryCode }}/flat/16.png" alt="{{ $countryCode }}">
{{ site.Language.LanguageName }}
</a>
<div class="dropdown-menu" aria-labelledby="languageSelector">
{{ range .Translations }}
<a class="dropdown-item nav-link languages-item" href="{{ path.Join "/" (cond (eq .Language.Lang "en") "" .Language.Lang) $pageURL }}">
{{ $countryCode := partial "helpers/country-code.html" .Language }}
{{ $countryCode := partial "helpers/country-code.html" . }}
<img class="flag" src="https://www.countryflags.io/{{ $countryCode }}/flat/24.png" alt="{{ $countryCode }}">
{{ .Language.LanguageName }}
</a>
{{ end }}
</div>
</div>
</div>

View file

@ -5,17 +5,17 @@
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="languageSelector" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ $countryCode := partial "helpers/country-code.html" .Language }}
{{ $countryCode := partial "helpers/country-code.html" . }}
<span class="flag-icon flag-icon-{{$countryCode}}"></span>
{{ site.Language.LanguageName }}
</a>
<div class="dropdown-menu" aria-labelledby="languageSelector">
{{ range .Translations }}
<a class="dropdown-item nav-link languages-item" href="{{ path.Join "/" (cond (eq .Language.Lang "en") "" .Language.Lang) $pageURL }}">
{{ $countryCode := partial "helpers/country-code.html" .Language }}
{{ $countryCode := partial "helpers/country-code.html" . }}
<span class="flag-icon flag-icon-{{$countryCode}}"></span>
{{ .Language.LanguageName }}
</a>
{{ end }}
</div>
</li>
</li>

View file

@ -1,16 +1,16 @@
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="languageSelector" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ $countryCode := partial "helpers/country-code.html" .Language }}
{{ $countryCode := partial "helpers/country-code.html" . }}
<span class="flag-icon flag-icon-{{$countryCode}}"></span>
{{ site.Language.LanguageName }}
</a>
<div class="dropdown-menu" aria-labelledby="languageSelector">
{{ range site.Home.AllTranslations }}
<a class="dropdown-item nav-link languages-item" href="{{ .RelPermalink }}">
{{ $countryCode := partial "helpers/country-code.html" .Language }}
{{ $countryCode := partial "helpers/country-code.html" . }}
<span class="flag-icon flag-icon-{{$countryCode}}"></span>
{{ .Language.LanguageName }}
</a>
{{ end }}
</div>
</li>
</li>