From 4d38c40f9fbdf495dbf490dd2cdafccd983dfbe1 Mon Sep 17 00:00:00 2001 From: George Cushen Date: Tue, 3 Mar 2026 00:29:51 +0000 Subject: [PATCH] feat(i18n): add multilingual support for author data files Hugo doesn't natively merge language-specific data directories with the default data. Add a `get_authors_data` partial that manually looks up `data//authors/` and deep-merges translated fields on top of the default data/authors/ profiles. Centralizes all `site.Data.authors` access through this new partial so author names, bios, roles, and other fields can be translated per language while inheriting non-translated fields from the default. --- modules/blox/blox/contact-info/block.html | 3 +- modules/blox/blox/team-showcase/block.html | 5 +- .../functions/get_author_profile.html | 3 +- .../_partials/functions/get_authors_data.html | 54 +++++++++++++++++++ modules/blox/layouts/_partials/site_head.html | 5 +- 5 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 modules/blox/layouts/_partials/functions/get_authors_data.html diff --git a/modules/blox/blox/contact-info/block.html b/modules/blox/blox/contact-info/block.html index 7c9af796..11fbe94c 100644 --- a/modules/blox/blox/contact-info/block.html +++ b/modules/blox/blox/contact-info/block.html @@ -116,7 +116,8 @@ {{ $author = strings.TrimSpace $author_raw }} {{ end }} {{ $profile := partial "functions/get_author_profile" $author }} -{{ $author_data := index site.Data.authors (urlize $author) }} +{{ $authors := partialCached "functions/get_authors_data" $page site.Language.Lang }} +{{ $author_data := index $authors (urlize $author) }} {{ $email := "" }} {{ $email_raw := index $content "email" }} {{ if eq (printf "%T" $email_raw) "string" }} diff --git a/modules/blox/blox/team-showcase/block.html b/modules/blox/blox/team-showcase/block.html index 6b53e91f..c52a4377 100644 --- a/modules/blox/blox/team-showcase/block.html +++ b/modules/blox/blox/team-showcase/block.html @@ -90,9 +90,10 @@ {{/* Build profiles from data/authors */}} {{ $allProfiles := slice }} - {{ range $slug, $_ := site.Data.authors }} + {{ $authors := partialCached "functions/get_authors_data" $page site.Language.Lang }} + {{ range $slug, $_ := $authors }} {{ $profile := partial "functions/get_author_profile" (dict "slug" $slug) }} - {{ $profileData := index site.Data.authors $slug }} + {{ $profileData := index $authors $slug }} {{ $allProfiles = $allProfiles | append (dict "profile" $profile "profileData" $profileData) }} {{ end }} diff --git a/modules/blox/layouts/_partials/functions/get_author_profile.html b/modules/blox/layouts/_partials/functions/get_author_profile.html index 5314c269..20c9b8bf 100644 --- a/modules/blox/layouts/_partials/functions/get_author_profile.html +++ b/modules/blox/layouts/_partials/functions/get_author_profile.html @@ -38,7 +38,8 @@ Output (dict): {{- $slug = $slug | urlize -}} -{{- $data := index site.Data.authors $slug -}} +{{- $authors := partialCached "functions/get_authors_data" . site.Language.Lang -}} +{{- $data := index $authors $slug -}} {{- $hasData := $data -}} {{/* Structured name resolution */}} diff --git a/modules/blox/layouts/_partials/functions/get_authors_data.html b/modules/blox/layouts/_partials/functions/get_authors_data.html new file mode 100644 index 00000000..809790d8 --- /dev/null +++ b/modules/blox/layouts/_partials/functions/get_authors_data.html @@ -0,0 +1,54 @@ +{{/* + Return the authors data map for the current language. + + For multilingual sites, language-specific author data from data//authors/ + is merged on top of the default data/authors/ files. This allows translating + fields like name, bio, role, etc., while inheriting non-translated fields. + + Usage: + {{ $authors := partialCached "functions/get_authors_data" . site.Language.Lang }} + + Returns: map of slug → author data (same shape as site.Data.authors) +*/}} + +{{- $authors := site.Data.authors -}} +{{- if not $authors -}} + {{- $authors = dict -}} +{{- end -}} + +{{/* Check for language-specific author overrides */}} +{{- $lang := site.Language.Lang -}} +{{- $langData := index site.Data $lang -}} +{{- if $langData -}} + {{- $langAuthors := index $langData "authors" -}} + {{- if $langAuthors -}} + {{/* Merge each language-specific author data on top of the default */}} + {{- $merged := dict -}} + {{/* Start with all default authors */}} + {{- range $slug, $data := $authors -}} + {{- $langAuthor := index $langAuthors $slug -}} + {{- if $langAuthor -}} + {{/* Language data overrides default; merge gives $langAuthor priority */}} + {{- $mergedAuthor := merge $data $langAuthor -}} + {{/* Deep-merge the 'name' submap if both have it */}} + {{- $baseName := index $data "name" -}} + {{- $langName := index $langAuthor "name" -}} + {{- if and (reflect.IsMap $baseName) (reflect.IsMap $langName) -}} + {{- $mergedAuthor = merge $mergedAuthor (dict "name" (merge $baseName $langName)) -}} + {{- end -}} + {{- $merged = merge $merged (dict $slug $mergedAuthor) -}} + {{- else -}} + {{- $merged = merge $merged (dict $slug $data) -}} + {{- end -}} + {{- end -}} + {{/* Add any authors that only exist in the language data */}} + {{- range $slug, $data := $langAuthors -}} + {{- if not (index $authors $slug) -}} + {{- $merged = merge $merged (dict $slug $data) -}} + {{- end -}} + {{- end -}} + {{- $authors = $merged -}} + {{- end -}} +{{- end -}} + +{{- return $authors -}} diff --git a/modules/blox/layouts/_partials/site_head.html b/modules/blox/layouts/_partials/site_head.html index 3fd9431b..f37a559e 100644 --- a/modules/blox/layouts/_partials/site_head.html +++ b/modules/blox/layouts/_partials/site_head.html @@ -18,12 +18,13 @@ {{ $superuser_role := "" }} {{/* Prefer is_owner flag, fallback to slug 'me' */}} -{{ range $slug, $data := site.Data.authors }} +{{ $authors := partialCached "functions/get_authors_data" . site.Language.Lang }} +{{ range $slug, $data := $authors }} {{ if and (not $superuser_slug) ($data.is_owner) }} {{ $superuser_slug = $slug }} {{ end }} {{ end }} -{{ if and (eq $superuser_slug "") (index site.Data.authors "me") }} +{{ if and (eq $superuser_slug "") (index $authors "me") }} {{ $superuser_slug = "me" }} {{ end }}