Translated menu labels, instructions and credits, and Shift+F12 to English
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>
This commit is contained in:
1 parent
e6505790c2
commit
8d354dbe1c
5 files changed
+245
-17
No files matched your search
@@ -13,6 +13,10 @@ 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
|
||||
@@ -23,8 +27,15 @@ 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):
|
||||
@@ -37,11 +48,58 @@ def set_language(language=None, directory=None):
|
||||
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
|
||||
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)
|
||||
|
||||
|
||||
Reference in new issue
Block a user