From 666d4d325164413f6d004d458696f8adc930a2a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Sun, 12 Jun 2022 19:24:14 +0200 Subject: [PATCH] i18n: support multilang content #271 --- exampleSite/config.toml | 1 + .../content/basics/configuration/_index.en.md | 2 ++ .../content/basics/migration/_index.en.md | 6 +++- exampleSite/content/cont/i18n/_index.en.md | 28 +++++++++++++++++++ layouts/partials/search.html | 12 ++++++-- static/js/search.js | 5 +--- 6 files changed, 47 insertions(+), 7 deletions(-) diff --git a/exampleSite/config.toml b/exampleSite/config.toml index 45a9cfeadb..12bc958d60 100644 --- a/exampleSite/config.toml +++ b/exampleSite/config.toml @@ -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 diff --git a/exampleSite/content/basics/configuration/_index.en.md b/exampleSite/content/basics/configuration/_index.en.md index c43c445112..ef444cdb13 100644 --- a/exampleSite/content/basics/configuration/_index.en.md +++ b/exampleSite/content/basics/configuration/_index.en.md @@ -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 diff --git a/exampleSite/content/basics/migration/_index.en.md b/exampleSite/content/basics/migration/_index.en.md index ff429b337a..62dc1344a7 100644 --- a/exampleSite/content/basics/migration/_index.en.md +++ b/exampleSite/content/basics/migration/_index.en.md @@ -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. @@ -22,7 +26,7 @@ This document shows you what's new in the latest release. For a detailed list of - **New**: All shortcodes now support named parameter. The positional parameter are still supported but will not be enhanced with new features, so you don't need to change anything in your installation. - This applies to [`expand`]({{% relref "shortcodes/expand" %}}) , [`include`]({{% relref "shortcodes/include" %}}) , [`notice`]({{% relref "shortcodes/notice" %}}) and [`siteparam`]({{% relref "shortcodes/siteparam" %}}) . + This applies to [`expand`]({{% relref "shortcodes/expand" %}}), [`include`]({{% relref "shortcodes/include" %}}), [`notice`]({{% relref "shortcodes/notice" %}}) and [`siteparam`]({{% relref "shortcodes/siteparam" %}}). - **New**: The [`button`]({{% relref "shortcodes/button" %}}) shortcode received some love and now has a parameter for the color style similar to other shortcodes. diff --git a/exampleSite/content/cont/i18n/_index.en.md b/exampleSite/content/cont/i18n/_index.en.md index 932d434fe9..14e1acd603 100644 --- a/exampleSite/content/cont/i18n/_index.en.md +++ b/exampleSite/content/cont/i18n/_index.en.md @@ -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. diff --git a/layouts/partials/search.html b/layouts/partials/search.html index 03a147a71e..b4408018c3 100644 --- a/layouts/partials/search.html +++ b/layouts/partials/search.html @@ -5,11 +5,19 @@ {{- $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 }} - + + {{- range $contentlangs }} + + {{- end }} \ No newline at end of file diff --git a/static/js/search.js b/static/js/search.js index 50767bc706..0fb45fa6cb 100644 --- a/static/js/search.js +++ b/static/js/search.js @@ -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