Skip to content

Commit

Permalink
[i18n] Ensure fallback pages have en lang (#4683)
Browse files Browse the repository at this point in the history
  • Loading branch information
chalin authored Jun 14, 2024
1 parent 61a26ba commit 3b44fbf
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
35 changes: 35 additions & 0 deletions layouts/docs/baseof.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{{/* Docsy override */ -}}
{{ $lang := partial "i18n/lang.html" . -}}

<!doctype html>
<html itemscope itemtype="http://schema.org/WebPage" lang="{{ $lang }}" class="no-js">
<head>
{{ partial "head.html" . }}
</head>
<body class="td-{{ .Kind }}{{ with .Page.Params.body_class }} {{ . }}{{ end }}">
<header>
{{ partial "navbar.html" . }}
</header>
<div class="container-fluid td-outer">
<div class="td-main">
<div class="row flex-xl-nowrap">
<aside class="col-12 col-md-3 col-xl-2 td-sidebar d-print-none">
{{ partial "sidebar.html" . }}
</aside>
<aside class="d-none d-xl-block col-xl-2 td-sidebar-toc d-print-none">
{{ partial "page-meta-links.html" . }}
{{ partial "toc.html" . }}
{{ partial "taxonomy_terms_clouds.html" . }}
</aside>
<main class="col-12 col-md-9 col-xl-8 ps-md-5" role="main">
{{ partial "version-banner.html" . }}
{{ if not .Site.Params.ui.breadcrumb_disable }}{{ partial "breadcrumb.html" . }}{{ end }}
{{ block "main" . }}{{ end }}
</main>
</div>
</div>
{{ partial "footer.html" . }}
</div>
{{ partial "scripts.html" . }}
</body>
</html>
25 changes: 25 additions & 0 deletions layouts/partials/i18n/fallback-page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{{/*

Returns the fallback page of this page, if it has one.
A fallback page, is a page in the default site language
that has been mounted under this locale as a fallback
when this locale is missing a translation.

*/ -}}

{{ $result := false -}}

{{ if and .Site.IsMultiLingual .File -}}
{{ $defaultLang := .Site.Sites.Default.Language.Lang -}}

{{ $resultsArray := where .Translations "Lang" $defaultLang -}}
{{ $defaultLangPage := index $resultsArray 0 -}}
{{ if and $defaultLangPage
$defaultLangPage.File
(eq .File.Filename $defaultLangPage.File.Filename)
-}}
{{ $result = $defaultLangPage -}}
{{ end -}}
{{ end -}}

{{ return $result -}}
6 changes: 6 additions & 0 deletions layouts/partials/i18n/lang.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{ $fallbackPage := partial "i18n/fallback-page.html" . -}}
{{ $lang := .Site.Language.Lang -}}
{{ with $fallbackPage -}}
{{ $lang = .Language.Lang -}}
{{ end -}}
{{ return $lang -}}
12 changes: 8 additions & 4 deletions scripts/check-i18n.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# Inspired by https://github.com/kubernetes/website/blob/main/scripts/lsync.sh

COMMIT_HASH_ARG=""
DEFAULT_CONTENT="content"
DEFAULT_LANG="en"
DEFAULT_TARGET="content"
DEFAULT_TARGET="$DEFAULT_CONTENT"
EXIT_STATUS=0
EXTRA_DIFF_ARGS="--numstat"
FLAG_DIFF_DETAILS=""
Expand Down Expand Up @@ -33,6 +34,8 @@ Usage: $(basename "$0") [options] [TARGET_PATH ...]
a directory of localized pages, such as 'content/ja'. The default TARGET_PATH
is '$DEFAULT_TARGET'.
Options:
-a List/process all localization page files accessible through target paths.
-c HASH Update or add the '$I18N_DLC_KEY' key value to HASH for all selected
Expand Down Expand Up @@ -176,12 +179,14 @@ function set_file_i18n_hash() {
local f="$1"
local HASH="$2"
local pre_msg="${3:--\t-}"
local post_msg="${4:-key set}"
local post_msg="${4:-key}"

if grep -q "^$I18N_DLC_KEY:" "$f"; then
perl -i -pe "s/(^$I18N_DLC_KEY):.*$/\$1: $HASH/" "$f"
post_msg="$post_msg UPDATED"
else
perl -i -0777 -pe "s/^(---.*?)(\n---\n)/\$1\n$I18N_DLC_KEY: $HASH\$2/sm" "$f"
post_msg="$post_msg ADDED"
fi
if [[ -z $FLAG_QUIET ]]; then
echo -e "$pre_msg\t$f $HASH $post_msg"
Expand Down Expand Up @@ -244,7 +249,6 @@ function main() {

for f in $TARGETS; do
((FILE_COUNT++))
local LIST=0

LASTCOMMIT_FF=$(perl -ne "print \"\$1\" if /^$I18N_DLC_KEY:\\s*(.*)/" "$f")
LASTCOMMIT="$LASTCOMMIT_FF"
Expand All @@ -269,7 +273,7 @@ function main() {
## Processing $LIST_KIND DRIFTED

# Does $f have an default-language version?
EN_VERSION=$(echo "$f" | sed "s/content\/.\{2,5\}\//content\/en\//g")
EN_VERSION=$(echo "$f" | sed "s/$DEFAULT_CONTENT\/.\{2,5\}\//$DEFAULT_CONTENT\/$DEFAULT_LANG\//g")
if [[ ! -e "$EN_VERSION" ]]; then
((FILE_PROCESSED_COUNT++))
echo -e "File not found\t$EN_VERSION - $f - $DEFAULT_LANG was removed or renamed"
Expand Down

0 comments on commit 3b44fbf

Please sign in to comment.