-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.py
More file actions
47 lines (35 loc) · 1.35 KB
/
translator.py
File metadata and controls
47 lines (35 loc) · 1.35 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
45
46
47
from __future__ import annotations
import json
from typing import Any, cast
import discord
from discord import app_commands
from localizer import tanjunLocalizer
class TanjunTranslator(app_commands.Translator):
def __init__(self) -> None:
self.translations: list[dict[str, object]] = []
self.load_translations()
def load_translations(self) -> None:
try:
with open("locales/de.json", encoding="utf-8") as f:
data: object = json.load(f)
self.translations = cast(list[dict[str, object]], data)
except (FileNotFoundError, json.JSONDecodeError):
self.translations = []
async def translate(
self,
string: app_commands.locale_str,
locale: discord.Locale,
context: app_commands.TranslationContext[Any, Any],
) -> str | None:
if str(locale.value) not in ["de", "de-DE", "en", "en-US", "en-GB"]:
return None
locale_str: str = str(locale.value)
if locale_str in ["en-US", "en-GB"]:
locale_str = "en"
key_str: str = str(string).replace("_", ".")
current: object = tanjunLocalizer.localize(locale_str, key_str)
if isinstance(current, str):
if current == "err: no translation found.":
return None
return current
return None