Skip to content

Commit dac4db9

Browse files
author
PyCompiler ARK++
committed
refactor: Update i18n registration process for dynamic translation support
1 parent 93fddad commit dac4db9

2 files changed

Lines changed: 33 additions & 16 deletions

File tree

Plugins/Cleaner/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ def _load_lang(c: str) -> bool:
202202

203203
def bcasl_register(manager):
204204
manager.add_plugin(PLUGIN)
205-
# Register in INSTANCES for i18n support
205+
# Register in i18n registry for dynamic translation updates
206206
try:
207-
from Plugins_SDK.GeneralContext.i18n import INSTANCES
208-
INSTANCES[META.id] = PLUGIN
207+
from Plugins_SDK.GeneralContext.i18n import register_instance
208+
register_instance(META.id, PLUGIN)
209209
except Exception:
210210
pass

Plugins_SDK/GeneralContext/i18n.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,43 @@
1818
1919
"""
2020

21-
from typing import Any, Union
21+
from typing import Any
2222

23-
try:
24-
from bcasl import BcPluginBase
25-
except ImportError:
26-
BcPluginBase = type(None) # type: ignore
23+
# Keep live Plugins instances to support dynamic interactions (e.g., i18n refresh)
24+
# We avoid importing BCASL types here to keep this utility decoupled.
25+
INSTANCES: dict[str, Any] = {}
2726

28-
try:
29-
from acasl import AcPluginBase
30-
except ImportError:
31-
AcPluginBase = type(None) # type: ignore
3227

28+
def register_instance(plugin_id: str, instance: Any) -> None:
29+
"""Register a live plugin instance for i18n updates.
30+
The instance may optionally implement 'apply_i18n(gui, tr: dict)'.
31+
"""
32+
try:
33+
if plugin_id:
34+
INSTANCES[str(plugin_id)] = instance
35+
except Exception:
36+
pass
3337

3438

35-
# Keep live Plugins Ac/Bc/Ce instances to support dynamic interactions (e.g., i18n refresh)
36-
INSTANCES: dict[str, Any] = {}
39+
def unregister_instance(plugin_id: str) -> None:
40+
"""Unregister a plugin instance when it is disposed/unloaded."""
41+
try:
42+
if plugin_id in INSTANCES:
43+
INSTANCES.pop(plugin_id, None)
44+
except Exception:
45+
pass
46+
47+
48+
def clear_instances() -> None:
49+
"""Clear all tracked instances (e.g., when reloading plugins)."""
50+
try:
51+
INSTANCES.clear()
52+
except Exception:
53+
pass
3754

3855

39-
def apply_translations(gui, tr: dict) -> None:
40-
"""Propagate i18n translations to all Plugin types that expose 'apply_i18n(gui, tr)'."""
56+
def apply_translations(gui: Any, tr: dict) -> None:
57+
"""Propagate i18n translations to all plugins that expose 'apply_i18n(gui, tr)'."""
4158
try:
4259
for plugin_id, inst in list(INSTANCES.items()):
4360
try:

0 commit comments

Comments
 (0)