scripts: add zh_CN build support and language-aware dependency install

Wire Chinese (Simplified) into build-book.sh as a first-class target
alongside en_US, pt_BR, and es_ES, and teach setup_translation.sh how
to install the system packages that translated editions need.

build-book.sh:
- Add zh_CN to LANGUAGES_ALL and to every language helper
  (get_chapters_dir, get_appendices_dir, get_pandoc_lang,
  get_lang_display, normalize_lang) so it flows through the
  existing per-language build path with no special-casing.
- Wire Noto CJK fonts into the PDF build for zh_CN via the
  Eisvogel template's CJKmainfont/CJKsansfont/CJKmonofont
  variables (which auto-load xeCJK), plus a mainfont override
  so Latin text in the body matches the surrounding Chinese.
  The Latin defaults from metadata.yaml have no CJK glyphs,
  so this is required for the zh_CN PDF to render.
- Refresh help text, error messages, output-file listing,
  CONTENT SOURCES, EXAMPLES, and REQUIREMENTS to include
  zh_CN and the CJK font / texlive-lang-chinese dependencies.

setup_translation.sh:
- Add install_language_dependencies(), which maps a language
  code to the apt packages needed to build that edition:
  Noto CJK fonts + texlive-lang-chinese/cjk for zh*, the
  Japanese and Korean equivalents for ja/ko, and the
  matching texlive-lang-* packs for pt/es/fr/de/it/ru.
- Print the exact apt-get and fc-cache commands before
  running them so they can be reproduced manually, and
  fall back cleanly when apt-get is unavailable (non-Debian
  hosts) or sudo credentials are not cached.
- Add a --skip-deps flag for CI and unprivileged environments
  that just want the directory scaffolding without touching
  system packages.
- Update usage/help to document --skip-deps and the new
  zh_CN example.
This commit is contained in:
Edson Brandi 2026-05-13 20:53:13 +00:00
parent 55b603ecea
commit 2e72c740ef
2 changed files with 182 additions and 16 deletions

View file

