3 #include "resources/config.h" 7 #include <QLibraryInfo> 11 #include <QStringBuilder> 12 #include <QTranslator> 13 #if defined(QT_UTILITIES_GUI_QTWIDGETS) 14 #include <QApplication> 17 #include <QStyleFactory> 18 #elif defined(QT_UTILITIES_GUI_QTQUICK) 20 #include <QGuiApplication> 23 #include <QCoreApplication> 31 inline void initResources()
33 Q_INIT_RESOURCE(qtutilsicons);
36 inline void cleanupResources()
38 Q_CLEANUP_RESOURCE(qtutilsicons);
118 for (
const QString &repoName : repositoryNames) {
119 QTranslator *qtTranslator =
new QTranslator;
120 const QString fileName(repoName % QChar(
'_') % localeName);
122 QCoreApplication::installTranslator(qtTranslator);
123 }
else if (qtTranslator->load(fileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
124 QCoreApplication::installTranslator(qtTranslator);
125 }
else if (qtTranslator->load(fileName, QStringLiteral(
"../share/qt/translations"))) {
127 QCoreApplication::installTranslator(qtTranslator);
128 }
else if (qtTranslator->load(fileName, QStringLiteral(
":/translations"))) {
129 QCoreApplication::installTranslator(qtTranslator);
132 cerr <<
"Unable to load translation file for Qt repository \"" << repoName.toLocal8Bit().data() <<
"\" and language " 133 << localeName.toLocal8Bit().data() <<
"." << endl;
159 if (QLocale().name() != QLatin1String(
"en_US")) {
183 QTranslator *appTranslator =
new QTranslator;
184 const QString fileName(QStringLiteral(
"%1_%2").arg(applicationName, localeName));
186 QCoreApplication::installTranslator(appTranslator);
187 }
else if (appTranslator->load(fileName, QStringLiteral(
"."))) {
188 QCoreApplication::installTranslator(appTranslator);
189 }
else if (appTranslator->load(fileName, QStringLiteral(
"./translations"))) {
190 QCoreApplication::installTranslator(appTranslator);
191 }
else if (appTranslator->load(fileName, QStringLiteral(APP_INSTALL_PREFIX
"/share/%1/translations").arg(applicationName))) {
192 QCoreApplication::installTranslator(appTranslator);
193 }
else if (appTranslator->load(fileName, QStringLiteral(
"../share/%1/translations").arg(applicationName))) {
194 QCoreApplication::installTranslator(appTranslator);
195 }
else if (appTranslator->load(fileName, QStringLiteral(
":/translations"))) {
196 QCoreApplication::installTranslator(appTranslator);
198 delete appTranslator;
199 cerr <<
"Unable to load translation file for \"" << applicationName.toLocal8Bit().data() <<
"\" and the language \"" 200 << localeName.toLocal8Bit().data() <<
"\"." << endl;
211 for (
const QString &applicationName : applicationNames) {
224 for (
const QString &applicationName : applicationNames) {
237 #if defined(QT_UTILITIES_GUI_QTWIDGETS) 243 return qobject_cast<QApplication *>(QCoreApplication::instance()) !=
nullptr;
247 #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK) 253 return qobject_cast<QGuiApplication *>(QCoreApplication::instance()) !=
nullptr;
262 return qobject_cast<QCoreApplication *>(QCoreApplication::instance()) !=
nullptr;
277 QString
locateConfigFile(
const QString &applicationName,
const QString &fileName,
const QSettings *settings)
280 if (QFile::exists(fileName)) {
286 path = QFileInfo(settings->fileName()).absoluteDir().absoluteFilePath(fileName);
287 if (QFile::exists(path)) {
294 path = QStringLiteral(
"../etc/") % applicationName % QChar(
'/') % fileName;
295 if (QFile::exists(path)) {
300 path = QStringLiteral(
"../share/") % applicationName % QChar(
'/') % fileName;
301 if (QFile::exists(path)) {
308 path = QStringLiteral(
"/etc/") % applicationName % QChar(
'/') % fileName;
309 if (QFile::exists(path)) {
314 path = QStringLiteral(
"/usr/share/") % applicationName % QChar(
'/') % fileName;
315 if (QFile::exists(path)) {
QT_UTILITIES_EXPORT void cleanup()
Frees the resources used and provided by this library.
QT_UTILITIES_EXPORT bool hasCoreApp()
Returns whether a QCoreApplication has been instantiated yet.
Convenience functions to load translations for Qt and the application.
QT_UTILITIES_EXPORT QString locateConfigFile(const QString &applicationName, const QString &fileName, const QSettings *settings=nullptr)
Locates the config file with the specified fileName for the application with the specified applicatio...
QT_UTILITIES_EXPORT QString & additionalTranslationFilePath()
Allows to set an additional search path for translation files.
QT_UTILITIES_EXPORT void init()
Initiates the resources used and provided by this library.
Convenience functions to check whether a QCoreApplication/QGuiApplication/QApplication singleton has ...
QT_UTILITIES_EXPORT void loadApplicationTranslationFile(const QString &applicationName)
Loads and installs the appropriate application translation file for the current locale.
Provides convenience functions for handling config files.
void loadQtTranslationFile(initializer_list< QString > repositoryNames, const QString &localeName)
Loads and installs the appropriate Qt translation file for the specified locale.