Skip to content

Commit 2e9cbbb

Browse files
committed
Add fallback to default language for code_sample shortcode
This imporves internationalization support by falling back to the default language's examples when a translation doesn't exist, prevention build errors for missing localized code samples. - Refactor code_sample.html to handle missing files more gracefully - Move filename generation to scratch variable for reusability - Add fallback logic to try default language when file not found in current language - Reorganize source lookup flow with proper conditional blocks - Maintain existing ghlink generation with updated filename variable
1 parent fa93cd8 commit 2e9cbbb

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

layouts/shortcodes/code_sample.html

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@
33
{{ $codelang := .Get "language" | default (path.Ext $file | strings.TrimPrefix ".") }}
44
{{ $fileDir := path.Split $file }}
55
{{ $bundlePath := path.Join .Page.File.Dir $fileDir.Dir }}
6-
{{ $filename := printf "/content/%s/examples/%s" .Page.Lang $file | safeURL }}
7-
{{ $ghlink := printf "https://%s/%s%s" site.Params.githubwebsiteraw (default "main" site.Params.docsbranch) $filename | safeURL }}
6+
{{ $.Scratch.Set "filename" (printf "/content/%s/examples/%s" .Page.Lang $file) }}
87
{{/* First assume this is a bundle and the file is inside it. */}}
9-
{{ $resource := $p.Resources.GetMatch (printf "%s*" $file ) }}
10-
{{ with $resource }}
8+
{{ with $p.Resources.GetMatch (printf "%s*" $file) }}
119
{{ $.Scratch.Set "content" .Content }}
12-
{{ else }}
10+
{{ end }}
1311
{{/* Read the file relative to the content root. */}}
14-
{{ $resource := readFile $filename}}
15-
{{ with $resource }}{{ $.Scratch.Set "content" . }}{{ end }}
12+
{{ with readFile ($.Scratch.Get "filename")}}
13+
{{ $.Scratch.Set "content" . }}
14+
{{ end }}
15+
{{/* If not found, try the default language */}}
16+
{{ $defaultLang := (index (sort site.Languages "Weight") 0).Lang }}
17+
{{ with readFile (printf "/content/%s/examples/%s" $defaultLang $file) }}
18+
{{ $.Scratch.Set "content" . }}
19+
{{ $.Scratch.Set "filename" (printf "/content/%s/examples/%s" $defaultLang $file) }}
1620
{{ end }}
1721
{{ if not ($.Scratch.Get "content") }}
1822
{{ errorf "[%s] %q not found in %q" site.Language.Lang $fileDir.File $bundlePath }}
1923
{{ end }}
24+
{{ $filename := printf ($.Scratch.Get "filename") | safeURL }}
25+
{{ $ghlink := printf "https://%s/%s%s" site.Params.githubwebsiteraw (default "main" site.Params.docsbranch) $filename | safeURL }}
2026
{{ with $.Scratch.Get "content" }}
2127
<div class="highlight code-sample">
2228
<div class="copy-code-icon">

0 commit comments

Comments
 (0)