@ -42,6 +42,7 @@
# en_US English (original) content/chapters, content/appendices
# pt_BR Brazilian Portuguese translations/pt_BR/chapters, translations/pt_BR/appendices
# es_ES Spanish translations/es_ES/chapters, translations/es_ES/appendices
# zh_CN Chinese (Simplified) translations/zh_CN/chapters, translations/zh_CN/appendices
#
# Running the script with no arguments builds every supported format
# (PDF + EPUB + HTML) in every supported language. Individual formats or
@ -78,7 +79,7 @@ PDF_ENGINE="xelatex"
# Supported languages in canonical form. The list order controls the order
# used by "build everything" runs and by the final summary.
LANGUAGES_ALL=(en_US pt_BR es_ES)
LANGUAGES_ALL=(en_US pt_BR es_ES zh_CN)
# =============================================================================
# LANGUAGE HELPERS
@ -90,6 +91,7 @@ get_chapters_dir() {
en_US) echo "content/chapters" ;;
pt_BR) echo "translations/pt_BR/chapters" ;;
es_ES) echo "translations/es_ES/chapters" ;;
zh_CN) echo "translations/zh_CN/chapters" ;;
*) echo ""; return 1 ;;
esac
}
@ -100,18 +102,21 @@ get_appendices_dir() {
en_US) echo "content/appendices" ;;
pt_BR) echo "translations/pt_BR/appendices" ;;
es_ES) echo "translations/es_ES/appendices" ;;
zh_CN) echo "translations/zh_CN/appendices" ;;
*) echo ""; return 1 ;;
esac
}
# Map a canonical language code to its Pandoc/LaTeX `lang` metadata value.
# Pandoc uses this to pick the right hyphenation/typography rules, which is
# why pt_BR and es_ES need it set explicitly instead of defaulting to en-US.
# why pt_BR, es_ES, and zh_CN need it set explicitly instead of defaulting
# to en-US.
get_pandoc_lang() {
case "$1" in
en_US) echo "en-US" ;;
pt_BR) echo "pt-BR" ;;
es_ES) echo "es-ES" ;;
zh_CN) echo "zh-CN" ;;
*) echo ""; return 1 ;;
esac
}
@ -122,6 +127,7 @@ get_lang_display() {
en_US) echo "English (en_US)" ;;
pt_BR) echo "Brazilian Portuguese (pt_BR)" ;;
es_ES) echo "Spanish (es_ES)" ;;
zh_CN) echo "Chinese Simplified (zh_CN)" ;;
*) echo "$1" ;;
esac
}
@ -141,6 +147,7 @@ normalize_lang() {
enus) echo "en_US" ;;
ptbr) echo "pt_BR" ;;
eses) echo "es_ES" ;;
zhcn) echo "zh_CN" ;;
*) echo "" ;;
esac
}
@ -272,13 +279,14 @@ FORMATS (case-insensitive; may be combined; default: all three):
--html Build HTML5
--all Shorthand for --pdf --epub --html
LANGUAGES (case- and separator-insensitive; repeatable; default: all three):
LANGUAGES (case- and separator-insensitive; repeatable; default: all four):
--lang CODE
--lang=CODE
CODE may be:
en_US English (original)
pt_BR Brazilian Portuguese
es_ES Spanish
zh_CN Chinese (Simplified)
all Every supported language (the default)
Case and the separator are ignored, so the following are equivalent:
pt_BR pt-BR PT_br Pt-Br ptbr PTBR
@ -289,7 +297,7 @@ OTHER OPTIONS:
DEFAULTS:
Running "$0" with no arguments builds every supported format (PDF, EPUB,
HTML) in every supported language (en_US, pt_BR, es_ES).
HTML) in every supported language (en_US, pt_BR, es_ES, zh_CN).
OUTPUT DIRECTORY:
Generated files are placed under: $OUTPUT_DIR/
@ -307,11 +315,16 @@ OUTPUT FILES (per language):
$OUTPUT_DIR/${OUTPUT_BASENAME}-es_ES.pdf
$OUTPUT_DIR/${OUTPUT_BASENAME}-es_ES.epub
$OUTPUT_DIR/${OUTPUT_BASENAME}-es_ES.html
Chinese Simplified (zh_CN):
$OUTPUT_DIR/${OUTPUT_BASENAME}-zh_CN.pdf
$OUTPUT_DIR/${OUTPUT_BASENAME}-zh_CN.epub
$OUTPUT_DIR/${OUTPUT_BASENAME}-zh_CN.html
CONTENT SOURCES:
en_US content/chapters/ content/appendices/
pt_BR translations/pt_BR/chapters/ translations/pt_BR/appendices/
es_ES translations/es_ES/chapters/ translations/es_ES/appendices/
zh_CN translations/zh_CN/chapters/ translations/zh_CN/appendices/
EXAMPLES:
$0 # Build every format in every language
@ -319,6 +332,7 @@ EXAMPLES:
$0 --lang pt_BR # Build every format in Brazilian Portuguese
$0 --lang pt-br --lang es-ES # Build every format in pt_BR and es_ES
$0 --pdf --lang en_US # Build the English PDF only
$0 --pdf --lang zh_CN # Build the Chinese (Simplified) PDF only
$0 --EPUB --HTML --Lang=Pt-Br # Mixed case (all equivalent)
$0 --test # Check dependencies without building
@ -326,9 +340,12 @@ REQUIREMENTS:
- Pandoc 3.0+
- XeLaTeX (texlive-xetex)
- Eisvogel template at: $EISVOGEL_TEMPLATE
- LaTeX hyphenation packs for Portuguese and Spanish
(e.g. texlive-lang-portuguese, texlive-lang-spanish)
- Required fonts (Times New Roman, Arial, Liberation Mono)
- LaTeX hyphenation/language packs for the translated editions
(e.g. texlive-lang-portuguese, texlive-lang-spanish,
texlive-lang-chinese, texlive-lang-cjk)
- Required Latin fonts (Times New Roman, Arial, Liberation Mono)
- Required CJK fonts for zh_CN
(fonts-noto-cjk, fonts-noto-cjk-extra — Noto Serif/Sans/Mono CJK SC)
For detailed installation instructions, see: $SCRIPT_DIR/BUILD-README.md
EOF
@ -490,6 +507,25 @@ build_pdf() {
return 1
fi
# Language-specific font configuration. The default fonts declared in
# scripts/metadata.yaml (Times New Roman / Arial / Liberation Mono) have
# no CJK glyphs, so zh_CN needs Noto CJK fonts wired in through the
# Eisvogel template's CJKmainfont/CJKsansfont/CJKmonofont variables
# (which auto-load xeCJK) plus a mainfont override so Latin text in the
# body shares the same visual weight as the surrounding Chinese.
local -a font_args=()
case "$lang" in
zh_CN)
font_args=(
--variable mainfont="Noto Serif CJK SC"
--variable sansfont="Noto Sans CJK SC"
--variable CJKmainfont="Noto Serif CJK SC"
--variable CJKsansfont="Noto Sans CJK SC"
--variable CJKmonofont="Noto Sans Mono CJK SC"
)
;;
esac
echo " Including files: ${#BUILD_FILES[@]} total (1 title + ${BUILD_FILES_CHAPTERS} chapters + ${BUILD_FILES_APPENDICES} appendices)"
echo " Running pandoc with Eisvogel template (lang=$pandoc_lang)..."
@ -517,6 +553,7 @@ build_pdf() {
--syntax-highlighting=tango \
--variable linestretch=1.15 \
--variable geometry:"inner=2cm,outer=2cm,top=2.5cm,bottom=2.5cm" \
"${font_args[@]}" \
-o "$output_file" 2>&1
local exit_code=$?
@ -747,7 +784,7 @@ while [[ $# -gt 0 ]]; do
canonical="$(normalize_lang "$raw_value")"
if [ -z "$canonical" ]; then
echo "❌ Unknown language: '$raw_value'"
echo " Accepted: en_US, pt_BR, es_ES, or all (case- and separator-insensitive)"
echo " Accepted: en_US, pt_BR, es_ES, zh_CN, or all (case- and separator-insensitive)"
exit 1
fi
if [ "$canonical" = "all" ]; then
@ -760,13 +797,13 @@ while [[ $# -gt 0 ]]; do
--lang)
shift
if [ $# -eq 0 ]; then
echo "❌ --lang requires a value (en_US, pt_BR, es_ES, or all)"
echo "❌ --lang requires a value (en_US, pt_BR, es_ES, zh_CN, or all)"
exit 1
fi
canonical="$(normalize_lang "$1")"
if [ -z "$canonical" ]; then
echo "❌ Unknown language: '$1'"
echo " Accepted: en_US, pt_BR, es_ES, or all (case- and separator-insensitive)"
echo " Accepted: en_US, pt_BR, es_ES, zh_CN, or all (case- and separator-insensitive)"
exit 1
fi
if [ "$canonical" = "all" ]; then

View file

@ -121,17 +121,132 @@ show_usage() {
echo " or with region (e.g., 'pt_BR', 'en_US')"
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo " -f, --force Force copy even if files already exist"
echo " -v, --verbose Show detailed output"
echo " -h, --help Show this help message"
echo " -f, --force Force copy even if files already exist"
echo " -v, --verbose Show detailed output"
echo " --skip-deps Do not attempt to install language-specific"
echo " system dependencies (fonts, LaTeX packs)"
echo ""
echo "Examples:"
echo " $0 fr # Set up French translation"
echo " $0 de_DE # Set up German (Germany) translation"
echo " $0 es_MX # Set up Spanish (Mexico) translation"
echo " $0 zh_CN # Set up Chinese (Simplified) translation"
echo ""
echo "This script creates a new language directory under translations/"
echo "and populates it with copies of content files for translation work."
echo "For languages that need special fonts or LaTeX hyphenation packs"
echo "(e.g. zh_CN, zh_TW, ja_JP, ko_KR), it will also try to install the"
echo "required system packages via apt-get unless --skip-deps is given."
}
# Function to install language-specific system dependencies (fonts and
# LaTeX hyphenation packs). The default build configuration in
# scripts/metadata.yaml only covers Latin-script fonts, so CJK editions
# need additional Noto CJK fonts plus the matching texlive-lang-* packs
# in order for build-book.sh to produce a usable PDF.
#
# The function prints the exact commands first so they can be copied
# manually, then attempts to run them. It is a no-op for languages that
# do not need extra packages, and it bails out cleanly on non-Debian
# systems where apt-get is unavailable.
install_language_dependencies() {
local lang_code="$1"
local -a apt_packages=()
case "$lang_code" in
zh|zh_CN|zh_TW|zh_HK)
# Simplified and Traditional Chinese: Noto CJK fonts plus
# texlive's Chinese/CJK language support.
apt_packages=(
fonts-noto-cjk
fonts-noto-cjk-extra
texlive-lang-chinese
texlive-lang-cjk
)
;;
ja|ja_JP)
apt_packages=(
fonts-noto-cjk
fonts-noto-cjk-extra
fonts-ipafont
fonts-ipafont-gothic
fonts-ipafont-mincho
texlive-lang-japanese
texlive-lang-cjk
)
;;
ko|ko_KR)
apt_packages=(
fonts-noto-cjk
fonts-noto-cjk-extra
fonts-nanum
texlive-lang-korean
texlive-lang-cjk
)
;;
pt|pt_BR|pt_PT)
apt_packages=(texlive-lang-portuguese)
;;
es|es_ES|es_MX)
apt_packages=(texlive-lang-spanish)
;;
fr|fr_FR)
apt_packages=(texlive-lang-french)
;;
de|de_DE)
apt_packages=(texlive-lang-german)
;;
it|it_IT)
apt_packages=(texlive-lang-italian)
;;
ru|ru_RU)
apt_packages=(fonts-liberation texlive-lang-cyrillic)
;;
*)
# No extra dependencies known for this language.
return 0
;;
esac
if [ "${#apt_packages[@]}" -eq 0 ]; then
return 0
fi
print_header "Installing system dependencies for $lang_code"
echo "The following apt packages are required to build $lang_code:"
echo ""
echo " sudo apt-get install -y ${apt_packages[*]}"
echo ""
echo "After installation, refresh the font cache so XeLaTeX can see"
echo "any new fonts:"
echo ""
echo " sudo fc-cache -fv"
echo ""
if ! command -v apt-get >/dev/null 2>&1; then
print_warning "apt-get not found. Skipping automatic install."
print_warning "Run the commands above manually on this system."
return 0
fi
if ! sudo -n true 2>/dev/null; then
print_warning "Automatic install needs sudo and the cached"
print_warning "credentials are missing. You will be prompted"
print_warning "for your password by sudo below; press Ctrl-C"
print_warning "to cancel and run the commands manually."
fi
if sudo apt-get install -y "${apt_packages[@]}"; then
print_status "Packages installed successfully."
if command -v fc-cache >/dev/null 2>&1; then
sudo fc-cache -fv >/dev/null 2>&1 || true
print_status "Font cache refreshed."
fi
else
print_error "apt-get install failed. Run the commands above manually."
return 1
fi
}
# Function to get language name from code
@ -168,6 +283,7 @@ main() {
local language_code=""
local force_copy=false
local verbose=false
local skip_deps=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
@ -184,6 +300,10 @@ main() {
verbose=true
shift
;;
--skip-deps)
skip_deps=true
shift
;;
-*)
print_error "Unknown option: $1"
show_usage
@ -233,6 +353,15 @@ main() {
print_warning "Use --force option to overwrite existing files."
fi
# Install language-specific system dependencies (fonts, LaTeX packs).
# Off by default for CI / unprivileged environments via --skip-deps.
if [ "$skip_deps" = true ]; then
print_status "Skipping language-specific dependency install (--skip-deps)."
else
install_language_dependencies "$language_code" || \
print_warning "Continuing despite dependency installation issues."
fi
print_status "Translators can now start working on files in: $lang_dir"
}