Multi-language site, how to make linktitle correctly display the title of the corresponding language in the menu template

Multi-language site, how to make linktitle correctly display the title of the corresponding language in the menu template

            <a class="nav-link" href="{{- .PageRef | relLangURL -}}" title="{{ or (T .Identifier) .Name | safeHTML }}">{{ or (T .Identifier) .Name | safeHTML }}</a>
            <a class="nav-link" href="{{- .PageRef | relLangURL -}}" title="{{ .Page.LinkTitle | safeHTML }}">{{ .Page.LinkTitle | safeHTML }}</a>

The first line of code can correctly display the corresponding translation fields on different language pages, but there is a disadvantage, that is, if it is changed, the hugo translation file needs to be modified.

The goal of the second line of code is to directly use the content of the title field in the content file without using the i18n translation file.

The problem we are encountering now is that the .Page.LinkTitle in the second line of code only displays the title of the default language. In other language pages, the navigation bar menu only displays the title field in the default content file instead of other languages. .

I don’t know if I expressed it clearly, through Google Translate.

code context

{{ or (T .Identifier) .Name | safeHTML }}

It queries the translation table for the current language using the menu entry’s identifier and returns the translated string. If the translation table does not exist, or if the identifier key is not present in the translation table, it falls back to name.

or if the identifier key is not present in the translation table, it falls back to name .

I think the expected work has not been completed. When the corresponding key does not exist in the translation table, the name is not used.

For multilingual menus, is this not working for you ?

What you said requires writing some Identifier translations in the translation table file. I want to use the title fields in files in different languages ​​to be used as menu text in pages in different languages. In this way, when modifying the menu, you only need to modify the text in one position.

You can simple use page param to get this.

However, for site menu, using config menu settings is better.

That is when there is only one menu. If there are multiple different menus, it should be convenient if the name of the menu can directly use the title of the content page. For multilingual sites.

It is currently found that this automatically uses the title in the content file as the menu title text in multi-language sites.

It seems that I only need to replace identifier with name to meet my requirements. There is no need to use another translation table.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.