Hi everyone,
I’m looking for a way to display the name of a language from its ISO 639-1 code, for example “en”, “fr” or “es”. The language name must be translated into the current language of the site.
I used to do it in JavaScript:
const languageName = new Intl.DisplayNames("fr", { type: 'language' });
console.log(languageName.of("us")); //output États-Unis
Do you know a way of doing this in Hugo?
In Go it would seem that it is possible in this way How do I convert a language code to its proper name in Go? - Stack Overflow
Thank you in advance for your feedback
The idiomatic way to handle this is to provide the language name when you configure the language…
[languages.fr]
disabled = false
languageCode = 'fr-FR'
languageDirection = 'rtl'
languageName = 'Français'
title = 'Documentation technique'
weight = 1
…and retrieve it as needed with:
{{ .Site.Language.LanguageName }}
There is not a built-in template function, and creating a partial to do this seems like overkill given simplicity of the above.
Thanks Joe for your help.
I realise that my example was wrong, which might explain why I have the impression that your answer by using {{ .Site.Language.LanguageName }}
doesn’t seem to solve the problem. I’ll rephrase.
I want to get the name of a language from its ISO code, in the current language of the site. For example, on the English version of the website, for fr
I would like to obtain french
and for es
I would like to obtain spanish
.
I’m updating my JavaScript example,
// On the english version of the website
const languageName = new Intl.DisplayNames("en", { type: 'language' });
const frenchLanguageName = languageName.of("fr");
console.log(frenchLanguageName); // output "french"
From what I understand, the simplest thing to do at this stage would be to use a data model that allows you to make the association. For example:
{
"fr": {
"en": "anglais",
"es": "espagnol",
"fr": "français"
},
"en": {
"en": "english",
"es": "spanish",
"fr": "french"
},
"es": {
"en": "inglés",
"es": "español",
"fr": "francés"
}
}
I hope I was clearer, thank you!
Gotcha. So if I have a language switcher in the menu, for example, I could show the language names in the current language. Correct?
Yes, that sounds like a good example!
Your data file approach seems best.
Ok, thank you very much for your answers and your responsiveness 
That’s actually not a bad idea for a template function.
lang.LanguageName SRC [TARGET]
If TARGET is not provided, set to SRC.
{{ lang.LanguageName "pt-br" "en" }} --> Brazilian Portuguese
{{ lang.LanguageName "pt-br" "pt-pr" }} --> português
{{ lang.LanguageName "pt-br" }} --> português
And you could pass in {{ or site.Language.LanguageCode site.Language.Lang }}
.
I would love this function 
I would also find it very useful to be able to do the same thing for regions, as is the case in JavaScript with Intl.DisplayNames.
This could result in
{{ lang.RegionName "US" "en" }} --> United States
{{ lang.RegionName "CN" "en" }} --> China
If TARGET is not provided, it’s take the current language. For exemple in the french website
{{ lang.RegionName "CN" }} --> Chine
In Hugo, where would the region name come from? Would you extract it from Language.LanguageCode or Language.Lang? If yes, could the args be language tags (e.g., en-GB) just like the lang.LanguageName function?
In my case, the region name is a value (in ISO 3166 format) in a custom data model. It looks a bit like something like this:
{
"ID_1":{
"languages": ["en"],
"countries": ["FR", "CN"]
},
"ID_2":{
"languages": ["fr"],
"countries": ["US", "DE"]
},
"ID_3":{
"languages": ["en"],
"countries": ["FR"]
}
}
So I won’t need to extract it from Language.LanguageCode
or Language.Lang
.
Also I’m following this specifications to format my data.
I think I might be able to sell lang.LanguageName
because it’s a good fit with Hugo’s i18n architecture (i.e., the language tags already exist, even for a monolingual site):
https://github.com/gohugoio/hugo/issues/12212
The lang.Region
function is less appealing. It’s certainly useful for you, but you might be the only one, so it’s hard to justify development, testing, documentation, maintenance, etc.
If I’m on a French website as a German user and I see a language switcher offering me “Allemand”, I would have to know that means “Deutsch”. And that’s simple – imagine being on a Greek site having to choose from your language in a different script…
Maybe there’s a better example of why lang.LanguageName
would be useful?