Multilingual Page 404 Not Found?

I’ve been trying to add a second language to my site. Seems simple enough; add a languages section in config:

[Languages]
[Languages.en]
weight = 1
[Languages.fr]
weight = 2

Then simply translate documents as needed. For instance, /content/page.md and /content/page.fr.md will be automatically linked.

When navigating to localhost:1313/page/, I see the original English page, no problem. However, when navigating to localhost:1313/fr/page/, all that’s served is “404 not found”.

Is there some sort of gotcha not written in the docs?

Things I tried:

  • requiring the default language to also have a subfolder /en
  • printing the link to the translated document (didn’t show up)
  • looked at the console, both languages appear when generating the site
  • deleted the generated files and re-served
  • updated Hugo to the latest version
  • tried finding people with the same problem
  • started the multilingual modifications over from scratch
  • simplified the config changes and tried capitalizing [Languages] because of a reference found online
  • searched through the forum, nobody seems to be experiencing this problem, I must be doing something stupid

Please post the front matter of content/page.fr.md

If your pages are set to draft your local server might not load them if you don’t tell it to publish drafts. If you have other files in the content directory they might override the contents somehow. If you have more language settings (like defaultContentLanguage or disableLanguages set they might influence the results. All that requires a bit more than a short config copy-paste :slight_smile:

There actually is a gotcha here: the order of publishDir in config.toml.

Originally, the [Languages] section was defined, followed by publishDir. Maybe because [Languages.en] was defined first, it did not go to the publish directory, but to a default /public folder. Building the site then looked like this:

  • docs/fr – sub-folder with French content, into the defined assetDir
  • public – english content

When serving, Hugo grabbed content from the public folder, so URLs like .../fr/ were pointing to public/fr/ instead of docs/fr/.

Solution
Put the publishDir first, followed by the [Languages] array. For example:

publishDir = "docs"
[Languages]
    [Languages.en]
        weight = 1
    [Languages.fr]
        weight = 2

Could this be a bug with Hugo? Seems like an odd and rather unhelpful feature.

No. This behavior is defined in the TOML specification for tables:

The top-level table, also called the root table, starts at the beginning of the document and ends just before the first table header (or EOF). Unlike other tables, it is nameless and cannot be relocated.

The publishDir = "docs" key/value pair is part of the top level table, so it must appear before any of the other tables.

2 Likes

Well that would explain a few problems, wouldn’t it.

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