-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.py
More file actions
67 lines (53 loc) · 2.12 KB
/
translator.py
File metadata and controls
67 lines (53 loc) · 2.12 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from typing import Optional
from discord import Locale, app_commands
from discord.app_commands import TranslationContextLocation
class Translator(app_commands.Translator):
"""
This class is responsable for translating everything interaction related.
"""
def __init__(self, i18n) -> None:
super().__init__()
self.i18n = i18n
@staticmethod
def __get_locale(locale: Locale) -> str | None:
if (
locale == Locale.brazil_portuguese
or locale == Locale.brazil_portuguese.value
):
return "pt"
if locale == Locale.american_english or locale == Locale.british_english:
return "en"
return None
def get_translated(
self, mode: str, context: app_commands.TranslationContext, locale: Locale
) -> str | None:
if mode not in [
"command_name",
"command_description",
"group_name",
"group_description",
]:
return None
cog = context.data.module.replace("cogs.", "")
name = context.data.qualified_name.replace(" ", "_")
return self.i18n.get_app_commands_translation(name, cog, locale, mode)
async def translate(
self,
string: app_commands.locale_str,
locale: Locale,
context: app_commands.TranslationContext,
) -> Optional[str]:
locale = self.__get_locale(locale)
if not self.i18n.check_lang(locale):
return None
match context.location:
case TranslationContextLocation.command_name:
return self.get_translated("command_name", context, locale)
case TranslationContextLocation.command_description:
return self.get_translated("command_description", context, locale)
case TranslationContextLocation.group_name:
return self.get_translated("group_name", context, locale)
case TranslationContextLocation.group_description:
return self.get_translated("group_description", context, locale)
# TODO add the rest of translations
return None