#!/usr/bin/env bash # Keep the translations of libstormgames' own text up to date. # # tools/update-translations.sh extract the text, merge it into # every translation and compile them # tools/update-translations.sh new de start a translation, for example de or pt_BR # # The text to translate goes to locales/libstormgames.pot. Each translation is # locales//LC_MESSAGES/libstormgames.po, compiled next to it as # libstormgames.mo, which is what the library reads. Games choose the language # with set_language(). Needs GNU gettext. set -euo pipefail libraryDir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" localeDir="$libraryDir/locales" templateFile="$localeDir/libstormgames.pot" cd "$libraryDir" mkdir -p "$localeDir" sourceFiles=(./*.py) xgettext --language=Python --from-code=UTF-8 \ --keyword=_ --keyword=N_ --keyword=ngettext:1,2 --keyword=pgettext:1c,2 \ --add-comments=Translators --package-name="libstormgames" --sort-by-file \ --output="$templateFile" "${sourceFiles[@]}" echo "Extracted the text to translate into $templateFile" if [[ "${1:-}" == "new" ]]; then language="${2:?Name the language, for example: tools/update-translations.sh new de}" newFile="$localeDir/$language/LC_MESSAGES/libstormgames.po" if [[ -e "$newFile" ]]; then echo "$newFile already exists, so it was left alone." exit 1 fi mkdir -p "$(dirname "$newFile")" msginit --no-translator --locale="$language" --input="$templateFile" --output-file="$newFile" # msginit often picks ASCII, which cannot hold accented letters, so switch to UTF-8. msgconv --to-code=UTF-8 --output-file="$newFile" "$newFile" echo "Started $newFile" fi shopt -s nullglob for translationFile in "$localeDir"/*/LC_MESSAGES/libstormgames.po; do msgmerge --quiet --update --backup=none "$translationFile" "$templateFile" msgfmt --check --output-file="${translationFile%.po}.mo" "$translationFile" echo "Compiled $translationFile" done