-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalization.py
More file actions
44 lines (34 loc) · 1.31 KB
/
localization.py
File metadata and controls
44 lines (34 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import gettext
import os
import configparser
# Verzeichnis, in dem die Übersetzungen liegen
LOCALE_DIR = os.path.join(os.path.dirname(__file__), "locales")
DEFAULT_LANG = "de"
def get_translator():
"""
Liest die Konfigurationsdatei und gibt die korrekte Übersetzer-Funktion zurück.
"""
config = configparser.ConfigParser()
language_code = DEFAULT_LANG
config_path = "config.ini"
if os.path.exists(config_path):
config.read(config_path)
language_code = config.get("Settings", "language", fallback=DEFAULT_LANG)
try:
lang = gettext.translation(
"base", localedir=LOCALE_DIR, languages=[language_code], fallback=True
)
except FileNotFoundError:
# Fallback auf die Standardsprache, wenn die Übersetzungsdatei fehlt
lang = gettext.translation(
"base", localedir=LOCALE_DIR, languages=[DEFAULT_LANG], fallback=True
)
return lang.gettext
# Die globale Übersetzer-Funktion wird hier einmalig initialisiert
_ = get_translator()
def save_language_setting(language_code):
"""Speichert die ausgewählte Sprache in der Konfigurationsdatei."""
config = configparser.ConfigParser()
config["Settings"] = {"language": language_code}
with open("config.ini", "w") as configfile:
config.write(configfile)