Skip to content

Commit 55eab59

Browse files
authored
Merge pull request #31 from nkdAgility/update-language-detector
Refactor translation retrieval logic for improved clarity and efficiency
2 parents 1758329 + 34c4736 commit 55eab59

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

module/layouts/_partials/functions/get-guide-translations-for-version.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,12 @@
8888
{{ $hasPage := false }}
8989
{{ if $page }}
9090
{{ $hasPage = true }}
91-
{{/* Check if page has actual content by checking plain text content and word count */}}
91+
{{/* Check if page has actual body content (not just frontmatter) */}}
92+
{{/* Plain contains the rendered content body without frontmatter */}}
93+
{{/* Count words in Plain to ensure actual content exists, not just translated frontmatter */}}
9294
{{ $plainContent := trim $page.Plain " \n\r\t" }}
93-
{{ $hasContent = and (gt $page.WordCount 0) (ne $plainContent "") }}
95+
{{ $plainWords := split $plainContent " " | len }}
96+
{{ $hasContent = gt $plainWords 10 }}
9497
{{ end }}
9598

9699
{{/* Check if PDF is available and get PDF path */}}

module/layouts/_partials/functions/get-guide-translations-list.html

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
{{/* Get all versions for this guide */}}
88
{{ $guidePages := where .Pages "Type" "guide" }}
99

10-
{{/* Map to track latest version for each language */}}
11-
{{ $languageMap := dict }}
12-
13-
{{/* Process all versions to find the latest for each language */}}
10+
{{/* Create a slice with version info for sorting */}}
11+
{{ $versionsWithInfo := slice }}
1412
{{ range $guidePages }}
1513
{{/* Extract version from URL path */}}
1614
{{ $pathParts := split .RelPermalink "/" }}
@@ -21,52 +19,53 @@
2119
{{ break }}
2220
{{ end }}
2321
{{ end }}
24-
22+
2523
{{ if $version }}
26-
{{/* Get available translations for this version using the dedicated function */}}
27-
{{ $versionTranslations := partial "functions/get-guide-translations-for-version" . }}
28-
29-
{{/* Process each translation to find the latest usable version for each language */}}
30-
{{ range $versionTranslations }}
31-
{{ $langCode := .Language }}
24+
{{/* Parse version for sorting (YYYY.M format) */}}
25+
{{ $versionParts := split $version "." }}
26+
{{ $year := int (index $versionParts 0) }}
27+
{{ $month := int (index $versionParts 1) }}
28+
{{ $sortKey := add (mul $year 100) $month }}
29+
{{ $versionsWithInfo = $versionsWithInfo | append (dict "page" . "version" $version "sortKey" $sortKey) }}
30+
{{ end }}
31+
{{ end }}
3232

33-
{{/* Check if this translation is usable (has content or PDF) */}}
34-
{{ $isUsable := or (eq .ReadOnline true) (eq .ReadPDF true) }}
33+
{{/* Sort versions in DESCENDING order (newest first) */}}
34+
{{ $versionsWithInfo = sort $versionsWithInfo "sortKey" "desc" }}
3535

36-
{{/* Only process usable translations */}}
37-
{{ if $isUsable }}
38-
{{/* Check if we already have this language or if this version is newer */}}
39-
{{ $existing := index $languageMap $langCode }}
40-
{{ if not $existing }}
41-
{{/* First time seeing this language, add it with version info */}}
42-
{{ $translationWithVersion := merge . (dict "Version" $version) }}
43-
{{ $languageMap = merge $languageMap (dict $langCode $translationWithVersion) }}
44-
{{ else }}
45-
{{/* Compare versions to see if this one is newer */}}
46-
{{ $currentVersion := $version }}
47-
{{ $existingVersion := $existing.Version }}
36+
{{/* Map to track best version for each language */}}
37+
{{ $languageMap := dict }}
4838

49-
{{/* Parse versions for comparison (YYYY.M format) */}}
50-
{{ $currentParts := split $currentVersion "." }}
51-
{{ $existingParts := split $existingVersion "." }}
39+
{{/* Process all versions (newest to oldest) to find best available for each language */}}
40+
{{ range $versionsWithInfo }}
41+
{{ $guidePage := .page }}
42+
{{ $version := .version }}
43+
44+
{{/* Get available translations for this version using the dedicated function */}}
45+
{{ $versionTranslations := partial "functions/get-guide-translations-for-version" $guidePage }}
5246

53-
{{ $currentYear := int (index $currentParts 0) }}
54-
{{ $currentMonth := int (index $currentParts 1) }}
55-
{{ $existingYear := int (index $existingParts 0) }}
56-
{{ $existingMonth := int (index $existingParts 1) }}
47+
{{/* Process each translation to find the best version for each language */}}
48+
{{ range $versionTranslations }}
49+
{{ $langCode := .Language }}
5750

58-
{{ $isNewer := false }}
59-
{{ if gt $currentYear $existingYear }}
60-
{{ $isNewer = true }}
61-
{{ else if and (eq $currentYear $existingYear) (gt $currentMonth $existingMonth) }}
62-
{{ $isNewer = true }}
63-
{{ end }}
51+
{{/* Check if this translation is usable (has content or PDF) */}}
52+
{{ $isUsable := or (eq .ReadOnline true) (eq .ReadPDF true) }}
6453

65-
{{ if $isNewer }}
66-
{{/* Update with newer version only if it's usable */}}
67-
{{ $updatedTranslation := merge . (dict "Version" $currentVersion) }}
68-
{{ $languageMap = merge $languageMap (dict $langCode $updatedTranslation) }}
69-
{{ end }}
54+
{{/* Only process usable translations */}}
55+
{{ if $isUsable }}
56+
{{/* Check if we already have this language */}}
57+
{{ $existing := index $languageMap $langCode }}
58+
{{ if not $existing }}
59+
{{/* First time seeing this language, add it */}}
60+
{{ $translationWithVersion := merge . (dict "Version" $version) }}
61+
{{ $languageMap = merge $languageMap (dict $langCode $translationWithVersion) }}
62+
{{ else }}
63+
{{/* We already have this language from a newer version */}}
64+
{{/* Only replace if current (older) version has ReadOnline content and existing doesn't */}}
65+
{{ if and (eq .ReadOnline true) (ne $existing.ReadOnline true) }}
66+
{{/* This older version has actual content, while newer version doesn't - use this one */}}
67+
{{ $updatedTranslation := merge . (dict "Version" $version) }}
68+
{{ $languageMap = merge $languageMap (dict $langCode $updatedTranslation) }}
7069
{{ end }}
7170
{{ end }}
7271
{{ end }}

0 commit comments

Comments
 (0)