Skip to content

Commit d86e3cd

Browse files
committed
Localize /gh
1 parent 17b0498 commit d86e3cd

File tree

4 files changed

+166
-36
lines changed

4 files changed

+166
-36
lines changed

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

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
UserGitHubTokens,
2727
UserLogin,
2828
)
29+
from ghutils.utils import l10n
2930
from ghutils.utils.discord.embeds import set_embed_author
3031
from ghutils.utils.discord.references import (
3132
CommitReference,
@@ -53,48 +54,54 @@ class GitHubCog(GHUtilsCog, GroupCog, group_name="gh"):
5354

5455
# /gh
5556

56-
@app_commands.command()
57+
@app_commands.command(
58+
description=l10n.command_description("gh issue"),
59+
)
5760
@app_commands.rename(reference="issue")
61+
@l10n.describe(gh_issue=["reference"])
62+
@l10n.describe_common("visibility")
5863
async def issue(
5964
self,
6065
interaction: Interaction,
6166
reference: IssueReference,
6267
visibility: MessageVisibility = "private",
6368
):
64-
"""Get a link to a GitHub issue."""
65-
6669
await respond_with_visibility(
6770
interaction,
6871
visibility,
6972
embed=_create_issue_embed(*reference),
7073
)
7174

72-
@app_commands.command()
75+
@app_commands.command(
76+
description=l10n.command_description("gh pr"),
77+
)
7378
@app_commands.rename(reference="pr")
79+
@l10n.describe(gh_pr=["reference"])
80+
@l10n.describe_common("visibility")
7481
async def pr(
7582
self,
7683
interaction: Interaction,
7784
reference: PRReference,
7885
visibility: MessageVisibility = "private",
7986
):
80-
"""Get a link to a GitHub pull request."""
81-
8287
await respond_with_visibility(
8388
interaction,
8489
visibility,
8590
embed=_create_issue_embed(*reference),
8691
)
8792

88-
@app_commands.command()
93+
@app_commands.command(
94+
description=l10n.command_description("gh commit"),
95+
)
8996
@app_commands.rename(reference="commit")
97+
@l10n.describe(gh_commit=["reference"])
98+
@l10n.describe_common("visibility")
9099
async def commit(
91100
self,
92101
interaction: Interaction,
93102
reference: CommitReference,
94103
visibility: MessageVisibility = "private",
95104
):
96-
"""Get a link to a GitHub commit."""
97-
98105
repo, commit = reference
99106

100107
async with self.bot.github_app(interaction) as (github, _):
@@ -128,15 +135,17 @@ async def commit(
128135

129136
await respond_with_visibility(interaction, visibility, embed=embed)
130137

131-
@app_commands.command()
138+
@app_commands.command(
139+
description=l10n.command_description("gh repo"),
140+
)
141+
@l10n.describe(gh_repo=["repo"])
142+
@l10n.describe_common("visibility")
132143
async def repo(
133144
self,
134145
interaction: Interaction,
135146
repo: FullRepositoryOption,
136147
visibility: MessageVisibility = "private",
137148
):
138-
"""Get a link to a GitHub repository."""
139-
140149
async with self.bot.github_app(interaction) as (github, _):
141150
result = await github.async_graphql(
142151
"""
@@ -164,10 +173,10 @@ async def repo(
164173

165174
await respond_with_visibility(interaction, visibility, embed=embed)
166175

167-
@app_commands.command()
176+
@app_commands.command(
177+
description=l10n.command_description("gh login"),
178+
)
168179
async def login(self, interaction: Interaction):
169-
"""Authorize GitHub Utils to make requests on behalf of your GitHub account."""
170-
171180
user_id = interaction.user.id
172181
login_id = str(uuid.uuid4())
173182

@@ -188,10 +197,10 @@ async def login(self, interaction: Interaction):
188197
ephemeral=True,
189198
)
190199

191-
@app_commands.command()
200+
@app_commands.command(
201+
description=l10n.command_description("gh logout"),
202+
)
192203
async def logout(self, interaction: Interaction):
193-
"""Remove your GitHub account from GitHub Utils."""
194-
195204
with self.bot.db_session() as session:
196205
# TODO: this should delete the authorization too, but idk how
197206
# https://docs.github.com/en/rest/apps/oauth-applications?apiVersion=2022-11-28#delete-an-app-authorization
@@ -209,14 +218,15 @@ async def logout(self, interaction: Interaction):
209218
ephemeral=True,
210219
)
211220

212-
@app_commands.command()
221+
@app_commands.command(
222+
description=l10n.command_description("gh status"),
223+
)
224+
@l10n.describe_common("visibility")
213225
async def status(
214226
self,
215227
interaction: Interaction,
216228
visibility: MessageVisibility = "private",
217229
):
218-
"""Show information about GitHub Utils."""
219-
220230
if info := self.env.deployment:
221231
color = Color.green()
222232
commit_url = f"https://github.com/object-Object/discord-github-utils/commit/{info.commit_sha}"
@@ -268,10 +278,12 @@ async def status(
268278

269279
await respond_with_visibility(interaction, visibility, embed=embed)
270280

271-
class Search(SubGroup):
272-
"""Search for things on GitHub."""
273-
274-
@app_commands.command()
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"])
286+
@l10n.describe_common("visibility")
275287
async def files(
276288
self,
277289
interaction: Interaction,
@@ -282,15 +294,6 @@ async def files(
282294
limit: Range[int, 1, 25] = 5,
283295
visibility: MessageVisibility = "private",
284296
):
285-
"""Search for files in a repository by name.
286-
287-
Args:
288-
ref: Branch name, tag name, or commit to search in. Defaults to the
289-
default branch of the repo.
290-
exact: If true, use exact search; otherwise use fuzzy search.
291-
limit: Maximum number of results to show.
292-
"""
293-
294297
async with self.bot.github_app(interaction) as (github, state):
295298
if state != LoginState.LOGGED_IN:
296299
raise NotLoggedInError()

bot/src/ghutils/core/translator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ async def translate(
3232
locale: Locale,
3333
context: TranslationContextTypes,
3434
) -> str | None:
35-
l10n = self.l10n[locale]
36-
return l10n.format_value(string.extras["id"])
35+
msg_id = string.extras.get("id", string.message)
36+
return self.l10n[locale].format_value(msg_id, string.extras)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# command descriptions: command-description_{command}
2+
# |--------------------------------------------------------------------------------------------------|
3+
4+
# parameter descriptions: parameter-description_{command}_{parameter}
5+
# |--------------------------------------------------------------------------------------------------|
6+
7+
# common parameters
8+
9+
parameter-description_common_visibility =
10+
Whether the message should be visible to everyone, or just you.
11+
12+
# /gh issue
13+
14+
command-description_gh-issue =
15+
Get a link to a GitHub issue.
16+
17+
parameter-description_gh-issue_reference =
18+
Issue to look up (`owner/repo#123` or `#123`). Use `/gh login` to get autocomplete.
19+
20+
# /gh pr
21+
22+
command-description_gh-pr =
23+
Get a link to a GitHub pull request.
24+
25+
parameter-description_gh-pr_reference =
26+
Pull request to look up (`owner/repo#123` or `#123`). Use `/gh login` to get autocomplete.
27+
28+
# /gh commit
29+
30+
command-description_gh-commit =
31+
Get a link to a GitHub commit.
32+
33+
parameter-description_gh-commit_reference =
34+
Commit SHA to look up (`owner/repo@sha` or `@sha`). Use `/gh login` to get autocomplete.
35+
36+
# /gh repo
37+
38+
command-description_gh-repo =
39+
Get a link to a GitHub repository.
40+
41+
# /gh login
42+
43+
command-description_gh-login =
44+
Authorize GitHub Utils to make requests on behalf of your GitHub account.
45+
46+
# /gh logout
47+
48+
command-description_gh-logout =
49+
Remove your GitHub account from GitHub Utils.
50+
51+
# /gh status
52+
53+
command-description_gh-status =
54+
Show information about GitHub Utils.
55+
56+
# /gh search
57+
58+
command-description_gh-search =
59+
Search for things on GitHub.
60+
61+
# /gh search files
62+
63+
command-description_gh-search-files =
64+
Search for files in a repository by name.
65+
66+
parameter-description_gh-search-files_repo =
67+
Repository to search in (`owner/repo`).
68+
69+
parameter-description_gh-search-files_query =
70+
Filename to search for.
71+
72+
parameter-description_gh-search-files_ref =
73+
Branch name, tag name, or commit to search in. Defaults to the default branch of the repo.
74+
75+
parameter-description_gh-search-files_exact =
76+
If true, use exact search; otherwise use fuzzy search.
77+
78+
parameter-description_gh-search-files_limit =
79+
Maximum number of results to show.

bot/src/ghutils/utils/l10n.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Localization helpers."""
2+
3+
import re
4+
from typing import Mapping
5+
6+
from discord import app_commands
7+
from discord.app_commands import locale_str
8+
9+
type StrIterable = list[str] | tuple[str, ...]
10+
11+
_SEPARATOR_PATTERN = re.compile(r"[ _-]+")
12+
13+
14+
def command_description(command: str):
15+
command = _format_command(command)
16+
return locale_str(f"command-description_{command}")
17+
18+
19+
def parameter_description(command: str | None, parameter: str):
20+
command = _format_command(command or "common")
21+
return locale_str(f"parameter-description_{command}_{parameter}")
22+
23+
24+
def describe_common(*parameters: str):
25+
return describe(common=parameters)
26+
27+
28+
def describe(
29+
dict_args: Mapping[str, StrIterable] | None = None,
30+
**kwargs: StrIterable,
31+
):
32+
"""Localize app command parameters.
33+
34+
Key: command to localize parameters for.
35+
36+
Value: parameter names.
37+
"""
38+
if dict_args:
39+
kwargs |= dict_args
40+
return app_commands.describe(**{
41+
parameter: parameter_description(command, parameter)
42+
for command, parameters in kwargs.items()
43+
for parameter in parameters
44+
})
45+
46+
47+
def _format_command(command: str):
48+
return _SEPARATOR_PATTERN.sub("-", command).replace("/", "")

0 commit comments

Comments
 (0)