hugo-theme-meme
hugo-theme-meme copied to clipboard
How to make multilingual Websites?
How do I need to organize my posts for multilanguage websites? I dont want to use subdirectories but suffixes on the files. From the Hugo documentation this should be possible.
Assume de
is your default content language:
-
change
defaultContentLanguage
tode
-
add
[languages]
params to the bottom ofconfig.toml
, e.g.########################################## # Configure Multilingual Mode # de [languages.de] languageName = "Deutsch" weight = 1 languageCode = "de-DE" # en [languages.en] languageName = "English" weight = 2 languageCode = "en-US"
-
add a
de
post, e.g.hello-world.de.md
-
set
enableLangToggle
totrue
-
hugo server
I tried that and got this error:
$ hugo server Building sites … Built in 147 ms Error: Error building site: failed to render pages: render of "section" failed: execute of template failed: template: section/categories.html:2:7: executing "main" at <partial "pages/tree-sections.html" .>: error calling partial: execute of template failed: template: partials/pages/tree-sections.html:8:19: executing "partials/pages/tree-sections.html" at <partial "utils/tree-sections.html" .>: error calling partial: "E:\Hugo\sites\github\themes\meme\layouts\partials\utils\tree-sections.html:10:15": execute of template failed: template: partials/utils/tree-sections.html:10:15: executing "treeSections" at <readDir $path>: error calling readDir: failed to read directory "./%!s(
)": open E:\Hugo\sites\github%!s( ): Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung ist falsch.
My content directory looks like this: E:\HUGO\SITES\GITHUB\CONTENT ├───about ├───categories └───posts ...........├───allgemein ...........├───programmieren ...........└───smarthome
with subdirectories under posts for my categories. my guess is that is part of the issue. in each directory is an _index.md file and in the categories folders there is at least one post. I tried adding translated posts in each category but that didnt help.
I see, it's a problem of tree-sections
partial, try this:
##########################################
# Configure Multilingual Mode
# de
[languages.de]
languageName = "Deutsch"
weight = 1
languageCode = "de-DE"
+ contentDir = "content"
# en
[languages.en]
languageName = "English"
weight = 2
languageCode = "en-US"
+ contentDir = "content"
That works! Thank you very much!
Hello! today I upgraded my Hugo and pulled the newest version of the theme and the same problem came back.
After tinkering around a bit I found a solution for my specific usecase. Apparently Hugo changed something around the contentDir
and does not fill the variable $.Site.Params.contentDir
when I use "translation by filename" instead of "translation by content directory".
My fix was a change in tree-sections.html in line 4: from
{{- $contentDir := $.Site.Params.contentDir -}}
to
{{- $contentDir := `content` -}}
Now it works but it's a hack rather than a solution. ;)
This might be helpful https://github.com/reuixiy/hugo-theme-meme/issues/432#issuecomment-1600817573.