-
Notifications
You must be signed in to change notification settings - Fork 1
/
translator.py
55 lines (51 loc) · 1.68 KB
/
translator.py
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
48
49
50
51
52
53
54
55
import i18n
from discord import Locale
from typing import Optional
from discord import app_commands
from discord.app_commands import TranslationContextLocation
locales = {
Locale.american_english: "en",
Locale.british_english: "en",
Locale.brazil_portuguese: "pt",
Locale.chinese: "zh",
Locale.croatian: "hr",
Locale.dutch: "nl",
Locale.french: "fr",
Locale.german: "de",
Locale.japanese: "ja",
Locale.polish: "pl",
Locale.spain_spanish: "es",
Locale.swedish: "sv",
}
class TakoTranslator(app_commands.Translator):
async def load(self):
i18n.set("filename_format", "{locale}.{format}")
i18n.set("fallback", "en")
i18n.load_path.append("i18n")
async def translate(
self,
string: app_commands.locale_str,
locale: Locale,
context: app_commands.TranslationContextTypes,
) -> Optional[str]:
try:
locale_str = locales[locale]
except KeyError:
return None
other = False
if context.location == TranslationContextLocation.other:
other = True
try:
translation = i18n.t(
f"{'' if other else str(context.location).replace('TranslationContextLocation.', '')}.{string.message}",
locale=locale_str,
**string.extras,
)
except:
translation = f"{str(context.location).replace('TranslationContextLocation.', '')}.{string.message}"
return (
translation
if not translation
== f"{'' if other else str(context.location).replace('TranslationContextLocation.', '')}.{string.message}"
else None
)