Skip to content

Commit 07a1ae1

Browse files
committed
Localize /gh status embed
1 parent d86a973 commit 07a1ae1

File tree

3 files changed

+78
-11
lines changed

3 files changed

+78
-11
lines changed

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
gh_request,
4545
shorten_sha,
4646
)
47+
from ghutils.utils.l10n import translate_text
4748
from ghutils.utils.strings import truncate_str
4849

4950
logger = logging.getLogger(__name__)
@@ -275,40 +276,45 @@ async def status(
275276
deployment_time_info = _discord_date(info.timestamp)
276277
else:
277278
color = Color.orange()
278-
commit_info = "Unknown"
279-
deployment_time_info = "Unknown"
279+
commit_info = await translate_text(interaction, "commit_unknown")
280+
deployment_time_info = await translate_text(
281+
interaction, "deployment-time_unknown"
282+
)
280283

281284
app_info = await self.bot.application_info()
282285

283286
embed = (
284287
Embed(
285-
title="Bot Status",
288+
title=await translate_text(interaction, "title"),
286289
color=color,
287290
)
288291
.set_footer(text=f"v{VERSION}")
289292
.add_field(
290-
name="Deployed commit",
293+
name=await translate_text(interaction, "commit"),
291294
value=commit_info,
292295
inline=False,
293296
)
294297
.add_field(
295-
name="Deployment time",
298+
name=await translate_text(interaction, "deployment-time"),
296299
value=deployment_time_info,
297300
inline=False,
298301
)
299302
.add_field(
300-
name="Uptime",
303+
name=await translate_text(interaction, "uptime"),
301304
value=_discord_date(self.bot.start_time),
302305
inline=False,
303306
)
304307
.add_field(
305-
name="Install count",
306-
value=textwrap.dedent(f"""\
307-
{app_info.approximate_guild_count} server{"s" if app_info.approximate_guild_count != 1 else ""}
308-
{app_info.approximate_user_install_count} individual user{"s" if app_info.approximate_user_install_count != 1 else ""}"""),
308+
name=await translate_text(interaction, "installs"),
309+
value=await translate_text(
310+
interaction,
311+
"installs_value",
312+
servers=app_info.approximate_guild_count,
313+
users=app_info.approximate_user_install_count,
314+
),
309315
)
310316
.add_field(
311-
name="Commands",
317+
name=await translate_text(interaction, "commands"),
312318
value=f"{ilen(self.bot.tree.walk_commands())}",
313319
)
314320
)

bot/src/ghutils/resources/l10n/en-US/main.ftl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,40 @@ gh-status_description =
8282
gh-status_parameter-description_visibility =
8383
{-parameter-description_visibility}
8484
85+
gh-status_text_title =
86+
Bot Status
87+
88+
gh-status_text_commit =
89+
Deployed commit
90+
91+
gh-status_text_commit_unknown =
92+
Unknown
93+
94+
gh-status_text_deployment-time =
95+
Deployment time
96+
97+
gh-status_text_deployment-time_unknown =
98+
Unknown
99+
100+
gh-status_text_uptime =
101+
Uptime
102+
103+
gh-status_text_installs =
104+
Install count
105+
106+
gh-status_text_installs_value =
107+
{ $servers ->
108+
[one] 1 server
109+
*[other] { $servers } servers
110+
}
111+
{ $users ->
112+
[one] 1 individual user
113+
*[other] { $users } individual users
114+
}
115+
116+
gh-status_text_commands =
117+
Commands
118+
85119
# /gh search
86120

87121
gh-search_description =

bot/src/ghutils/utils/l10n.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
"""Localization helpers."""
22

3+
import logging
34
import re
5+
from typing import Any
46

7+
from discord import Interaction
58
from discord.app_commands import locale_str
69

710
type StrIterable = list[str] | tuple[str, ...]
811

912
_SEPARATOR_PATTERN = re.compile(r"[ _-]+")
1013

1114

15+
logger = logging.getLogger(__name__)
16+
17+
18+
async def translate_text(interaction: Interaction, key: str, **kwargs: Any):
19+
if interaction.command is None:
20+
raise ValueError(
21+
"Attempted to translate command text when interaction.command is None"
22+
)
23+
24+
msg_id = command_text_id(interaction.command.qualified_name, key)
25+
result = await interaction.translate(locale_str(msg_id, **kwargs))
26+
27+
if result is None:
28+
logger.warning(f"Failed to translate string: {msg_id}")
29+
return msg_id
30+
31+
return result
32+
33+
1234
def command_description_id(command: str):
1335
command = _format_identifier(command)
1436
return f"{command}_description"
@@ -28,5 +50,10 @@ def parameter_description(command: str | None, parameter: str):
2850
return locale_str("...", id=parameter_description_id(command, parameter))
2951

3052

53+
def command_text_id(command: str, key: str):
54+
command = _format_identifier(command)
55+
return f"{command}_text_{key}"
56+
57+
3158
def _format_identifier(command: str):
3259
return _SEPARATOR_PATTERN.sub("-", command).replace("/", "")

0 commit comments

Comments
 (0)