Skip to content

Commit 0f38d27

Browse files
committed
Fall back to English tldr examples when translation is missing
Merge translated tldr zips with the English one so that utilities without translated examples (e.g., cut in French) still show the English examples instead of having no Examples section at all. Add a notice on affected pages linking to the tldr-pages project for contributors to help translate the examples.
1 parent 50a4a9d commit 0f38d27

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

scripts/build-docs-l10n.sh

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ EN_TLDR="$TMPDIR/tldr-en.zip"
5050
cp "$COREUTILS_DIR/docs/tldr.zip" "$EN_TLDR" 2>/dev/null || true
5151

5252
# Download and repack translated tldr archives (uudoc expects pages/ prefix)
53+
# Merge with English so missing examples fall back to English
5354
echo "Downloading translated tldr archives..."
55+
EN_TLDR_DIR="$TMPDIR/tldr-en-dir"
56+
if [ -f "$EN_TLDR" ]; then
57+
mkdir -p "$EN_TLDR_DIR"
58+
(cd "$EN_TLDR_DIR" && unzip -o "$EN_TLDR" > /dev/null 2>&1)
59+
fi
5460
for lang in "${!LANG_MAP[@]}"; do
5561
raw="$TMPDIR/tldr-raw-${lang}.zip"
5662
curl -sfL "https://github.com/tldr-pages/tldr/releases/download/v2.3/tldr-pages.${lang}.zip" \
@@ -59,7 +65,21 @@ for lang in "${!LANG_MAP[@]}"; do
5965
repack_dir="$TMPDIR/tldr-repack-${lang}"
6066
mkdir -p "$repack_dir/pages"
6167
(cd "$repack_dir" && unzip -o "$raw" -d pages/ > /dev/null 2>&1)
62-
(cd "$repack_dir" && zip -r "$TMPDIR/tldr-${lang}.zip" pages/ > /dev/null 2>&1)
68+
69+
# Record which utilities have translated examples (before merging with English)
70+
translated_list="$TMPDIR/tldr-translated-${lang}.list"
71+
(cd "$repack_dir" && find pages -name "*.md" -printf '%f\n' | sed 's/\.md$//' | sort -u > "$translated_list")
72+
73+
# Merge: start with English, overlay translated on top
74+
if [ -d "$EN_TLDR_DIR" ]; then
75+
merge_dir="$TMPDIR/tldr-merge-${lang}"
76+
cp -r "$EN_TLDR_DIR" "$merge_dir"
77+
cp -r "$repack_dir/pages"/* "$merge_dir/pages/" 2>/dev/null || true
78+
(cd "$merge_dir" && zip -r "$TMPDIR/tldr-${lang}.zip" pages/ > /dev/null 2>&1)
79+
rm -rf "$merge_dir"
80+
else
81+
(cd "$repack_dir" && zip -r "$TMPDIR/tldr-${lang}.zip" pages/ > /dev/null 2>&1)
82+
fi
6383
rm -rf "$repack_dir" "$raw"
6484
fi
6585
done
@@ -191,6 +211,26 @@ for lang in "${!LANG_MAP[@]}"; do
191211
echo " Added translation notice to ${#fallback_utils[@]} utilities with untranslated strings"
192212
fi
193213

214+
# Inject notice into Examples section for utilities whose examples fell back to English
215+
translated_list="$TMPDIR/tldr-translated-${lang}.list"
216+
if [ -f "$translated_list" ]; then
217+
examples_fallback=0
218+
for md_file in docs/src/utils/*.md; do
219+
[ -f "$md_file" ] || continue
220+
util=$(basename "$md_file" .md)
221+
# Only process files that have an Examples section
222+
if grep -q "^## Examples" "$md_file" && ! grep -qxF "$util" "$translated_list"; then
223+
# This utility's examples came from English fallback
224+
sed -i '/^## Examples$/a\
225+
<div class="warning">The examples have not been translated yet and are shown in English. You can help by <a href="https://github.com/tldr-pages/tldr">translating them on tldr-pages</a>.</div>' "$md_file"
226+
examples_fallback=$((examples_fallback + 1))
227+
fi
228+
done
229+
if [ "$examples_fallback" -gt 0 ]; then
230+
echo " Added example translation notice to $examples_fallback utilities"
231+
fi
232+
fi
233+
194234
# Build mdbook to a language-specific output directory
195235
sed -i '/^multilingual/d' docs/book.toml
196236
(cd docs && mdbook build -d "book-${lang}")

0 commit comments

Comments
 (0)