|
18 | 18 |
|
19 | 19 | """ |
20 | 20 |
|
21 | | -from typing import Any, Union |
| 21 | +from typing import Any |
22 | 22 |
|
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] = {} |
27 | 26 |
|
28 | | -try: |
29 | | - from acasl import AcPluginBase |
30 | | -except ImportError: |
31 | | - AcPluginBase = type(None) # type: ignore |
32 | 27 |
|
| 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 |
33 | 37 |
|
34 | 38 |
|
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 |
37 | 54 |
|
38 | 55 |
|
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)'.""" |
41 | 58 | try: |
42 | 59 | for plugin_id, inst in list(INSTANCES.items()): |
43 | 60 | try: |
|
0 commit comments