Files
libstormgames/tools/update-translations.sh
Storm DragonandClaude Opus 5.5 80ddfdcd34 Translate the library's own text, in its own gettext domain
Everything the library speaks or shows itself is marked for
translation in a libstormgames domain, separate from any game's, so
one translation serves every game: the text box, the text window,
speech history, game_menu's own items, learn_sounds, instructions,
credits, donate and the high scores. Sentences built from pieces are
whole sentences with named placeholders now, such as
"{character} deleted".

set_language(code) chooses the language, so a game that lets players
pick one can make the library speak it too; with no call it follows
the system, and falls back to English. Translations go in
locales/<language>/LC_MESSAGES/libstormgames.po, and
tools/update-translations.sh extracts the text into
locales/libstormgames.pot and compiles them.

The text box now names symbols when moving over or deleting them, as
it already did when typing, and get_input's default prompt is
translated. Game.display_text no longer drops the first and last
lines when Space reads them all or T copies them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 00:10:19 -05:00

49 lines
2.0 KiB
Bash
Executable File

#!/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/<language>/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