Projekt_IO/language_label_mapper.py

31 lines
1023 B
Python
Raw Normal View History

import os
2024-01-14 14:53:54 +01:00
from jproperties import Properties
"""
Translator method.
If everything fine then returns translated labels.
Else throws an Exception and returns untranslated labels.
"""
def translate(to_translate, lang):
try:
config = Properties()
script_directory = os.path.dirname(os.path.abspath(__file__))
resources_path = os.path.join(script_directory, "./resources")
2024-01-14 14:53:54 +01:00
# Load properties file for given lang
with open(os.path.join(resources_path, f"./{lang}.properties"), 'rb') as config_file:
2024-01-14 14:53:54 +01:00
config.load(config_file, encoding='UTF-8')
# Translate labels for given to_translate dictionary
for index, label_info in to_translate.items():
label = label_info.get("label")
to_translate[index]["label"] = config.get(label).data
2024-01-17 19:46:35 +01:00
return to_translate, []
2024-01-14 14:53:54 +01:00
except Exception as e:
error_message = f"Error translating labels: {e}"
print(error_message)
return to_translate, error_message