Principle:3641397194 wq Gpt6 Astra Runtime UI Localization
| Knowledge Sources | |
|---|---|
| Domains | UI_Localization, Reverse_Engineering |
| Last Updated | 2026-10-26 14:00 GMT |
Overview
Technique for translating a Qt-based desktop application's user interface into another language at runtime, without modifying any application files, by combining paint-layer translation with reversible direct text replacement.
Description
Runtime UI localization translates interface text while the application is running, rather than at compile time or via resource files. The central challenge is that some UI text serves as an identity string (window titles used by `find_widget`, menu paths used by `attach_action_to_menu`) — changing these breaks scripts and plugins. The solution splits translation into two strategies:
- Paint-layer translation (QProxyStyle): override `drawControl` to translate the text on a copy of the `QStyleOption` just before rendering. The real control text stays in the original language, so identity-based lookups keep working, but the user sees translated text.
- Reversible direct replacement: for purely display text (labels, buttons, tooltips, dialog titles), replace the text directly and store the original as a Qt property. On uninstall, restore from the property.
A dictionary (exact match) plus regex rules (for dynamic text like `IDA View-A`) provide the translations. Unmatched strings are logged for later dictionary expansion. The entire localization is reversible: removing the plugin restores all original text.
Usage
Use this principle when you need to:
- Localize a Qt application without source modifications: deploy a plugin rather than rebuilding the app.
- Preserve identity strings: keep window titles and menu paths unchanged for script/plugin compatibility.
- Toggle languages at runtime: allow the user to switch between languages without restarting.
- Incremental dictionary expansion: log unmatched strings to grow the dictionary over time.
Theoretical Basis
The technique leverages the Qt rendering pipeline. `QProxyStyle.drawControl` is called for each visible element with a `QStyleOption` carrying the text. By intercepting at this layer, translation is a display-only transformation:
Where is the translation function. For identity-critical strings, (no change). For display-only strings, with stored for reversal.
A critical safety concern with sip/PyQt5 + Python 3.12: style options are temporary C++ stack objects, and sip may return a mistyped wrapper for a recycled address. Reading `.text` on a mistyped wrapper causes an access violation. The mitigation is a type-code check before reading any field.
Pseudo-code Logic:
# Abstract algorithm (NOT real implementation)
def draw_control(element, option, painter, widget):
if element in TRANSLATABLE_ELEMENTS:
if option.type == expected_type(option): # safety check
translated = translate_menu(option.text)
if translated:
option = copy(option)
option.text = translated
super().draw_control(element, option, painter, widget)
def swap(widget, prop):
original = widget.text
translated = translate(original)
if translated:
widget.set_property(prop, original) # store for restore
widget.text = translated