Skip to content

Commit 17b0498

Browse files
committed
Set up GHUtilsTranslator using Fluent
1 parent 90fbee5 commit 17b0498

File tree

8 files changed

+73
-0
lines changed

8 files changed

+73
-0
lines changed

bot/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies = [
1717
"githubkit[auth-app]>=0.11.8",
1818
"pfzy>=0.3.4",
1919
"more-itertools>=10.5.0",
20+
"fluent-runtime>=0.4.0",
2021
]
2122

2223
[tool.rye]

bot/src/ghutils/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ async def main():
1111
env = GHUtilsEnv.get()
1212
async with GHUtilsBot(env) as bot:
1313
create_db_and_tables(bot.engine)
14+
await bot.load_translator()
1415
await bot.load_cogs()
1516
await bot.start(env.token.get_secret_value())
1617

bot/src/ghutils/core/bot.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ghutils.utils.imports import iter_modules
1717

1818
from .env import GHUtilsEnv
19+
from .translator import GHUtilsTranslator
1920
from .tree import GHUtilsCommandTree
2021
from .types import LoginState
2122

@@ -60,6 +61,10 @@ def db_session_of(cls, interaction: Interaction):
6061
def github_app_of(cls, interaction: Interaction):
6162
return cls.of(interaction).github_app(interaction)
6263

64+
async def load_translator(self):
65+
logger.info("Loading translator")
66+
await self.tree.set_translator(GHUtilsTranslator())
67+
6368
async def load_cogs(self):
6469
for cog in iter_modules(cogs, skip_internal=True):
6570
try:

bot/src/ghutils/core/translator.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from contextlib import ExitStack
2+
3+
from discord import Locale
4+
from discord.app_commands import TranslationContextTypes, Translator, locale_str
5+
from fluent.runtime import FluentLocalization, FluentResourceLoader
6+
7+
from ghutils.resources import load_resource_dir
8+
9+
10+
class GHUtilsTranslator(Translator):
11+
async def load(self) -> None:
12+
self.exit_stack = ExitStack()
13+
14+
path = self.exit_stack.enter_context(load_resource_dir("l10n"))
15+
loader = FluentResourceLoader(path.as_posix() + "/{locale}")
16+
17+
self.l10n = {
18+
locale: FluentLocalization(
19+
locales=[locale.value, "en-US"],
20+
resource_ids=["main.ftl"],
21+
resource_loader=loader,
22+
)
23+
for locale in Locale
24+
}
25+
26+
async def unload(self) -> None:
27+
self.exit_stack.close()
28+
29+
async def translate(
30+
self,
31+
string: locale_str,
32+
locale: Locale,
33+
context: TranslationContextTypes,
34+
) -> str | None:
35+
l10n = self.l10n[locale]
36+
return l10n.format_value(string.extras["id"])

bot/src/ghutils/resources/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import contextmanager
12
from importlib import resources
23

34

@@ -8,3 +9,10 @@ def get_resource(name: str):
89

910
def load_resource(name: str, encoding: str = "utf-8"):
1011
return get_resource(name).read_text(encoding)
12+
13+
14+
@contextmanager
15+
def load_resource_dir(name: str):
16+
resource = get_resource(name)
17+
with resources.as_file(resource) as path:
18+
yield path

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

Whitespace-only changes.

requirements-dev.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ anyio==4.4.0
2727
attrs==23.2.0
2828
# via aiohttp
2929
# via cattrs
30+
# via fluent-runtime
3031
# via jsii
3132
aws-cdk-asset-awscli-v1==2.2.202
3233
# via aws-cdk-lib
@@ -43,6 +44,8 @@ aws-cdk-lib==2.158.0
4344
# via aws-cdk-github-oidc
4445
# via ghutils-infrastructure
4546
# via object-ci
47+
babel==2.16.0
48+
# via fluent-runtime
4649
cattrs==23.2.3
4750
# via jsii
4851
certifi==2024.6.2
@@ -77,6 +80,10 @@ fastapi-cli==0.0.4
7780
# via fastapi
7881
filelock==3.15.4
7982
# via virtualenv
83+
fluent-runtime==0.4.0
84+
# via ghutils-bot
85+
fluent-syntax==0.19.0
86+
# via fluent-runtime
8087
frozenlist==1.4.1
8188
# via aiohttp
8289
# via aiosignal
@@ -172,6 +179,8 @@ python-dotenv==1.0.1
172179
# via uvicorn
173180
python-multipart==0.0.9
174181
# via fastapi
182+
pytz==2024.2
183+
# via fluent-runtime
175184
pyyaml==6.0.1
176185
# via pre-commit
177186
# via uvicorn
@@ -204,6 +213,8 @@ typer==0.12.3
204213
# via fastapi-cli
205214
typing-extensions==4.12.2
206215
# via fastapi
216+
# via fluent-runtime
217+
# via fluent-syntax
207218
# via githubkit
208219
# via hishel
209220
# via jsii

requirements.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ anyio==4.4.0
2727
attrs==23.2.0
2828
# via aiohttp
2929
# via cattrs
30+
# via fluent-runtime
3031
# via jsii
3132
aws-cdk-asset-awscli-v1==2.2.202
3233
# via aws-cdk-lib
@@ -43,6 +44,8 @@ aws-cdk-lib==2.158.0
4344
# via aws-cdk-github-oidc
4445
# via ghutils-infrastructure
4546
# via object-ci
47+
babel==2.16.0
48+
# via fluent-runtime
4649
cattrs==23.2.3
4750
# via jsii
4851
certifi==2024.6.2
@@ -71,6 +74,10 @@ fastapi==0.111.0
7174
# via ghutils-bot
7275
fastapi-cli==0.0.4
7376
# via fastapi
77+
fluent-runtime==0.4.0
78+
# via ghutils-bot
79+
fluent-syntax==0.19.0
80+
# via fluent-runtime
7481
frozenlist==1.4.1
7582
# via aiohttp
7683
# via aiosignal
@@ -159,6 +166,8 @@ python-dotenv==1.0.1
159166
# via uvicorn
160167
python-multipart==0.0.9
161168
# via fastapi
169+
pytz==2024.2
170+
# via fluent-runtime
162171
pyyaml==6.0.1
163172
# via uvicorn
164173
rich==13.7.1
@@ -189,6 +198,8 @@ typer==0.12.3
189198
# via fastapi-cli
190199
typing-extensions==4.12.2
191200
# via fastapi
201+
# via fluent-runtime
202+
# via fluent-syntax
192203
# via githubkit
193204
# via hishel
194205
# via jsii

0 commit comments

Comments
 (0)