Skip to content

Commit 3062394

Browse files
committed
✨ Introduce search_translations function for flexible translation file lookup and update Context type definition to use TypeAlias
1 parent 667a007 commit 3062394

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/custom/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import contextlib
55
from logging import getLogger
6-
from typing import TYPE_CHECKING, Any, override
6+
from typing import TYPE_CHECKING, Any, TypeAlias, override
77

88
import aiocache
99
import discord
@@ -225,6 +225,6 @@ async def on_connect() -> None:
225225

226226
type Bot = CustomBot | CustomRestBot
227227

228-
type Context = ExtContext | ApplicationContext
228+
Context: TypeAlias = ExtContext | ApplicationContext # noqa: UP040
229229

230230
__all__ = ["ApplicationContext", "Bot", "Context", "CustomBot", "CustomRestBot", "ExtContext"]

src/start.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ def __init__(
8686
logger.debug("", exc_info=e)
8787

8888

89+
def search_translations(extension_path: Path) -> Path | None:
90+
if (translation_path := (extension_path / "translations.yml")).exists():
91+
return translation_path
92+
if (translation_path := (extension_path / "translations.yaml")).exists():
93+
return translation_path
94+
# now we also have to search in src/translations
95+
extension_name = extension_path.name
96+
if (translation_path := (Path(__file__).parent / "translations" / f"{extension_name}.yml")).exists():
97+
return translation_path
98+
if (translation_path := (Path(__file__).parent / "translations" / f"{extension_name}.yaml")).exists():
99+
return translation_path
100+
return None
101+
102+
89103
def load_extensions() -> tuple[
90104
"FunctionlistType",
91105
"FunctionlistType",
@@ -128,7 +142,7 @@ def load_extensions() -> tuple[
128142

129143
logger.info(f"Loading extension {name}")
130144
translation: ExtensionTranslation | None = None
131-
if (translation_path := (extension / "translations.yml")).exists():
145+
if (translation_path := (search_translations(extension))) is not None:
132146
try:
133147
translation = i18n.load_translation(str(translation_path))
134148
translations.append(translation)

0 commit comments

Comments
 (0)