singlang/scripts/generate_translations.sh

141 lines
5.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
TRANSLATIONS_DIR="translations"
POT_FILE="$TRANSLATIONS_DIR/messages.pot"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
show_help() {
# Определяем язык системы (берем первые два символа из LANG или LC_ALL)
SYS_LANG="${LC_ALL:-$LANG}"
SYS_LANG="${SYS_LANG:0:2}"
# По умолчанию английский, если язык не ru
if [ "$SYS_LANG" = "ru" ]; then
echo "Справка по скрипту"
echo "-----------------"
echo "Назначение:"
echo " Этот скрипт автоматизирует создание и обновление файлов переводов (.po)"
echo " для исходных файлов с расширением .d в директории source/."
echo ""
echo "Использование:"
echo " ./$(basename "$0") [язык]"
echo " ./$(basename "$0") [-h | --help]"
echo ""
echo "Параметры:"
echo " [язык] - Необязательный код языка (например, ru, en). Если не указан,"
echo " обрабатываются языки ru и en."
echo " -h, --help - Показать эту справку и выйти."
echo ""
echo "Описание работы:"
echo " 1. Проверяет наличие .d-файлов в source/."
echo " 2. Создаёт или обновляет POT-файл (translations/messages.pot) из .d-файлов."
echo " 3. Создаёт или обновляет .po-файлы для указанного языка (или ru и en по умолчанию)."
echo " 4. Удаляет временный POT-файл после завершения."
echo ""
echo "Примеры:"
echo " ./$(basename "$0") # Создать/обновить переводы для ru и en"
echo " ./$(basename "$0") ru # Создать/обновить перевод только для ru"
echo " ./$(basename "$0") -h # Показать эту справку"
echo ""
echo "Требования:"
echo " Установленные утилиты: xgettext, msgmerge, msginit (GNU Gettext)."
else
echo "Script Help"
echo "-----------"
echo "Purpose:"
echo " This script automates the creation and updating of translation files (.po)"
echo " for source files with the .d extension in the source/ directory."
echo ""
echo "Usage:"
echo " ./$(basename "$0") [language]"
echo " ./$(basename "$0") [-h | --help]"
echo ""
echo "Parameters:"
echo " [language] - Optional language code (e.g., ru, en). If not specified,"
echo " processes languages ru and en."
echo " -h, --help - Show this help and exit."
echo ""
echo "How it works:"
echo " 1. Checks for .d files in source/."
echo " 2. Creates or updates the POT file (translations/messages.pot) from .d files."
echo " 3. Creates or updates .po files for the specified language (or ru and en by default)."
echo " 4. Removes the temporary POT file after completion."
echo ""
echo "Examples:"
echo " ./$(basename "$0") # Create/update translations for ru and en"
echo " ./$(basename "$0") ru # Create/update translation only for ru"
echo " ./$(basename "$0") -h # Show this help"
echo ""
echo "Requirements:"
echo " Installed utilities: xgettext, msgmerge, msginit (GNU Gettext)."
fi
exit 0
}
check_source_files() {
if ! find source/ -name "*.d" | grep -q .; then
echo -e "${RED}Error: No .d files found in source/ or its subdirectories${NC}"
exit 1
fi
}
generate_pot_file() {
echo -e "${GREEN}Generating POT file from all .d files in source/ and subdirectories...${NC}"
if [ -f "$POT_FILE" ]; then
local temp_pot="$TRANSLATIONS_DIR/temp.pot"
find source/ -name "*.d" -exec xgettext --keyword=_ --language=C --from-code=UTF-8 -o "$temp_pot" {} +
msgmerge -U "$POT_FILE" "$temp_pot"
rm "$temp_pot"
else
find source/ -name "*.d" -exec xgettext --keyword=_ --language=C --from-code=UTF-8 -o "$POT_FILE" {} +
fi
if [ ! -f "$POT_FILE" ]; then
echo -e "${RED}Error: Failed to generate $POT_FILE${NC}"
exit 1
fi
}
process_language() {
local lang="$1"
local po_file="$TRANSLATIONS_DIR/${lang}.po"
if [ ! -f "$po_file" ]; then
echo -e "${GREEN}Creating $po_file...${NC}"
msginit -l "$lang" -i "$POT_FILE" -o "$po_file" --no-translator
sed -i 's/charset=ASCII/charset=UTF-8/' "$po_file"
else
echo -e "${YELLOW}Updating $po_file with existing translations preserved...${NC}"
msgmerge -U "$po_file" "$POT_FILE"
fi
}
main() {
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_help
fi
mkdir -p "$TRANSLATIONS_DIR"
check_source_files
generate_pot_file
if [ $# -eq 1 ]; then
process_language "$1"
echo -e "${GREEN}PO file for language '$1' generated successfully!${NC}"
else
for lang in ru en; do
process_language "$lang"
done
echo -e "${GREEN}PO files generated successfully!${NC}"
fi
rm "$POT_FILE"
}
main "$@"