Skip to content

Commit 50a4a9d

Browse files
committed
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.
1 parent 59e691e commit 50a4a9d

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

scripts/build-docs-l10n.sh

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,19 @@ for lang in "${!LANG_MAP[@]}"; do
6565
done
6666

6767
# Merge translated FTL with English: translated keys take priority,
68-
# English keys fill in any gaps (handles empty files, partial translations)
69-
# Returns 0 if fully translated, 1 if English fallback was used
68+
# English keys fill in any gaps (handles empty files, partial translations).
69+
# Sets MERGE_HAD_FALLBACK=1 if any English fallback entries were added.
7070
merge_ftl() {
7171
local english="$1"
7272
local translated="$2"
7373
local output="$3"
74+
MERGE_HAD_FALLBACK=0
7475

7576
# If translated file is empty or missing, keep English as-is
7677
if [ ! -s "$translated" ]; then
7778
cp "$english" "$output"
78-
return 1
79+
MERGE_HAD_FALLBACK=1
80+
return 0
7981
fi
8082

8183
# Extract top-level message IDs from the translated file
@@ -87,18 +89,16 @@ merge_ftl() {
8789
cp "$translated" "$output"
8890

8991
# Append English entries whose keys are NOT in the translated file
90-
local had_fallback=0
9192
local current_key=""
9293
local entry_lines=""
9394
while IFS= read -r line || [ -n "$line" ]; do
9495
if [[ "$line" =~ ^[a-zA-Z][a-zA-Z0-9_-]*[[:space:]]*= ]]; then
9596
# Flush previous entry if it was missing from translation
9697
if [ -n "$current_key" ] && ! echo "$translated_keys" | grep -qxF "$current_key"; then
9798
printf '%s\n' "$entry_lines" >> "$output"
98-
had_fallback=1
99+
MERGE_HAD_FALLBACK=1
99100
fi
100-
current_key="${line%%[[:space:]]*=*}"
101-
# Trim: extract just the identifier
101+
# Extract just the identifier
102102
current_key=$(echo "$line" | grep -oP '^[a-zA-Z][a-zA-Z0-9_-]*')
103103
entry_lines="$line"
104104
elif [[ "$line" =~ ^[[:space:]] ]] && [ -n "$current_key" ]; then
@@ -108,7 +108,7 @@ merge_ftl() {
108108
# Blank line or comment — flush previous entry if needed
109109
if [ -n "$current_key" ] && ! echo "$translated_keys" | grep -qxF "$current_key"; then
110110
printf '%s\n' "$entry_lines" >> "$output"
111-
had_fallback=1
111+
MERGE_HAD_FALLBACK=1
112112
fi
113113
current_key=""
114114
entry_lines=""
@@ -117,22 +117,26 @@ merge_ftl() {
117117
# Flush last entry
118118
if [ -n "$current_key" ] && ! echo "$translated_keys" | grep -qxF "$current_key"; then
119119
printf '%s\n' "$entry_lines" >> "$output"
120-
had_fallback=1
120+
MERGE_HAD_FALLBACK=1
121121
fi
122-
return $had_fallback
123122
}
124123

125124
restore_en() {
126125
# Restore all en-US.ftl files from backup
127126
for util_dir in "$COREUTILS_DIR"/src/uu/*/locales/; do
128127
util=$(basename "$(dirname "$util_dir")")
129128
backup="$TMPDIR/src-backup/uu/$util/locales/en-US.ftl"
130-
[ -f "$backup" ] && cp "$backup" "${util_dir}en-US.ftl"
129+
if [ -f "$backup" ]; then
130+
cp "$backup" "${util_dir}en-US.ftl"
131+
fi
131132
done
132-
local uucore_backup="$TMPDIR/src-backup/uucore/locales/en-US.ftl"
133-
[ -f "$uucore_backup" ] && cp "$uucore_backup" "$COREUTILS_DIR/src/uucore/locales/en-US.ftl"
133+
if [ -f "$TMPDIR/src-backup/uucore/locales/en-US.ftl" ]; then
134+
cp "$TMPDIR/src-backup/uucore/locales/en-US.ftl" "$COREUTILS_DIR/src/uucore/locales/en-US.ftl"
135+
fi
134136
# Restore English tldr
135-
[ -f "$EN_TLDR" ] && cp "$EN_TLDR" "$COREUTILS_DIR/docs/tldr.zip"
137+
if [ -f "$EN_TLDR" ]; then
138+
cp "$EN_TLDR" "$COREUTILS_DIR/docs/tldr.zip"
139+
fi
136140
}
137141

138142
cd "$COREUTILS_DIR"
@@ -154,7 +158,8 @@ for lang in "${!LANG_MAP[@]}"; do
154158
if [ -f "${util_dir}${ftl_name}.ftl" ]; then
155159
util=$(basename "$(dirname "$util_dir")")
156160
en_backup="$TMPDIR/src-backup/uu/$util/locales/en-US.ftl"
157-
if ! merge_ftl "$en_backup" "${util_dir}${ftl_name}.ftl" "${util_dir}en-US.ftl"; then
161+
merge_ftl "$en_backup" "${util_dir}${ftl_name}.ftl" "${util_dir}en-US.ftl"
162+
if [ "$MERGE_HAD_FALLBACK" = "1" ]; then
158163
fallback_utils[$util]=1
159164
fi
160165
fi
@@ -176,10 +181,11 @@ for lang in "${!LANG_MAP[@]}"; do
176181
weblate_lang="${ftl_name//-/_}"
177182
for util in "${!fallback_utils[@]}"; do
178183
md_file="docs/src/utils/${util}.md"
179-
[ -f "$md_file" ] || continue
180-
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>"
181-
# Insert notice after the first line (# utility-name)
182-
sed -i "1a\\${notice}" "$md_file"
184+
if [ -f "$md_file" ]; then
185+
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>"
186+
# Insert notice after the first line (# utility-name)
187+
sed -i "1a\\${notice}" "$md_file"
188+
fi
183189
done
184190
if [ ${#fallback_utils[@]} -gt 0 ]; then
185191
echo " Added translation notice to ${#fallback_utils[@]} utilities with untranslated strings"

0 commit comments

Comments
 (0)