Imagine that I have all the content translated to chinese but the chinese folder isn’t under the data folder. In this case I want to load the english version.
Thanks for the reply @Jura but maybe I don’t expressed correctly.
My idea is if the translation doesn’t exists in the selected language, the English translation is loaded.
Take this example:
I have this directory structure:
data
en_US
countries.json
days.json
es_ES
days.json
If I access the website with the English version everything goes well. The countries and days are loaded.
But if I access the website with the Spanish version, only the days are loaded.
What I want is in the Spanish case the website loads the countries from English and the days from Spanish.
This is a example code of loading data based on the language, but it’s incomplete because if the file doesn’t exist in the language nothing it’s loaded.
{{ $data := index .Site.Data .Site.Language.Lang }}
{{ range $data.countries }}
....
{{ end }}
{{ range $data.days }}
....
{{ end }}
{{ end }}
Use .Sites.First to render the country list from the English language version (just make sure that it is the first defined language) and then use what you have above to render the days.
{{ $data := index .Sites.First.Data .Site.Language.Lang }}
....
{{ end }}
I tried and it doesn’t works. The Spanish site it’s empty
I also forgot to say that the data it’s going to be a “variable” folder. Maybe the countries.json file will be or not… What I want to do is something like that:
if (fileExist) {
loadFileData;
} else {
loadEnglishData;
}
.Sites.First.Data will have no effect since the Data folder is language agnostic. There is after all only one data folder per project.
Instead try: {{ $data := index .Site.Data .Sites.First.Language.Lang }}
Not sure what you mean by variable folder. Hugo is a static site generator. It requires a specific folder structure particularly in a multilingual setting.
Maybe it would be best if you could provide a sample repo to illustrate your use case.
Sorry, my fault. This’s what I want. I’m going to explain all the problem again:
Imagine that I have this folder structure:
data
en_US
countries.json
days.json
es_ES
days.json
Well, right now, in the time of writing this post. The es_ES folder only have the days.json file. But maybe tomorrow, the next week or the next month someone translate the countries.json file to Spanish. And the folder structure changes to this:
data
en_US
countries.json
days.json
es_ES
countries.json
days.json
So, what I want is hugo detect (I don’t know how) that the file doesn’t exists and load the English version. But if the file exists hugo should load the localized version.
In the C programming language it’s something like…:
if (fileExist) {
loadFile();
} else {
loadEnglishVersion();
}
With this code hugo always load the English version:
{{ $data := index .Site.Data .Sites.First.Language.Lang }}