Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.

Commit

Permalink
new lang helper
Browse files Browse the repository at this point in the history
  • Loading branch information
RealistikDash committed Oct 8, 2020
1 parent 7be7257 commit 583e0fb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
4 changes: 4 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class CryptKeys():
solo3 = "oC36fpYaPtdg"
solo4 = "pC26fpYaQCtg"

class Paths():
"""File paths."""
lang_packs = "languages/packs/"

ASCII_ART = r"""
{col1} _____ {col2} _____ {col3} _____ {col4} _____ {col5} _____ {reset}
{col1} /\ \ {col2} /\ \ {col3} /\ \ {col4} |\ \ {col5} /\ \ {reset}
Expand Down
71 changes: 65 additions & 6 deletions helpers/lang.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,71 @@
import json
import os
from helpers.generalhelper import JsonFile
from config import user_config
from constants import Paths
from exceptions import LangNotFound

class Lang:
def __init__(self):
for f in os.listdir("lang"):
if f == user_config["lang"] + ".json":
lang = json.load(open("lang/" + f, "r"))
for key, value in lang.items():
setattr(self, key, value)
def __init__(self, language : str = "en"):
"""Loads the given language."""
self.text_find_msg = "Failed to find translated text for {}|{}"
self.main_lang = language
self.english_lang = JsonFile(Paths.lang_packs + "en.json").get_file() # English as a backup for when the main lang does not contain the str you're looking for.

self.lang = JsonFile(Paths.lang_packs + language + ".json").get_file()

# Check if lang is valid.
if self.lang is None:
raise LangNotFound

def _format_string(self, text : str, format_args : tuple) -> str:
"""Formats a string according to the format args provided."""
return text.format(*format_args)

def _fail_find(self, type : str, text : str) -> str:
"""Creates a fail find str."""
return self.text_find_msg.format(type, text)

def _get_from_json(self, type : str, text : str) -> str:
"""Returns a string of given type from json."""
text_category = self.lang.get(type)
if text_category is None:
text_category = self.english_lang.get(type)
if text_category is None:
return self._fail_find(type, text)

new_text = text_category.get(text)
if new_text is None:
new_text = self.english_lang[type].get(text)
if new_text is None:
return self._fail_find(type, text)

return new_text

def _get_full(self, type : str, text : str, format_args : tuple = ()) -> str:
"""Gets full formatted translation from lang pack."""
new_text = self._get_from_json(type, text)
new_text = self._format_string(new_text, format_args)
return new_text

def warn(self, text : str, *args) -> str:
"""Translates a warn message."""
return self._get_full("warning", text, args)

def info(self, text : str, *args) -> str:
"""Translates a info message."""
return self._get_full("info", text, args)

def error(self, text : str, *args) -> str:
"""Translates a error message."""
return self._get_full("errors", text, args)

def debug(self, text : str, *args) -> str:
"""Translates a debug message."""
return self._get_full("debug", text, args)

def runtime(self, text : str, *args) -> str:
"""Translates a runtime message."""
return self._get_full("runtime", text, args)

lang = Lang()

0 comments on commit 583e0fb

Please sign in to comment.