From e3452e7920b0742cb375930e85354a31fabe89c7 Mon Sep 17 00:00:00 2001 From: razonyang Date: Mon, 27 Nov 2023 01:04:23 +0800 Subject: [PATCH] feat: fill i18n fields if enabled --- layouts/_default/index.decapcmsconfig.yaml | 12 ++++++++++++ layouts/partials/decap-cms/functions/locale.html | 10 ++-------- .../partials/decap-cms/functions/locales-map.html | 7 +++++++ layouts/partials/decap-cms/functions/locales.html | 10 ++++++++++ 4 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 layouts/partials/decap-cms/functions/locales-map.html create mode 100644 layouts/partials/decap-cms/functions/locales.html diff --git a/layouts/_default/index.decapcmsconfig.yaml b/layouts/_default/index.decapcmsconfig.yaml index 514436f..f56c8a0 100644 --- a/layouts/_default/index.decapcmsconfig.yaml +++ b/layouts/_default/index.decapcmsconfig.yaml @@ -37,4 +37,16 @@ {{- $config.Set "logo_url" . }} {{- end }} {{- end }} +{{/* Fill i18n fields if enabled. */}} +{{- with $config.Get "i18n" }} + {{- $i18n := . }} + {{- $locales := partialCached "decap-cms/functions/locales" . }} + {{- if not .locales }} + {{- $i18n = merge $i18n (dict "locales" $locales) }} + {{- end }} + {{- if not .default_locale }} + {{- $i18n = merge $i18n (dict "default_locale" (index $locales 0)) }} + {{- end }} + {{- $config.Set "i18n" $i18n }} +{{- end }} {{- transform.Remarshal "yaml" $config.Values | safeHTML -}} diff --git a/layouts/partials/decap-cms/functions/locale.html b/layouts/partials/decap-cms/functions/locale.html index 58dda6c..75ee95e 100644 --- a/layouts/partials/decap-cms/functions/locale.html +++ b/layouts/partials/decap-cms/functions/locale.html @@ -1,12 +1,6 @@ -{{- $locales := dict - "zh-cn" "zh_Hans" - "zh-hans" "zh_Hans" - "zh-hant" "zh_Hant" - "zh-hk" "zh_Hant" - "zh-tw" "zh_Hant" -}} +{{- $map := partialCached "decap-cms/functions/locales-map" . }} {{- $locale := site.Language.LanguageCode }} -{{- with index $locales $locale }} +{{- with index $map $locale }} {{- $locale = . }} {{- end }} {{- return $locale }} diff --git a/layouts/partials/decap-cms/functions/locales-map.html b/layouts/partials/decap-cms/functions/locales-map.html new file mode 100644 index 0000000..79dfc24 --- /dev/null +++ b/layouts/partials/decap-cms/functions/locales-map.html @@ -0,0 +1,7 @@ +{{- return dict + "zh-cn" "zh_Hans" + "zh-hans" "zh_Hans" + "zh-hant" "zh_Hant" + "zh-hk" "zh_Hant" + "zh-tw" "zh_Hant" +}} diff --git a/layouts/partials/decap-cms/functions/locales.html b/layouts/partials/decap-cms/functions/locales.html new file mode 100644 index 0000000..85ef375 --- /dev/null +++ b/layouts/partials/decap-cms/functions/locales.html @@ -0,0 +1,10 @@ +{{- $map := partialCached "decap-cms/functions/locales-map" . }} +{{- $locales := slice }} +{{- range site.Languages }} + {{- with index $map .LanguageCode }} + {{- $locales = $locales | append . }} + {{- else }} + {{- $locales = $locales | append .LanguageCode }} + {{- end }} +{{- end }} +{{- return $locales }}