i18n: support multilang content #271

This commit is contained in:
Sören Weber 2022-06-12 19:24:14 +02:00
parent b154db1f9e
commit 666d4d3251
No known key found for this signature in database
GPG key ID: 07D17FF580AE7589
6 changed files with 47 additions and 7 deletions

View file

@ -42,6 +42,7 @@ title = "Hugo Relearn Documentation"
# graphs; you usually will not need it and you should remove this for
# security reasons
mermaidInitialize = "{ \"securityLevel\": \"loose\" }"
additionalContentLanguage = [ "en" ]
[outputs]
# add JSON to the home to support lunr search; This is a mandatory setting

View file

@ -72,6 +72,8 @@ Note that some of these parameters are explained in details in other sections of
titleSeparator = "-"
# If set to true, the menu in the sidebar will be displayed in a collapsible tree view.
collapsibleMenu = false
# If a single page can contain content in multiple languages, add those here
additionalContentLanguage = [ "en" ]
```
## A word on running your site in a subfolder

View file

@ -14,6 +14,10 @@ This document shows you what's new in the latest release. For a detailed list of
---
## 4.1.0
- **New**: While fixing issues with the search functionality for non latin languages, you can now [configure to have multiple languages on a single page]({{% relref "cont/i18n/#search-with-mixed-language-support" %}}).
## 4.0.0
- **Breaking**: The `custom_css` config parameter was removed from the configuration. If used in an existing installation, it can be achieved by overriding the `custom-header.html` template in a much more generic manner.

View file

@ -52,6 +52,34 @@ Be aware that only translated pages are displayed in menu. It's not replaced wit
Use [slug](https://gohugo.io/content-management/multilingual/#translate-your-content) Front Matter parameter to translate urls too.
{{% /notice %}}
## Search
In case each page's content is written in one single language only, the above configuration will already configure the site's search functionality correctly.
{{% notice warning %}}
Although the theme supports a wide variety of supported languages, the site's search does not.
You'll see error reports in your browsers console log for each unsupported language. Currently unsupported are:
- Indonesian
- Korean
- Polish
{{% /notice %}}
### Search with mixed language support
In case your page's content contains text in multiple languages (e.g. you are writing a russian documentation for your english API), you can add those languages to your `config.toml` to broaden search.
```toml
[params]
additionalContentLanguage = [ "en" ]
````
As this is an array, you can add multiple additional languages.
{{% notice note %}}
Keep in mind that the language code required here, is the base language code. E.g. if you have additonal content in `zh-CN`, you have to add just `zh` to this parameter.
{{% /notice %}}
## Overwrite translation strings
Translations strings are used for common default values used in the theme (*Edit* button, *Search placeholder* and so on). Translations are available in English and Piratized English but you may use another language or want to override default values.

View file

@ -5,11 +5,19 @@
</div>
{{- $assetBusting := not .Site.Params.disableAssetsBusting }}
{{- $pageBaseLang := replaceRE "([a-z]+).*" "${1}" .Page.Lang }}
{{- $contentlangs := (union (slice | append .Site.Params.contentLang) (slice $pageBaseLang)) }}
{{- $quotedcontentlangs := slice }}
{{- range $contentlangs }}
{{- $quotedcontentlangs = $quotedcontentlangs | append (printf "'%s'" .) }}
{{- end }}
<script>
var pageBaseLang="{{ $pageBaseLang }}";
var contentLangs=[{{ delimit $quotedcontentlangs ", " | safeJS }}];
</script>
<script src="{{"js/auto-complete.js" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{"js/lunr.min.js" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{"js/lunr.stemmer.support.min.js" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{ (printf "js/lunr.%s.min.js" $pageBaseLang) | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}"></script>
<script src="{{"js/lunr.multi.min.js" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}"></script>
{{- range $contentlangs }}
<script src="{{ (printf "js/lunr.%s.min.js" .) | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}"></script>
{{- end }}
<script src="{{ "js/search.js" | relURL }}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}"></script>

View file

@ -9,10 +9,7 @@ function initLunr() {
// Set up lunrjs by declaring the fields we use
// Also provide their boost level for the ranking
lunrIndex = lunr(function() {
if(lunr[pageBaseLang]){
this.use(lunr[pageBaseLang]);
}
this.use(lunr.multiLanguage.apply(null, contentLangs));
this.ref('index');
this.field('title', {
boost: 15