Menu template in theme skeleton throws empty warnings with --printI18nWarnings flag

I run hugo --printI18nWarnings and the warning below showed up, but empty? Is there a way to find the missing translation?

WARN  i18n|MISSING_TRANSLATION|en|
WARN  i18n|MISSING_TRANSLATION|fr|

I don’t know if there is any better way since I’m not very familiar with Hugo, but there is one way that will solve your problem for non-dynamic ones I think.

  1. open the folder in a text editor, such as vs code.
  2. search for all i18n references in files.
  3. select all ids and copy them to a new text file A.
  4. enter the i18n file, get all the ids in the translation file, and copy them to text file B.
  5. sort all lines in A and B, then unique all the ids by replacing ^(.*)(\n\1)+$ with $1 (remember to turn on the regex option).
  6. right click to select file A for comparison, then compare with file B, the marked lines are most likely the ids you missed.

This template code:

{{ T "" }}

Produces these warnings:

WARN  i18n|MISSING_TRANSLATION|en|
WARN  i18n|MISSING_TRANSLATION|de|

You’ve got an empty string somewhere, either a literal value or a var.

I have checked every file twice and I can’t find an empty value for i18n. I wish there was a way to get more precise info on this.

EDIT: Found out this menu template is the one generating the empty warnings. cc @jmooring (Removing the menu template makes the warnings disappear)

Edit 2: Replicated the same on a new site install. Might be a bug.

hugo new site test-site
cd test-site
hugo new theme test
echo 'theme = "test"' >> hugo.toml
hugo --printI18nWarnings
$ hugo env
hugo v0.119.0-b84644c008e0dc2c4b67bd69cccf87a41a03937e+extended windows/amd64 BuildDate=2023-09-24T15:20:17Z VendorInfo=gohugoio
GOOS="windows"
GOARCH="amd64"
GOVERSION="go1.21.1"
github.com/sass/libsass="3.6.5"
github.com/webmproject/libwebp="v1.2.4"
1 Like

Yes, it is. Sorry about that. You can use this instead:

layouts/partials/menu.html
{{- /*
Renders a menu for the given menu ID.

@context {page} page The current page.
@context {string} menuID The menu ID.

@example: {{ partial "menu.html" (dict "menuID" "main" "page" .) }}
*/}}

{{- $page := .page }}
{{- $menuID := .menuID }}

{{- with index site.Menus $menuID }}
  <nav>
    <ul>
      {{- partial "inline/menu/walk.html" (dict "page" $page "menuEntries" .) }}
    </ul>
  </nav>
{{- end }}

{{- define "partials/inline/menu/walk.html" }}
  {{- $page := .page }}
  {{- range .menuEntries }}
    {{- $attrs := dict "href" .URL }}
    {{- if $page.IsMenuCurrent .Menu . }}
      {{- $attrs = merge $attrs (dict "class" "active" "aria-current" "page") }}
    {{- else if $page.HasMenuCurrent .Menu .}}
      {{- $attrs = merge $attrs (dict "class" "ancestor" "aria-current" "true") }}
    {{- end }}
    {{- $name := .Name }}
    {{- with .Identifier }}
      {{- with T . }}
        {{- $name = . }}
      {{- end }}
    {{- end }}
    <li>
      <a
        {{- range $k, $v := $attrs }}
          {{- with $v }}
            {{- printf " %s=%q" $k $v | safeHTMLAttr }}
          {{- end }}
        {{- end -}}
      >{{ $name }}</a>
      {{- with .Children }}
        <ul>
          {{- partial "inline/menu/walk.html" (dict "page" $page "menuEntries" .) }}
        </ul>
      {{- end }}
    </li>
  {{- end }}
{{- end }}

I will update:

The revised template will still create empty warnings when using the --printI18nWarnings flag if a menu entry has an identifier without a translation. That’s an imperfect situation, but in my view a reasonable compromise to provide an out-of-the-box solution for multilingual sites.

We’ll see how this goes over the coming months; we can always revisit.

I think it’s easier to remove code than to explain how/where to add it. You can always comment this out for monolingual sites or sites that translate menu entries via site configuration.

{{- with .Identifier }}
  {{- with T . }}
    {{- $name = . }}
  {{- end }}
{{- end }}
1 Like

I don’t have an identifier in my menus. But still adding that revised code made the warning to disappear. Perhaps also test if the skeleton with the base layouts also generates those warnings.

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