Concatenating the .Lang variable in a multilingual environment produce unexpected results

Hello there.
I’m new to Hugo and to the GoLang, but not to programming.

Today I was trying to setup a prototype for a new site I need to build, with multilingual support.

In my config file I have created something like this:

languages:
  it:
    contentDir: content/it
    languageCode: it
    title: Benvenuto
    weight: 2
  en:
    contentDir: content/en
    languageCode: en
    title: Welcome
    weight: 1

and in an index page I have the following code blurb.

    {{ $lang := .Lang }}
    {{ $test := printf "/home/" | printf "%s%s" .Lang | printf "%s%s" "/data/" }}
    {{ $test }}

On each of the two given addresses I obtain what is expected, like

localhost:1313 = en /data/en/home 
localhost:1314 = it /data/it/home

The goal is to have a dynamic path from where I load files into the site.
However, so long the path is “hardcoded”, everything works fine.

E.g.

{{ $path := "/data/en/home/" }}
    {{ range os.ReadDir $path }}
        {{ $file := printf "%s" .Name | printf "%s%s" $path }}
        {{ readFile $file | safeHTML }}
    {{ end }}

The problem arise as soon as replace the $path variable with $test (containing a dynamic path). At that point, even on the English version, Hugo throws an exception that could not locate the /data/it/home folder and I don’t explain where /it/ is read considering that {{ .Lang }} is not an array.

Can somebody shed a light?

Are you 100% sure those directories exist for every languages set in your project?

You are using a different domain for each language right? (it does not appear so in your languages config but your are mentioning two URLs for each language.

Hello @regis I have stripped the baseURL from the config above proposed, which is the one responsible for setting up the two different localhost in this case (as far as I can understand from the documentation).

This is goes in line with what I want since files in the public dir are written respectively in the /en and /it folders.

To your question, though, the path is a relative one formed on the spot, but in any case is not included in the contentDir rather in the data folder. I don’t want those .html files being published on their own, and that’s why I’m adding them there.

So my folder structure looks like

|_ content
|  |__ Empty so far
|_ data
  |_en
  | |_home
  |_it
    |_home

As I am trying to load from data/it/home or data/en/home from the respective hostname, I though this could work. And in fact, if I do use and hardcoded path, this work. It’s just when I’m trying to use the dynamically formed path that I have troubles.

Any ideas?

I am unable to reproduce the behavior that you describe.

Please post a link to the public repository for your project.

See https://discourse.gohugo.io/t/requesting-help/9132.

Let us see your code

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

But in fact I did not create a public repository. The website is “minimal” that it might be easier a copy/paste of the attached files?

index.html in layouts

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
</head>

<body>
    {{ $lang := .Lang }}
    {{ $test := printf "/home/" | printf "%s%s" .Lang | printf "%s%s" "/data/" }}
    {{ $test }}


    {{ $path := "/data/en/home/" }}
    {{ range os.ReadDir $test }}
        {{ $file := printf "%s" .Name | printf "%s%s" $path }}
        {{ readFile $file | safeHTML }}
    {{ end }}

    {{ range .Translations }}
        {{ . }}
    {{ end}}
</body>

</html>

config.yaml in the root

baseURL: 'http://example.org/'
languageCode: 'en-gb'
theme: 'minimalist-main'

defaultContentLanguage: en
languages:
  it:
    baseURL: https://www.dummysite.eu
    contentDir: content/it
    languageCode: it
    title: Benvenuto
    weight: 2
  en:
    baseURL: https://www.dummysite.co.uk
    contentDir: content/en
    languageCode: en
    title: Welcome
    weight: 1

box.html saved in data/en/home and data/it/home

<o>I'm the dummy content to be imported</p>

Ok, forget about this … I mispelled the folder name typing in hone instead of home.

But my question remains as in, why the EN site was trying to read the IT folder?

I think it’d be better to open a new thread for that question as any help would need to browse through the complex history of this one.