game_menu takes optionLabels, a dictionary or a function giving what to say for each of a game's own options, so a translated game no longer hears its option names read out in English. A function is asked each time, so labels follow a change of language. The option is still what game_menu returns. instructions() and credits() read files/instructions.<language>.txt and files/credits.<language>.txt when there is one for the language being spoken, falling back to the language without its region and then to English, and read them as UTF-8. translated_file() and language_code() do the same for a game's own files. Shift+F12 in game_menu, instruction_menu and learn_sounds goes back to English and says so in English. set_language_reset() lets a game switch its own text and remember the choice at the same time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
4.4 KiB
Python
121 lines
4.4 KiB
Python
#!/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.
|
|
|
|
Shift+F12 in the library's menus goes back to English, and says so in
|
|
English. A game passes set_language_reset() what to do then, so it can
|
|
switch its own text and remember the choice too.
|
|
|
|
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"
|
|
english = "en"
|
|
localeDirectory = os.path.join(os.path.dirname(os.path.abspath(__file__)), "locales")
|
|
translation = gettext.NullTranslations()
|
|
# The language a game chose, or None while following the system.
|
|
currentLanguage = None
|
|
# What Shift+F12 in the menus calls to go back to English; None just switches the library.
|
|
languageResetHandler = None
|
|
# Said after Shift+F12, in English whatever the language was, so it is never translated.
|
|
languageResetMessage = "Language set to English."
|
|
|
|
|
|
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, currentLanguage
|
|
currentLanguage = language
|
|
languages = [language] if language else None
|
|
translation = gettext.translation(domain, directory or localeDirectory, languages=languages, fallback=True)
|
|
|
|
|
|
def language_code():
|
|
"""The language being spoken: the one a game chose, else the system's, as "de" or "pt_BR"."""
|
|
if currentLanguage:
|
|
return currentLanguage
|
|
# The same settings gettext follows, in the same order.
|
|
for variable in ("LANGUAGE", "LC_ALL", "LC_MESSAGES", "LANG"):
|
|
value = os.environ.get(variable)
|
|
if value:
|
|
code = value.split(":")[0].split(".")[0].split("@")[0]
|
|
if code and code not in ("C", "POSIX"):
|
|
return code
|
|
return english
|
|
|
|
|
|
def translated_file(path):
|
|
"""A text file in the language being spoken, as <name>.<language><extension> beside it, such as
|
|
files/instructions.de.txt for files/instructions.txt, or path itself when there is none."""
|
|
stem, extension = os.path.splitext(path)
|
|
code = language_code()
|
|
for candidate in (code, code.split("_")[0]):
|
|
if candidate == english:
|
|
continue
|
|
translated = f"{stem}.{candidate}{extension}"
|
|
if os.path.isfile(translated):
|
|
return translated
|
|
return path
|
|
|
|
|
|
def set_language_reset(handler=None):
|
|
"""Choose what Shift+F12 in the library's menus does, the way back for anyone stuck in a
|
|
language they cannot understand. handler is called with no arguments, and should switch
|
|
the game and the library to English and remember that, as by calling set_language("en").
|
|
Without one, Shift+F12 switches only the library's own text to English."""
|
|
global languageResetHandler
|
|
languageResetHandler = handler
|
|
|
|
|
|
def reset_language():
|
|
"""Go back to English, as Shift+F12 does in the menus. Returns the English message to speak."""
|
|
if languageResetHandler:
|
|
languageResetHandler()
|
|
else:
|
|
set_language(english)
|
|
return languageResetMessage
|
|
|
|
|
|
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()
|