Skip to content

Commit ecf3943

Browse files
committed
Use translation context to eliminate most decorators
1 parent d86e3cd commit ecf3943

File tree

3 files changed

+49
-38
lines changed

3 files changed

+49
-38
lines changed

bot/src/ghutils/cogs/app_commands/github.py

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@ class GitHubCog(GHUtilsCog, GroupCog, group_name="gh"):
5454

5555
# /gh
5656

57-
@app_commands.command(
58-
description=l10n.command_description("gh issue"),
59-
)
57+
@app_commands.command()
6058
@app_commands.rename(reference="issue")
61-
@l10n.describe(gh_issue=["reference"])
6259
@l10n.describe_common("visibility")
6360
async def issue(
6461
self,
@@ -72,11 +69,8 @@ async def issue(
7269
embed=_create_issue_embed(*reference),
7370
)
7471

75-
@app_commands.command(
76-
description=l10n.command_description("gh pr"),
77-
)
72+
@app_commands.command()
7873
@app_commands.rename(reference="pr")
79-
@l10n.describe(gh_pr=["reference"])
8074
@l10n.describe_common("visibility")
8175
async def pr(
8276
self,
@@ -90,11 +84,8 @@ async def pr(
9084
embed=_create_issue_embed(*reference),
9185
)
9286

93-
@app_commands.command(
94-
description=l10n.command_description("gh commit"),
95-
)
87+
@app_commands.command()
9688
@app_commands.rename(reference="commit")
97-
@l10n.describe(gh_commit=["reference"])
9889
@l10n.describe_common("visibility")
9990
async def commit(
10091
self,
@@ -135,10 +126,7 @@ async def commit(
135126

136127
await respond_with_visibility(interaction, visibility, embed=embed)
137128

138-
@app_commands.command(
139-
description=l10n.command_description("gh repo"),
140-
)
141-
@l10n.describe(gh_repo=["repo"])
129+
@app_commands.command()
142130
@l10n.describe_common("visibility")
143131
async def repo(
144132
self,
@@ -173,9 +161,7 @@ async def repo(
173161

174162
await respond_with_visibility(interaction, visibility, embed=embed)
175163

176-
@app_commands.command(
177-
description=l10n.command_description("gh login"),
178-
)
164+
@app_commands.command()
179165
async def login(self, interaction: Interaction):
180166
user_id = interaction.user.id
181167
login_id = str(uuid.uuid4())
@@ -197,9 +183,7 @@ async def login(self, interaction: Interaction):
197183
ephemeral=True,
198184
)
199185

200-
@app_commands.command(
201-
description=l10n.command_description("gh logout"),
202-
)
186+
@app_commands.command()
203187
async def logout(self, interaction: Interaction):
204188
with self.bot.db_session() as session:
205189
# TODO: this should delete the authorization too, but idk how
@@ -218,9 +202,7 @@ async def logout(self, interaction: Interaction):
218202
ephemeral=True,
219203
)
220204

221-
@app_commands.command(
222-
description=l10n.command_description("gh status"),
223-
)
205+
@app_commands.command()
224206
@l10n.describe_common("visibility")
225207
async def status(
226208
self,
@@ -278,11 +260,8 @@ async def status(
278260

279261
await respond_with_visibility(interaction, visibility, embed=embed)
280262

281-
class Search(SubGroup, description=l10n.command_description("gh search")):
282-
@app_commands.command(
283-
description=l10n.command_description("gh search files"),
284-
)
285-
@l10n.describe(gh_search_files=["repo", "query", "ref", "exact", "limit"])
263+
class Search(SubGroup):
264+
@app_commands.command()
286265
@l10n.describe_common("visibility")
287266
async def files(
288267
self,

bot/src/ghutils/core/translator.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
from contextlib import ExitStack
22

33
from discord import Locale
4-
from discord.app_commands import TranslationContextTypes, Translator, locale_str
4+
from discord.app_commands import (
5+
TranslationContextLocation,
6+
TranslationContextTypes,
7+
Translator,
8+
locale_str,
9+
)
510
from fluent.runtime import FluentLocalization, FluentResourceLoader
611

712
from ghutils.resources import load_resource_dir
13+
from ghutils.utils.l10n import command_description_id, parameter_description_id
814

915

1016
class GHUtilsTranslator(Translator):
@@ -32,5 +38,22 @@ async def translate(
3238
locale: Locale,
3339
context: TranslationContextTypes,
3440
) -> str | None:
35-
msg_id = string.extras.get("id", string.message)
36-
return self.l10n[locale].format_value(msg_id, string.extras)
41+
match string.extras:
42+
case {"id": str(msg_id)}:
43+
pass
44+
case _:
45+
match context.location:
46+
case TranslationContextLocation.command_description:
47+
msg_id = command_description_id(context.data.qualified_name)
48+
case TranslationContextLocation.parameter_description:
49+
msg_id = parameter_description_id(
50+
command=context.data.command.qualified_name,
51+
parameter=context.data.name,
52+
)
53+
case _:
54+
msg_id = string.message
55+
56+
result = self.l10n[locale].format_value(msg_id, string.extras)
57+
if result == msg_id:
58+
return string.message
59+
return result

bot/src/ghutils/utils/l10n.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,23 @@
1111
_SEPARATOR_PATTERN = re.compile(r"[ _-]+")
1212

1313

14+
def command_description_id(command: str):
15+
command = _format_identifier(command)
16+
return f"command-description_{command}"
17+
18+
1419
def command_description(command: str):
15-
command = _format_command(command)
16-
return locale_str(f"command-description_{command}")
20+
return locale_str("...", id=command_description_id(command))
21+
22+
23+
def parameter_description_id(command: str | None, parameter: str):
24+
command = _format_identifier(command or "common")
25+
parameter = _format_identifier(parameter)
26+
return f"parameter-description_{command}_{parameter}"
1727

1828

1929
def parameter_description(command: str | None, parameter: str):
20-
command = _format_command(command or "common")
21-
return locale_str(f"parameter-description_{command}_{parameter}")
30+
return locale_str("...", id=parameter_description_id(command, parameter))
2231

2332

2433
def describe_common(*parameters: str):
@@ -44,5 +53,5 @@ def describe(
4453
})
4554

4655

47-
def _format_command(command: str):
56+
def _format_identifier(command: str):
4857
return _SEPARATOR_PATTERN.sub("-", command).replace("/", "")

0 commit comments

Comments
 (0)