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>
This commit is contained in:
1 parent
3619c6720b
commit
80ddfdcd34
11 files changed
+683
-147
No files matched your search
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Translations for the text libstormgames itself speaks and shows.
|
||||
|
||||
The library has its own gettext domain, "libstormgames", separate from any
|
||||
game's, so one translation of the library serves every game that uses it.
|
||||
Translations live in locales/<language>/LC_MESSAGES/libstormgames.po inside
|
||||
the library, and tools/update-translations.sh extracts the text and
|
||||
compiles them.
|
||||
|
||||
By default the library follows the system's language settings. A game
|
||||
that lets players choose a language calls set_language() with the same
|
||||
code it uses for its own text, so the library and the game always speak
|
||||
the same language.
|
||||
|
||||
Mark text with _() where it is spoken, ngettext() where it depends on a
|
||||
number, and N_() for tables defined before the language is chosen, passed
|
||||
through _() when spoken. Placeholders are always named, as in
|
||||
"{name} deleted", so a translation can put them in any order.
|
||||
"""
|
||||
|
||||
import gettext
|
||||
import os
|
||||
|
||||
domain = "libstormgames"
|
||||
localeDirectory = os.path.join(os.path.dirname(os.path.abspath(__file__)), "locales")
|
||||
translation = gettext.NullTranslations()
|
||||
|
||||
|
||||
def set_language(language=None, directory=None):
|
||||
"""Choose the language the library speaks.
|
||||
|
||||
Args:
|
||||
language (str): A language code such as "de" or "pt_BR", or None to
|
||||
follow the system's settings. Without a translation for it, the
|
||||
library speaks English.
|
||||
directory (str): Where to find translations, for testing one that is
|
||||
not part of the library yet. Defaults to the library's own locales.
|
||||
"""
|
||||
global translation
|
||||
languages = [language] if language else None
|
||||
translation = gettext.translation(domain, directory or localeDirectory, languages=languages, fallback=True)
|
||||
|
||||
|
||||
def _(message):
|
||||
return translation.gettext(message)
|
||||
|
||||
|
||||
def ngettext(singular, plural, count):
|
||||
return translation.ngettext(singular, plural, count)
|
||||
|
||||
|
||||
def pgettext(context, message):
|
||||
return translation.pgettext(context, message)
|
||||
|
||||
|
||||
def N_(message):
|
||||
"""Marks text for translation without translating it yet."""
|
||||
return message
|
||||
|
||||
|
||||
set_language()
|
||||
Reference in new issue
Block a user