Proper way to setup "og:locale" metadata

There’s a lot of ways to get the page language in Hugo. I’m a bit puzzled what way should I use to setup og:locale metadata?

For now i’m doing it like this:

<!-- Firstly I check the language code of the current page and format it to conform Open Graph protocol -->
<!-- If it's not presented I try to use just the language (without the "territory" part) -->
<!-- If it's not presented I fallback the language code of the entire site and format it to conform Open Graph protocol -->
<!-- If it's not presented I try to use just the language of entire site (without the "territory" part) -->
<!-- If none of this works I use {{ .Lang }}. I don't know what it is -->
<meta property="og:locale" content="{{ with .Site.Language.LanguageCode }}{{ . | replaceRE `-` `_` }}{{ else }}{{ with .Site.Language.Lang }}{{ . }}{{ else }}{{ with .Lang }}{{ . }}{{ end }}{{ end }}{{ end }}" />

It looks very ugly. Moreover there are a lot of ways to get language:

<meta property="og:locale1" content="{{ .Lang }}" />
<meta property="og:locale2" content="{{ .Site.Language.LanguageCode }}" />
<meta property="og:locale3" content="{{ .Site.Language.Lang }}" />
<meta property="og:locale4" content="{{ .Page.Lang }}" />
<meta property="og:locale5" content="{{ .Language }}" />
<meta property="og:locale6" content="{{ .Language.Lang }}" />
<meta property="og:locale7" content="{{ .Language.LanguageCode }}" />

How can setup the og:local in simple and reliable way?

(site.LanguageCode | default "en_US") should do the trick.

EDIT:

on second thought this might clash with multilanguage pages. this here should cover that:

(page.Language.LanguageCode | default (site.LanguageCode | default "en_US"))

Thank you for reply :+1:

Is it correct to say that {{ .Language.LanguageCode }} and {{ page.Language.LanguageCode }} are equal?

Some of your code parts are not about the site or page language, but the general language configuration available.

Personally for me, if it’s my theme and I have only one language, then site is enough. If you develop a theme for others to use, you should cover all cases.

The docs seem to be pretty clear (for me):

page.language

site.language

Keep an eye on the (expected) answer to this thread. I would expect that to be true, but in my experience the page method might have a different meaning. For instance on list pages it might mean the page itself, instead of the items on that page, if you list the 10 last posts for instance. In my understanding page is the “top page”.

The Site and Page methods return the same values…

Site.Language.Lang == Page.Language.Lang
Site.Language.LanguageCode == Page.Language.LanguageCode
Site.Language.LanguageDirection == Page.Language.LanguageDirection
Site.Language.LanguageName == Page.Language.LanguageName

The .Language.LanguageCode methods fallback to .Language.Lang if languageCode is not defined in the site configuration.

So… I’d just set og:locale to Site/Page.Language.LanguageCode and be done with it.

I have no idea if og really requires an underscore instead of a hyphen. If true, it’s unfortunate… RFC 5646 specifically requires a hyphen.

sadly yes, they seem to not have any RFC required though.

Don’t use any of the above. If they aren’t documented, don’t use them.

In a page context .Lang and .Page.Lang point to the same thing, but that may be deprecated at some point… that’s why is is not documented. Use .Language.Lang instead, either on Page or on Site.

The Language methods on Page and Site return a language object. Again, use .Language.Lang instead.

https://gohugo.io/methods/site/language/
https://gohugo.io/methods/page/language/

2 Likes