#include "language-manager.h" #include "app-settings.h" #include #include #include #include #include #include #include #include #include namespace { // Kept alive for the whole process: a QTranslator that is destroyed takes the // installed translation with it. QTranslator* g_translator = nullptr; QString catalogueFileName(const QString& code) { return QStringLiteral("translations/pacmanager_%1.qm").arg(code); } } // namespace QStringList LanguageManager::searchPaths() { QStringList paths; // Next to the settings the user just chose: where a catalogue installed by // hand (or by a future in-app translation pack) belongs. paths << AppSettings::instance().logDirectory() + QStringLiteral("/translations"); const QString data = QStandardPaths::writableLocation( QStandardPaths::AppDataLocation); if (!data.isEmpty()) paths << data + QStringLiteral("/translations"); // Running from a build tree, the catalogues sit beside the binary. paths << QCoreApplication::applicationDirPath() + QStringLiteral("/translations"); QStringList existing; for (const QString& path : paths) { if (QDir(path).exists()) existing << path; } return existing; } QString LanguageManager::resolve(const QString& code) { const QString wanted = AppSettings::sanitizeLanguage(code); if (wanted != QLatin1String("system")) return wanted; // "system" follows the desktop, but only into a language the app knows: a // desktop set to Esperanto should leave the interface in its own language // rather than install nothing at all. const QString locale = QLocale::system().name().left(2); if (AppSettings::knownLanguages().contains(locale)) return locale; return QStringLiteral("fr"); } bool LanguageManager::isTranslationAvailable(const QString& code) { const QString resolved = resolve(code); const QString fileName = catalogueFileName(resolved); for (const QString& path : searchPaths()) { if (QFile::exists(path + QLatin1Char('/') + fileName)) return true; } return false; } QString LanguageManager::searchPathHint() { return searchPaths().isEmpty() ? QString() : searchPaths().first(); } QString LanguageManager::activeCode() { return g_translator ? g_translator->language() : QString(); } bool LanguageManager::apply(const QString& code) { const QString resolved = resolve(code); const QString fileName = catalogueFileName(resolved); // The previous catalogue goes first, so a language the user just left does // not keep translating part of the interface. if (g_translator) { QCoreApplication::removeTranslator(g_translator); delete g_translator; g_translator = nullptr; } bool installed = false; for (const QString& path : searchPaths()) { if (!g_translator) g_translator = new QTranslator(); if (g_translator->load(fileName, path)) { QCoreApplication::installTranslator(g_translator); installed = true; break; } delete g_translator; g_translator = nullptr; } // No catalogue (the common case today): the widgets still get the event, so // switching back to an untranslated language restores the original wording // instead of leaving the previous translation in place. const QWidgetList widgets = QApplication::allWidgets(); for (QWidget* widget : widgets) { QEvent event(QEvent::LanguageChange); QCoreApplication::sendEvent(widget, &event); } return installed; }