Skip to content

Commit 24a8207

Browse files
committed
Auto-detect locales in build-docs-l10n.sh from coreutils source
Replace hardcoded FTL_TO_URL and LANG_NAMES maps with dynamic detection. ftl_to_url() derives URL codes from FTL filenames (e.g., fr-FR -> fr, zh-Hans -> zh-Hans). locale_display_name() uses python3+babel for native language names with fallback to the code itself. New locales are now picked up automatically without editing the scripts.
1 parent 57433be commit 24a8207

2 files changed

Lines changed: 48 additions & 27 deletions

File tree

scripts/build-docs-l10n.sh

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ set -euo pipefail
1414

1515
COREUTILS_DIR="$(cd "${1:?Usage: $0 <coreutils-dir>}" && pwd)"
1616

17-
# ftl filename -> URL lang code (when they differ)
18-
declare -A FTL_TO_URL=(
19-
[fr-FR]="fr" [es-ES]="es" [zh-Hans]="zh" [zh-Hant]="zh-Hant"
20-
[pt-BR]="pt-BR" [nb-NO]="nb-NO"
21-
)
17+
# Convert FTL locale code to URL code: strip region when language == region
18+
# (e.g., fr-FR -> fr, es-ES -> es) but keep distinct ones (zh-Hans, pt-BR, nb-NO)
19+
ftl_to_url() {
20+
local code="$1"
21+
local lang="${code%%-*}"
22+
local region="${code#*-}"
23+
if [ "${region,,}" = "${lang,,}" ]; then
24+
echo "$lang"
25+
else
26+
echo "$code"
27+
fi
28+
}
2229

2330
# Discover available locales from the coreutils source (l10n already copied in)
2431
# Use ls utility as reference
@@ -27,7 +34,7 @@ for ftl in "$COREUTILS_DIR"/src/uu/ls/locales/*.ftl; do
2734
[ -f "$ftl" ] || continue
2835
ftl_name=$(basename "$ftl" .ftl)
2936
[ "$ftl_name" = "en-US" ] && continue
30-
url_code="${FTL_TO_URL[$ftl_name]:-$ftl_name}"
37+
url_code=$(ftl_to_url "$ftl_name")
3138
LANG_MAP[$url_code]="$ftl_name"
3239
done
3340

scripts/patch-mdbook-theme.sh

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,39 @@ L10N_DIR="${2:?Usage: $0 <coreutils-docs-dir> <coreutils-l10n-dir>}"
99

1010
HEAD_HBS="$DOCS_DIR/theme/head.hbs"
1111

12-
# Language display names (ftl filename -> display name)
13-
declare -A LANG_NAMES=(
14-
[en-US]="English"
15-
[ar]="العربية" [ast]="Asturianu" [ca]="Català" [cs]="Čeština"
16-
[da]="Dansk" [de]="Deutsch" [eo]="Esperanto" [es-ES]="Español"
17-
[fi]="Suomi" [fr-FR]="Français" [he]="עברית" [id]="Bahasa Indonesia"
18-
[it]="Italiano" [ja]="日本語" [kab]="Taqbaylit" [ko]="한국어"
19-
[nb-NO]="Norsk Bokmål" [ne]="नेपाली" [pl]="Polski" [pt]="Português"
20-
[pt-BR]="Português (Brasil)" [ru]="Русский" [sv]="Svenska"
21-
[tr]="Türkçe" [uk]="Українська" [vi]="Tiếng Việt"
22-
[zh-Hans]="中文 (简体)" [zh-Hant]="中文 (繁體)"
23-
)
24-
25-
# ftl filename -> URL lang code (used in /coreutils/docs-{code}/)
26-
declare -A FTL_TO_URL=(
27-
[en-US]="en" [fr-FR]="fr" [es-ES]="es" [zh-Hans]="zh" [zh-Hant]="zh-Hant"
28-
[pt-BR]="pt-BR" [nb-NO]="nb-NO"
29-
)
30-
# For others, the ftl name IS the URL code (ar, de, it, ja, etc.)
12+
# Convert FTL locale code to URL code: strip region when language == region
13+
# (e.g., fr-FR -> fr, es-ES -> es) but keep distinct ones (zh-Hans, pt-BR, nb-NO)
14+
ftl_to_url() {
15+
local code="$1"
16+
local lang="${code%%-*}"
17+
local region="${code#*-}"
18+
# If region is the language uppercased (fr-FR, es-ES, etc.), simplify to just the language
19+
if [ "${region,,}" = "${lang,,}" ]; then
20+
echo "$lang"
21+
else
22+
echo "$code"
23+
fi
24+
}
25+
26+
# Generate display name for a locale code
27+
# Uses python3+babel if available, otherwise falls back to the code itself
28+
locale_display_name() {
29+
local code="$1"
30+
if command -v python3 > /dev/null 2>&1; then
31+
local name
32+
name=$(python3 -c "
33+
try:
34+
from babel import Locale
35+
print(Locale.parse('${code}'.replace('-', '_')).get_display_name())
36+
except Exception:
37+
print('${code}')
38+
" 2>/dev/null)
39+
# Capitalize first letter for consistency
40+
echo "${name^}"
41+
else
42+
echo "$code"
43+
fi
44+
}
3145

3246
# Scan l10n repo to find which locales have translations
3347
# Use a representative utility (ls) to find available locales
@@ -37,8 +51,8 @@ for ftl in "$L10N_DIR"/src/uu/ls/locales/*.ftl; do
3751
ftl_name=$(basename "$ftl" .ftl)
3852
[ "$ftl_name" = "en-US" ] && continue
3953

40-
display="${LANG_NAMES[$ftl_name]:-$ftl_name}"
41-
url_code="${FTL_TO_URL[$ftl_name]:-$ftl_name}"
54+
url_code=$(ftl_to_url "$ftl_name")
55+
display=$(locale_display_name "$ftl_name")
4256
LANGS_JSON="$LANGS_JSON, ['$url_code', '$display']"
4357
done
4458

0 commit comments

Comments
 (0)