Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a hack for proper locale initialization #2017

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

CSharperMantle
Copy link
Contributor

@CSharperMantle CSharperMantle commented Jan 31, 2022

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

MOTD = [ # Candidate phrases for the message of the day (MOTD).
_("Hello, World!"),
_(
"This editor is free software written in Python. You can modify it, "
"add features or fix bugs if you like."
),
_("This editor is called Mu (you say it 'mew' or 'moo')."),
_("Google, Facebook, NASA, Pixar, Disney and many more use Python."),
_(
"Programming is a form of magic. Learn to cast the right spells with "
"code and you'll be a wizard."
),
_(

mu/mu/modes/base.py

Lines 192 to 210 in 5a5d772

class BaseMode(QObject):
"""
Represents the common aspects of a mode.
"""
name = "UNNAMED MODE"
short_name = "UNDEFINED_MODE"
description = "DESCRIPTION NOT AVAILABLE."
icon = "help"
repl = False
plotter = False
is_debugger = False
has_debugger = False
save_timeout = 5 #: Number of seconds to wait before saving work.
builtins = None #: Symbols to assume as builtins when checking code style.
file_extensions = []
module_names = MODULE_NAMES
code_template = _("# Write your code here :-)")

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.

  1. app.py is evaluated. Modules are imported.
  2. logic.py, modes/*.py and so on are evaluated.
  3. Static variables are evaluated when the interpreter compiles their definitions. At this time, the locale settings are platform default, which is not consistent with user settings.
  4. app.run() calls editor.restore_session(). The correct locale settings are refreshed into runtime environment.
  5. The window is painted and other localized strings are loaded with the correct locale.
  6. The inconsistency is observed between strings loaded from static variables and any other ways.

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

if not self._view.tab_count:
py = self.modes[self.mode].code_template + NEWLINE
tab = self._view.add_tab(
None, py, self.modes[self.mode].api(), NEWLINE
)
tab.setCursorPosition(len(py.split(NEWLINE)), 0)
logger.info("Starting with blank file.")

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.

@CSharperMantle
Copy link
Contributor Author

This patch is rebased onto the latest master.

@carlosperate carlosperate added i18n internationalisation translations labels Feb 22, 2022
@xbecas
Copy link
Collaborator

xbecas commented Mar 5, 2022

Related to #1849 also!

@xbecas xbecas added the interface Mu's UI - buttons, text, window, etc. label Mar 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
i18n internationalisation interface Mu's UI - buttons, text, window, etc. translations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistency between UI and status bar language
3 participants