Provide a hack for proper locale initialization #2017
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is submitted in the hope to resolve #2008.
Background. The localized strings (especially strings wrapped by a function call of
_("some_str")
) in Mu are generally created and referenced in two ways: 1) via static class or global variables, and 2) via Qt widgets. Some examples of static variables is listed below.mu/mu/logic.py
Lines 89 to 101 in 5a5d772
mu/mu/modes/base.py
Lines 192 to 210 in 5a5d772
The latter works perfectly when the locale is changed, as a restart of the app with a call to
editor.restore_session()
is enough to refresh the translations.However the former one does not work in this way. This behavior is due to the fact that class and global variables are evaluated when their containing scripts are executed. This is well ahead of the time when
editor.restore_session()
refreshes the locale configuration in user settings. Thus these static strings are not loaded properly. This process can be summarized as follows.app.py
is evaluated. Modules are imported.logic.py
,modes/*.py
and so on are evaluated.app.run()
callseditor.restore_session()
. The correct locale settings are refreshed into runtime environment.Note that the following lines DO NOT load localized strings as expected since
self.modes[self.mode].code_template
is evaluated at step 3 aforementioned when the locale settings have not been refreshed from the user settings yet.mu/mu/logic.py
Lines 956 to 963 in 5a5d772
Solutions. In order to enforce a refresh before any localized strings are evaluated, the session restoring process is carried out before critical modules are loaded.
https://github.com/CSharperMantle/mu/blob/9a907af9164dee71370ffdd0db016a5325e018d1/mu/app.py#L48-L63
Note that this solution might not be the best one due to significant ugliness it causes, but it is the one that has impact on only 1 file with no other changes required. Other solutions might include 1) deferring call to
_("some_str")
to some point after the session is restored, or 2) load all session-related data before any UI-related scripts are evaluated.