#!/bin/bash # FDD Book Translation Setup Script # Creates a new language directory and populates it with content files for translation set -e # Exit on any error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" CONTENT_DIR="$PROJECT_ROOT/content" TRANSLATIONS_DIR="$PROJECT_ROOT/translations" # Function to print colored output print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_header() { echo -e "${BLUE}[SETUP]${NC} $1" } # Function to validate language code validate_language_code() { local lang_code="$1" # Check if language code is provided if [ -z "$lang_code" ]; then print_error "Language code is required." return 1 fi # Check if language code follows ISO 639-1 format (2 letters) or ISO 639-1 with region (e.g., pt_BR) if [[ ! "$lang_code" =~ ^[a-z]{2}(_[A-Z]{2})?$ ]]; then print_error "Invalid language code format. Use ISO 639-1 format (e.g., 'en', 'pt', 'es') or with region (e.g., 'pt_BR', 'en_US')." return 1 fi return 0 } # Function to check if content directory exists check_content_directory() { if [ ! -d "$CONTENT_DIR" ]; then print_error "Content directory does not exist: $CONTENT_DIR" exit 1 fi } # Function to create language directory create_language_directory() { local lang_code="$1" local lang_dir="$TRANSLATIONS_DIR/$lang_code" if [ ! -d "$lang_dir" ]; then print_status "Creating language directory: $lang_dir" mkdir -p "$lang_dir" else print_warning "Language directory already exists: $lang_dir" fi } # Global variables for tracking copy results COPY_COUNT=0 SKIP_COUNT=0 # Function to copy directory structure and files copy_content_structure() { local source_dir="$1" local target_dir="$2" # Reset counters COPY_COUNT=0 SKIP_COUNT=0 # Create target directory if it doesn't exist mkdir -p "$target_dir" # Find all .md files in source directory and copy them while IFS= read -r -d '' file; do # Get relative path from source directory local relative_path="${file#$source_dir/}" local target_file="$target_dir/$relative_path" local target_file_dir="$(dirname "$target_file")" # Create target directory if it doesn't exist mkdir -p "$target_file_dir" # Check if target file already exists if [ -f "$target_file" ]; then print_warning "Skipping existing file: $relative_path" ((SKIP_COUNT++)) else print_status "Copying: $relative_path" cp "$file" "$target_file" ((COPY_COUNT++)) fi done < <(find "$source_dir" -name "*.md" -type f -print0) } # Function to show usage show_usage() { echo "Usage: $0 [OPTIONS]" echo "" echo "Arguments:" echo " language_code ISO 639-1 language code (e.g., 'en', 'pt', 'es')" 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 " --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 get_language_name() { local lang_code="$1" case "$lang_code" in "en") echo "English" ;; "pt") echo "Portuguese" ;; "pt_BR") echo "Portuguese (Brazil)" ;; "es") echo "Spanish" ;; "es_MX") echo "Spanish (Mexico)" ;; "es_ES") echo "Spanish (Spain)" ;; "fr") echo "French" ;; "fr_FR") echo "French (France)" ;; "de") echo "German" ;; "de_DE") echo "German (Germany)" ;; "it") echo "Italian" ;; "it_IT") echo "Italian (Italy)" ;; "ru") echo "Russian" ;; "ru_RU") echo "Russian (Russia)" ;; "zh") echo "Chinese" ;; "zh_CN") echo "Chinese (Simplified)" ;; "zh_TW") echo "Chinese (Traditional)" ;; "ja") echo "Japanese" ;; "ja_JP") echo "Japanese (Japan)" ;; "ko") echo "Korean" ;; "ko_KR") echo "Korean (South Korea)" ;; *) echo "$lang_code" ;; esac } # Main script logic main() { local language_code="" local force_copy=false local verbose=false local skip_deps=false # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in -h|--help) show_usage exit 0 ;; -f|--force) force_copy=true shift ;; -v|--verbose) verbose=true shift ;; --skip-deps) skip_deps=true shift ;; -*) print_error "Unknown option: $1" show_usage exit 1 ;; *) if [ -z "$language_code" ]; then language_code="$1" else print_error "Multiple language codes provided. Only one is allowed." exit 1 fi shift ;; esac done # Validate language code if ! validate_language_code "$language_code"; then show_usage exit 1 fi # Get language name local language_name=$(get_language_name "$language_code") print_header "Setting up translation for $language_name ($language_code)" # Check prerequisites check_content_directory # Create language directory create_language_directory "$language_code" local lang_dir="$TRANSLATIONS_DIR/$language_code" # Copy content structure print_status "Copying content files..." copy_content_structure "$CONTENT_DIR" "$lang_dir" print_status "Translation setup completed!" print_status "Language directory: $lang_dir" print_status "Files copied: $COPY_COUNT" print_status "Files skipped (already exist): $SKIP_COUNT" if [ "$SKIP_COUNT" -gt 0 ]; then print_warning "Some files already existed and were skipped." 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" } # Run main function with all arguments main "$@"