mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-06-02 02:54:33 +12:00
Reworked translation loading to avoid ambiguity
Issue #619 revealed an unintentional overlap between the auto-detection of the locale and the implicit builtin English language, which resulted in users being unable to select the builtin English language when their device was configured with a locale that we happen to have a translation for. The code has been rewritten to more clearly separate auto-detection and the final fallback that is the builtin English translations.
This commit is contained in:
parent
a2fc4c5ecd
commit
0aa83180de
|
@ -74,30 +74,33 @@ def prepare(tests=False):
|
||||||
appTranslator = QTranslator(app)
|
appTranslator = QTranslator(app)
|
||||||
# By default: locale
|
# By default: locale
|
||||||
|
|
||||||
def extractLocale(filename):
|
|
||||||
# len("manuskript_") = 13, len(".qm") = 3
|
|
||||||
return filename[11:-3] if len(filename) >= 16 else ""
|
|
||||||
|
|
||||||
def tryLoadTranslation(translation, source):
|
def tryLoadTranslation(translation, source):
|
||||||
if appTranslator.load(appPath(os.path.join("i18n", translation))):
|
if appTranslator.load(translation, appPath("i18n")):
|
||||||
app.installTranslator(appTranslator)
|
app.installTranslator(appTranslator)
|
||||||
print(app.tr("Loaded translation from {}: {}.").format(source, translation))
|
print("Loaded translation: {}".format(translation))
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
print(app.tr("Note: No translator found or loaded from {} for locale {}.").
|
print("No translation found or loaded. ({})".format(translation))
|
||||||
format(source, extractLocale(translation)))
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Load translation from settings
|
# Load application translation
|
||||||
translation = ""
|
translation = ""
|
||||||
|
source = "default"
|
||||||
if settings.contains("applicationTranslation"):
|
if settings.contains("applicationTranslation"):
|
||||||
|
# Use the language configured by the user.
|
||||||
translation = settings.value("applicationTranslation")
|
translation = settings.value("applicationTranslation")
|
||||||
print("Found translation in settings:", translation)
|
source = "user setting"
|
||||||
|
else:
|
||||||
if (translation != "" and not tryLoadTranslation(translation, "settings")) or translation == "":
|
# Auto-detect based on system locale.
|
||||||
# load from settings failed or not set, fallback
|
|
||||||
translation = "manuskript_{}.qm".format(locale)
|
translation = "manuskript_{}.qm".format(locale)
|
||||||
tryLoadTranslation(translation, "system locale")
|
source = "system locale"
|
||||||
|
# Note: a missing translation should default to builtin translation,
|
||||||
|
# meaning a missing 'manuskript_en_US.qm' is not a problem at all.
|
||||||
|
|
||||||
|
print("Preferred translation: {} (based on {})".format(("builtin" if translation == "" else translation), source))
|
||||||
|
if (translation != ""): # empty string == 'no translation, use builtin'
|
||||||
|
if not tryLoadTranslation(translation, source):
|
||||||
|
print("Falling back on the builtin translation.")
|
||||||
|
|
||||||
def respectSystemDarkThemeSetting():
|
def respectSystemDarkThemeSetting():
|
||||||
"""Adjusts the Qt theme to match the OS 'dark theme' setting configured by the user."""
|
"""Adjusts the Qt theme to match the OS 'dark theme' setting configured by the user."""
|
||||||
|
|
Loading…
Reference in a new issue