Opengraph template

I was thinking it would be a great idea to add support for language and alternate languages in the opengraph template e.g.

{{ with .Site.Language.Lang }}
<meta property="og:locale" content="{{ . }}">
{{ end }}
{{ if .IsTranslated }}
{{ range .Translations }}
<meta property="og:locale:alternate" content="{{ .Language.Lang }}">
{{ end }}
{{ end }}

any comments ?
I am happy to test, and upload

1 Like

Are you talking about the internal template? If so, well, … open a PR and see what happens. I am personally not a fan of internal templates and use modified copies of them in my projects. (unpopular opinion) It can’t be Hugo’s task to supply layouts :slight_smile: lot of work goes into keeping these layouts up to date. It might be better to create a howto with your modifications. But that’s just an opinion :wink:

1 Like

I’ve dug deeper and it seems the reason that it hasnt already been done is because the output of the language code is broken. According to to github en-US becomes en-us. With my testing with the current hugo version, en-US becomes just en. This wont work with facebook

Your testing uses {{ .Language.Lang }} and that is defined as en in your [languages] section. You could add a parameter like slug to it with your preferred language code and then use {{ printf "%s" .Language.Slug }} and (maybe, just maybe) it should show the proper string.

The en-US to en-us thing is not an issue in my opinion, because the HTML standard is “ok” with lowercased language codes. Some parsers are unhappy and the validation tool complains (I think). But browsers and google (which is basically all we should care about) understand it.

by the way, the following won’t lowercase in your templates:

  <html lang="{{- site.LanguageCode | default "en-US" -}}" class="no-js">
1 Like