#!/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//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 . 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()