Fix set -e compatibility in build-docs-l10n.sh

Use a global variable (MERGE_HAD_FALLBACK) instead of return codes
for merge_ftl, and use if/fi instead of [ ] && cmd in restore_en.
Both patterns caused silent script abortion under set -euo pipefail
when returning non-zero exit codes.
This commit is contained in:
Sylvestre Ledru
2026-03-22 18:14:15 +01:00
parent 59e691e8e1
commit 50a4a9de74
+25 -19
View File
@@ -65,17 +65,19 @@ for lang in "${!LANG_MAP[@]}"; do
done
# Merge translated FTL with English: translated keys take priority,
# English keys fill in any gaps (handles empty files, partial translations)
# Returns 0 if fully translated, 1 if English fallback was used
# English keys fill in any gaps (handles empty files, partial translations).
# Sets MERGE_HAD_FALLBACK=1 if any English fallback entries were added.
merge_ftl() {
local english="$1"
local translated="$2"
local output="$3"
MERGE_HAD_FALLBACK=0
# If translated file is empty or missing, keep English as-is
if [ ! -s "$translated" ]; then
cp "$english" "$output"
return 1
MERGE_HAD_FALLBACK=1
return 0
fi
# Extract top-level message IDs from the translated file
@@ -87,7 +89,6 @@ merge_ftl() {
cp "$translated" "$output"
# Append English entries whose keys are NOT in the translated file
local had_fallback=0
local current_key=""
local entry_lines=""
while IFS= read -r line || [ -n "$line" ]; do
@@ -95,10 +96,9 @@ merge_ftl() {
# Flush previous entry if it was missing from translation
if [ -n "$current_key" ] && ! echo "$translated_keys" | grep -qxF "$current_key"; then
printf '%s\n' "$entry_lines" >> "$output"
had_fallback=1
MERGE_HAD_FALLBACK=1
fi
current_key="${line%%[[:space:]]*=*}"
# Trim: extract just the identifier
# Extract just the identifier
current_key=$(echo "$line" | grep -oP '^[a-zA-Z][a-zA-Z0-9_-]*')
entry_lines="$line"
elif [[ "$line" =~ ^[[:space:]] ]] && [ -n "$current_key" ]; then
@@ -108,7 +108,7 @@ merge_ftl() {
# Blank line or comment — flush previous entry if needed
if [ -n "$current_key" ] && ! echo "$translated_keys" | grep -qxF "$current_key"; then
printf '%s\n' "$entry_lines" >> "$output"
had_fallback=1
MERGE_HAD_FALLBACK=1
fi
current_key=""
entry_lines=""
@@ -117,9 +117,8 @@ merge_ftl() {
# Flush last entry
if [ -n "$current_key" ] && ! echo "$translated_keys" | grep -qxF "$current_key"; then
printf '%s\n' "$entry_lines" >> "$output"
had_fallback=1
MERGE_HAD_FALLBACK=1
fi
return $had_fallback
}
restore_en() {
@@ -127,12 +126,17 @@ restore_en() {
for util_dir in "$COREUTILS_DIR"/src/uu/*/locales/; do
util=$(basename "$(dirname "$util_dir")")
backup="$TMPDIR/src-backup/uu/$util/locales/en-US.ftl"
[ -f "$backup" ] && cp "$backup" "${util_dir}en-US.ftl"
if [ -f "$backup" ]; then
cp "$backup" "${util_dir}en-US.ftl"
fi
done
local uucore_backup="$TMPDIR/src-backup/uucore/locales/en-US.ftl"
[ -f "$uucore_backup" ] && cp "$uucore_backup" "$COREUTILS_DIR/src/uucore/locales/en-US.ftl"
if [ -f "$TMPDIR/src-backup/uucore/locales/en-US.ftl" ]; then
cp "$TMPDIR/src-backup/uucore/locales/en-US.ftl" "$COREUTILS_DIR/src/uucore/locales/en-US.ftl"
fi
# Restore English tldr
[ -f "$EN_TLDR" ] && cp "$EN_TLDR" "$COREUTILS_DIR/docs/tldr.zip"
if [ -f "$EN_TLDR" ]; then
cp "$EN_TLDR" "$COREUTILS_DIR/docs/tldr.zip"
fi
}
cd "$COREUTILS_DIR"
@@ -154,7 +158,8 @@ for lang in "${!LANG_MAP[@]}"; do
if [ -f "${util_dir}${ftl_name}.ftl" ]; then
util=$(basename "$(dirname "$util_dir")")
en_backup="$TMPDIR/src-backup/uu/$util/locales/en-US.ftl"
if ! merge_ftl "$en_backup" "${util_dir}${ftl_name}.ftl" "${util_dir}en-US.ftl"; then
merge_ftl "$en_backup" "${util_dir}${ftl_name}.ftl" "${util_dir}en-US.ftl"
if [ "$MERGE_HAD_FALLBACK" = "1" ]; then
fallback_utils[$util]=1
fi
fi
@@ -176,10 +181,11 @@ for lang in "${!LANG_MAP[@]}"; do
weblate_lang="${ftl_name//-/_}"
for util in "${!fallback_utils[@]}"; do
md_file="docs/src/utils/${util}.md"
[ -f "$md_file" ] || continue
notice="<div class=\"warning\">Some strings on this page have not been translated yet. You can help by <a href=\"https://hosted.weblate.org/projects/rust-coreutils/${util}/${weblate_lang}/\">translating them on Weblate</a>.</div>"
# Insert notice after the first line (# utility-name)
sed -i "1a\\${notice}" "$md_file"
if [ -f "$md_file" ]; then
notice="<div class=\"warning\">Some strings on this page have not been translated yet. You can help by <a href=\"https://hosted.weblate.org/projects/rust-coreutils/${util}/${weblate_lang}/\">translating them on Weblate</a>.</div>"
# Insert notice after the first line (# utility-name)
sed -i "1a\\${notice}" "$md_file"
fi
done
if [ ${#fallback_utils[@]} -gt 0 ]; then
echo " Added translation notice to ${#fallback_utils[@]} utilities with untranslated strings"