Skip to content

Commit

Permalink
Allow custom language files
Browse files Browse the repository at this point in the history
  • Loading branch information
tibue99 committed May 15, 2023
1 parent 852c73f commit 90650e1
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ venv
*.db
*.log
*.ini
*.json
embeds.json

# Packaging
build/
Expand Down
2 changes: 1 addition & 1 deletion src/ezcord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ async def _error_event(self, ctx: discord.ApplicationContext, error: discord.Dis
else:
error_msg = f"{error}"

error_txt = f"{t('error')}: ```{error_msg}```"
error_txt = f"{t('error', f'```{error_msg}```')}"
try:
await error_emb(ctx, error_txt, title="Error")
except discord.HTTPException:
Expand Down
14 changes: 14 additions & 0 deletions src/ezcord/internal/language/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"times": {
"min": "Minute",
"sec": "Sekunde",
"hour": "Stunde",
"day": "Tag"
},
"bot": {
"error": "Der folgende Fehler ist aufgetreten: {}",
"cooldown": "Versuche es {} erneut.",
"no_perm_title": "Fehlende Berechtigung",
"no_perm_desc": "Mir fehlen die folgenden Berechtigungen, um diesen Befehl auszuführen."
}
}
14 changes: 14 additions & 0 deletions src/ezcord/internal/language/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"times": {
"min": "minute",
"sec": "second",
"hour": "hour",
"day": "day"
},
"bot": {
"error": "The following error occurred: {}",
"cooldown": "Try again {}.",
"no_perm_title": "Missing permission",
"no_perm_desc": "I'm missing the following permissions to execute this command."
}
}
33 changes: 33 additions & 0 deletions src/ezcord/internal/language/languages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json
import os
from functools import cache
from pathlib import Path


@cache
def load_txt(language: str) -> dict:
"""Load default language files and check if the user provided custom language files."""

lang = {}
parent = Path(__file__).parent.absolute()
for element in os.scandir(parent):
if not element.is_file() or not element.name.endswith(f"{language}.json"):
continue

with open(os.path.join(parent, f"{language}.json"), encoding="utf-8") as file:
lang = json.load(file)
break

# check if the user has a custom language file
for root, directories, files in os.walk(os.getcwd()):
for filename in files:
if filename != f"ez_{language}.json":
continue

path = os.path.join(root, filename)
with open(path, encoding="utf-8") as user_file:
user_dic = json.load(user_file)
for key, value in user_dic.items():
lang[key] = value

return lang
29 changes: 0 additions & 29 deletions src/ezcord/internal/languages.py

This file was deleted.

8 changes: 2 additions & 6 deletions src/ezcord/internal/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pathlib import Path
from typing import Literal

from .languages import *
from .language.languages import load_txt


def plural_de(amount: int, word: str, relative: bool = True) -> str:
Expand Down Expand Up @@ -102,11 +102,7 @@ def t(key: str, *args: str):
origin_file = Path(inspect.stack()[n].filename).stem

lang = get_lang()

if lang == "de":
return de[origin_file][key].format(*args)
else:
return en[origin_file][key].format(*args)
return load_txt(lang)[origin_file][key].format(*args)


@cache
Expand Down

0 comments on commit 90650e1

Please sign in to comment.