diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.cpp | 24 | ||||
| -rw-r--r-- | src/config.h | 12 | ||||
| -rw-r--r-- | src/main.cpp | 28 |
3 files changed, 55 insertions, 9 deletions
diff --git a/src/config.cpp b/src/config.cpp index 266ed39..600c558 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -196,6 +196,30 @@ void Config::load(const QString &path) m_startupAccount = settings.value(QStringLiteral("startup_account")).toString().trimmed(); + // Interface language. "system" is spelled out so the default can be written + // down rather than only expressed by deleting the key. + // + // Validated here rather than left to whether a translation loads, because + // those are different questions and only one of them is an error. QLocale + // accepts anything and degrades an unrecognised name to C, so `language = + // itallian` would load no translation and look exactly like asking for + // English. Meanwhile `language = en_US` legitimately loads nothing, since + // English is the source language and ships no .qm. Checking the NAME + // separates the typo from the deliberate choice. + const QString language = + settings.value(QStringLiteral("language")).toString().trimmed(); + if (!language.isEmpty() + && language.compare(QStringLiteral("system"), Qt::CaseInsensitive) != 0) { + if (QLocale(language).language() == QLocale::C) { + addProblem(tr("Language '%1' is not a locale name; using the " + "system language. Expected something like 'it' or " + "'it_IT'.") + .arg(language)); + } else { + m_language = language; + } + } + const QVariant zoom = settings.value(QStringLiteral("message_zoom")); if (zoom.isValid()) { bool ok = false; diff --git a/src/config.h b/src/config.h index d1f8e65..70f7181 100644 --- a/src/config.h +++ b/src/config.h @@ -292,6 +292,17 @@ public: /// the same fixed string on every card rather than failing visibly. QString dateFormat() const { return m_dateFormat; } + /// Interface language, or empty to follow the environment. + /// + /// A locale name, short ("it") or full ("it_IT"); Qt resolves the short + /// form to a country. `system` reads as empty, so a user can write the + /// default down rather than having to delete the key to get it back. + /// + /// Validated at load, because an unrecognised name does NOT fail: QLocale + /// degrades it to C, which then loads no translation and is indistinguishable + /// from asking for English on purpose. A typo would otherwise be silent. + QString language() const { return m_language; } + /// The saved query to open at startup, by name. Falls back to "Unread" /// when unset, and to the first saved query when no query by that name /// exists: [queries] is read through childKeys(), which sorts @@ -404,6 +415,7 @@ private: int m_toolbarIconSize = 24; QString m_notmuchConfig; QString m_dateFormat; + QString m_language; qreal m_messageZoom = 1.0; bool m_completionOnFocus = false; int m_markReadDelayMs = 2000; diff --git a/src/main.cpp b/src/main.cpp index 59f39be..2ed8057 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -89,14 +89,27 @@ int main(int argc, char *argv[]) app.setWindowIcon(QIcon(QStringLiteral(":/icons/qtmaildir.svg"))); app.setDesktopFileName(QStringLiteral("qtmaildir")); + Config config; + config.load(Config::defaultPath()); + // On main's stack deliberately: a QTranslator must outlive exec(), and one // scoped to a helper function unloads on return, silently reverting every - // string to English. Installed before Config is loaded, because config - // warnings are generated at load time and are themselves translated. + // string to English. + // + // Loaded AFTER Config, because [general] language overrides the + // environment. The cost is that config warnings are generated before the + // translator exists, so they are built in English; retranslating them would + // mean re-running load(), and a warning about the config file is the one + // string a user can still act on in either language. // - // QLocale() reads the system locale, so LANG=it_IT.UTF-8 selects the file - // with no config key of our own. A missing .qm returns false and the app - // runs in English, which is the correct failure rather than a fatal one. + // An empty language() means follow the environment, which is what QLocale() + // default-constructs to. A missing .qm returns false and the app runs in + // English: that is the correct outcome both for an unsupported language and + // for `language = en_US`, since English is the source and ships no .qm. + const QLocale locale = config.language().isEmpty() + ? QLocale() + : QLocale(config.language()); + QTranslator translator; QStringList translationDirs; // Beside the binary first, so a build tree works without installing. @@ -108,7 +121,7 @@ int main(int argc, char *argv[]) translationDirs << dir + QStringLiteral("/translations"); for (const QString &dir : std::as_const(translationDirs)) { - if (translator.load(QLocale(), QStringLiteral("qtmaildir"), + if (translator.load(locale, QStringLiteral("qtmaildir"), QStringLiteral("_"), dir)) { app.installTranslator(&translator); break; @@ -122,9 +135,6 @@ int main(int argc, char *argv[]) return 1; } - Config config; - config.load(Config::defaultPath()); - MainWindow window(config); window.show(); |
