From 8974b6c533789fe5d2a87dbdeeef29a58e29a71f Mon Sep 17 00:00:00 2001 From: sergioisidoro Date: Sat, 6 Jun 2020 20:38:43 +0300 Subject: [PATCH] dynamic-menu-configuration --- content/en/content-management/multilingual.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/content/en/content-management/multilingual.md b/content/en/content-management/multilingual.md index d1e7965b2..d384bd86b 100644 --- a/content/en/content-management/multilingual.md +++ b/content/en/content-management/multilingual.md @@ -507,6 +507,40 @@ The rendering of the main navigation works as usual. `.Site.Menus` will just con ``` +### Dynamically localizing menus with i18n +While customizing menus per language is useful, if you have a log of languages your config file can become hard to maintain. + +If your menus are the same in all languages, and the only thing that changes is the actual translation, you can use the menu `.Identifier` as a translation key for the name of the the menu: + +{{< code-toggle file="config" >}} +[[menu.main]] +name = "About me" +url = "about" +weight = 1 +identifier = "about" +{{< /code-toggle >}} + +You now need to specify the translations for the menu keys in the i18n files: + +{{< code file="i18n/pt.toml" >}} +[about] +other="Sobre mim" +{{< /code >}} + +And do the appropriate changes in the menu code to use the `i18n` tag with the `.Identifier` as a key. You will also note that here we are using a `default` to fall back to `.Name`, in case the `.Identifier` key is also not present in the language specified in the `defaultContentLanguage` configuration. + +{{< code file="layouts/partials/menu.html" >}} + +{{< /code >}} + + ## Missing Translations If a string does not have a translation for the current language, Hugo will use the value from the default language. If no default value is set, an empty string will be shown.