Skip to content

Commit

Permalink
Set up GHUtilsTranslator using Fluent
Browse files Browse the repository at this point in the history
  • Loading branch information
object-Object committed Nov 28, 2024
1 parent 90fbee5 commit 17b0498
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions bot/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies = [
"githubkit[auth-app]>=0.11.8",
"pfzy>=0.3.4",
"more-itertools>=10.5.0",
"fluent-runtime>=0.4.0",
]

[tool.rye]
Expand Down
1 change: 1 addition & 0 deletions bot/src/ghutils/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async def main():
env = GHUtilsEnv.get()
async with GHUtilsBot(env) as bot:
create_db_and_tables(bot.engine)
await bot.load_translator()
await bot.load_cogs()
await bot.start(env.token.get_secret_value())

Expand Down
5 changes: 5 additions & 0 deletions bot/src/ghutils/core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ghutils.utils.imports import iter_modules

from .env import GHUtilsEnv
from .translator import GHUtilsTranslator
from .tree import GHUtilsCommandTree
from .types import LoginState

Expand Down Expand Up @@ -60,6 +61,10 @@ def db_session_of(cls, interaction: Interaction):
def github_app_of(cls, interaction: Interaction):
return cls.of(interaction).github_app(interaction)

async def load_translator(self):
logger.info("Loading translator")
await self.tree.set_translator(GHUtilsTranslator())

async def load_cogs(self):
for cog in iter_modules(cogs, skip_internal=True):
try:
Expand Down
36 changes: 36 additions & 0 deletions bot/src/ghutils/core/translator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from contextlib import ExitStack

from discord import Locale
from discord.app_commands import TranslationContextTypes, Translator, locale_str
from fluent.runtime import FluentLocalization, FluentResourceLoader

from ghutils.resources import load_resource_dir


class GHUtilsTranslator(Translator):
async def load(self) -> None:
self.exit_stack = ExitStack()

path = self.exit_stack.enter_context(load_resource_dir("l10n"))
loader = FluentResourceLoader(path.as_posix() + "/{locale}")

self.l10n = {
locale: FluentLocalization(
locales=[locale.value, "en-US"],
resource_ids=["main.ftl"],
resource_loader=loader,
)
for locale in Locale
}

async def unload(self) -> None:
self.exit_stack.close()

async def translate(
self,
string: locale_str,
locale: Locale,
context: TranslationContextTypes,
) -> str | None:
l10n = self.l10n[locale]
return l10n.format_value(string.extras["id"])
8 changes: 8 additions & 0 deletions bot/src/ghutils/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import contextmanager
from importlib import resources


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

def load_resource(name: str, encoding: str = "utf-8"):
return get_resource(name).read_text(encoding)


@contextmanager
def load_resource_dir(name: str):
resource = get_resource(name)
with resources.as_file(resource) as path:
yield path
Empty file.
11 changes: 11 additions & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ anyio==4.4.0
attrs==23.2.0
# via aiohttp
# via cattrs
# via fluent-runtime
# via jsii
aws-cdk-asset-awscli-v1==2.2.202
# via aws-cdk-lib
Expand All @@ -43,6 +44,8 @@ aws-cdk-lib==2.158.0
# via aws-cdk-github-oidc
# via ghutils-infrastructure
# via object-ci
babel==2.16.0
# via fluent-runtime
cattrs==23.2.3
# via jsii
certifi==2024.6.2
Expand Down Expand Up @@ -77,6 +80,10 @@ fastapi-cli==0.0.4
# via fastapi
filelock==3.15.4
# via virtualenv
fluent-runtime==0.4.0
# via ghutils-bot
fluent-syntax==0.19.0
# via fluent-runtime
frozenlist==1.4.1
# via aiohttp
# via aiosignal
Expand Down Expand Up @@ -172,6 +179,8 @@ python-dotenv==1.0.1
# via uvicorn
python-multipart==0.0.9
# via fastapi
pytz==2024.2
# via fluent-runtime
pyyaml==6.0.1
# via pre-commit
# via uvicorn
Expand Down Expand Up @@ -204,6 +213,8 @@ typer==0.12.3
# via fastapi-cli
typing-extensions==4.12.2
# via fastapi
# via fluent-runtime
# via fluent-syntax
# via githubkit
# via hishel
# via jsii
Expand Down
11 changes: 11 additions & 0 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ anyio==4.4.0
attrs==23.2.0
# via aiohttp
# via cattrs
# via fluent-runtime
# via jsii
aws-cdk-asset-awscli-v1==2.2.202
# via aws-cdk-lib
Expand All @@ -43,6 +44,8 @@ aws-cdk-lib==2.158.0
# via aws-cdk-github-oidc
# via ghutils-infrastructure
# via object-ci
babel==2.16.0
# via fluent-runtime
cattrs==23.2.3
# via jsii
certifi==2024.6.2
Expand Down Expand Up @@ -71,6 +74,10 @@ fastapi==0.111.0
# via ghutils-bot
fastapi-cli==0.0.4
# via fastapi
fluent-runtime==0.4.0
# via ghutils-bot
fluent-syntax==0.19.0
# via fluent-runtime
frozenlist==1.4.1
# via aiohttp
# via aiosignal
Expand Down Expand Up @@ -159,6 +166,8 @@ python-dotenv==1.0.1
# via uvicorn
python-multipart==0.0.9
# via fastapi
pytz==2024.2
# via fluent-runtime
pyyaml==6.0.1
# via uvicorn
rich==13.7.1
Expand Down Expand Up @@ -189,6 +198,8 @@ typer==0.12.3
# via fastapi-cli
typing-extensions==4.12.2
# via fastapi
# via fluent-runtime
# via fluent-syntax
# via githubkit
# via hishel
# via jsii
Expand Down

0 comments on commit 17b0498

Please sign in to comment.