diff options
author | Fabian Müller <fmueller@owncloud.com> | 2021-03-16 17:46:41 +0300 |
---|---|---|
committer | Fabian Müller <80399010+fmoc@users.noreply.github.com> | 2021-03-31 12:43:26 +0300 |
commit | c6bfd2ba59358a09ff1e0e0cc8c79c10addb77d4 (patch) | |
tree | edd842ed1bbef5824624d4152d4b4dc29bd627ae | |
parent | 3ecdfcc97636c3fb160aa86a12c7cb67b6d682f6 (diff) |
Move translations-related functionaltiy into new utility namespace
-rw-r--r-- | src/gui/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/gui/application.cpp | 22 | ||||
-rw-r--r-- | src/gui/translations.cpp | 85 | ||||
-rw-r--r-- | src/gui/translations.h | 49 |
4 files changed, 138 insertions, 19 deletions
diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 868176ad0..2b9681420 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -86,6 +86,7 @@ set(client_SRCS servernotificationhandler.cpp guiutility.cpp elidedlabel.cpp + translations.cpp creds/httpcredentialsgui.cpp wizard/postfixlineedit.cpp wizard/abstractcredswizardpage.cpp diff --git a/src/gui/application.cpp b/src/gui/application.cpp index 451c1d4a5..36464b12b 100644 --- a/src/gui/application.cpp +++ b/src/gui/application.cpp @@ -42,6 +42,7 @@ #include "csync_exclude.h" #include "common/vfs.h" #include "settingsdialog.h" +#include "translations.h" #include "config.h" @@ -89,23 +90,6 @@ namespace { " --language <locale> : override UI language\n" " --confdir <dirname> : Use the given configuration folder."); } - - QString applicationTrPath() - { - QString devTrPath = qApp->applicationDirPath() + QString::fromLatin1("/../src/gui/"); - if (QDir(devTrPath).exists()) { - // might miss Qt, QtKeyChain, etc. - qCWarning(lcApplication) << "Running from build location! Translations may be incomplete!"; - return devTrPath; - } -#if defined(Q_OS_WIN) - return QApplication::applicationDirPath(); -#elif defined(Q_OS_MAC) - return QApplication::applicationDirPath() + QLatin1String("/../Resources/Translations"); // path defaults to app dir. -#elif defined(Q_OS_UNIX) - return QString::fromLatin1(SHAREDIR "/" APPLICATION_EXECUTABLE "/i18n/"); -#endif - } } // ---------------------------------------------------------------------------------- @@ -731,8 +715,8 @@ void Application::setupTranslations() foreach (QString lang, uiLanguages) { lang.replace(QLatin1Char('-'), QLatin1Char('_')); // work around QTBUG-25973 lang = substLang(lang); - const QString trPath = applicationTrPath(); - const QString trFile = QLatin1String("client_") + lang; + const QString trPath = Translations::applicationTrPath(); + const QString trFile = Translations::translationsFilePrefix() + lang; if (translator->load(trFile, trPath) || lang.startsWith(QLatin1String("en"))) { // Permissive approach: Qt and keychain translations // may be missing, but Qt translations must be there in order diff --git a/src/gui/translations.cpp b/src/gui/translations.cpp new file mode 100644 index 000000000..4b2529f43 --- /dev/null +++ b/src/gui/translations.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (C) by Duncan Mac-Vicar P. <duncan@kde.org> + * Copyright (C) by Klaas Freitag <freitag@owncloud.com> + * Copyright (C) by Daniel Molkentin <danimo@owncloud.com> + * Copyright (C) by Fabian Müller <fmueller@owncloud.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "translations.h" + +#include "config.h" +#include "theme.h" + +#include <QApplication> +#include <QLoggingCategory> +#include <QDir> +#include <QDirIterator> + +namespace OCC { + +namespace Translations { + + Q_LOGGING_CATEGORY(lcTranslations, "gui.translations", QtInfoMsg) + + const QString translationsFilePrefix() + { + return QStringLiteral("client_"); + } + + const QString translationsFileSuffix() + { + return QStringLiteral(".qm"); + } + + QString applicationTrPath() + { + QString devTrPath = qApp->applicationDirPath() + QString::fromLatin1("/../src/gui/"); + if (QDir(devTrPath).exists()) { + // might miss Qt, QtKeyChain, etc. + qCWarning(lcTranslations) << "Running from build location! Translations may be incomplete!"; + return devTrPath; + } +#if defined(Q_OS_WIN) + return QApplication::applicationDirPath(); +#elif defined(Q_OS_MAC) + return QApplication::applicationDirPath() + QLatin1String("/../Resources/Translations"); // path defaults to app dir. +#elif defined(Q_OS_UNIX) + return QStringLiteral(SHAREDIR "/" APPLICATION_EXECUTABLE "/i18n/"); +#endif + } + + QSet<QString> listAvailableTranslations() + { + QSet<QString> availableTranslations; + + // calculate a glob pattern which can be used in the iterator below to match only translations files + QString pattern = translationsFilePrefix() + "*" + translationsFileSuffix(); + + QDirIterator it(Translations::applicationTrPath(), QStringList() << pattern); + + while (it.hasNext()) { + // extract locale part from filename + // of course, this method relies on the filenames to be accurate + const auto fileName = QFileInfo(it.next()).fileName(); + const auto localePartLength = fileName.length() - translationsFileSuffix().length() - translationsFilePrefix().length(); + QString localeName = fileName.mid(translationsFilePrefix().length(), localePartLength); + + availableTranslations.insert(localeName); + } + + return availableTranslations; + } + +} // namespace Translations + +} // namespace OCC diff --git a/src/gui/translations.h b/src/gui/translations.h new file mode 100644 index 000000000..705c72c67 --- /dev/null +++ b/src/gui/translations.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) by Fabian Müller <fmueller@owncloud.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#ifndef TRANSLATIONSMANAGER_H +#define TRANSLATIONSMANAGER_H + +#include <QSet> +#include <QString> + +namespace OCC { + +namespace Translations { + + /** + * @return translation files' filename prefix + */ + const QString translationsFilePrefix(); + + /** + * @returntranslation files' filename suffix + */ + const QString translationsFileSuffix(); + + /** + * @return path to translation files + */ + QString applicationTrPath(); + + /** + * @return list of locales for which translations are available + */ + QSet<QString> listAvailableTranslations(); + +} // namespace Translations + +} // namespace OCC + +#endif // TRANSLATIONSMANAGER_H |