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"))) {
126 QCoreApplication::installTranslator(qtTranslator);
127 }
else if (qtTranslator->load(fileName, QStringLiteral(
":/translations"))) {
128 QCoreApplication::installTranslator(qtTranslator);
131 if (localeName.startsWith(QLatin1String(
"en"))) {
135 cerr <<
"Unable to load translation file for Qt repository \"" << repoName.toLocal8Bit().data() <<
"\" and locale " 136 << localeName.toLocal8Bit().data() <<
"." << endl;
162 if (QLocale().name() != QLatin1String(
"en_US")) {
186 QTranslator *appTranslator =
new QTranslator;
187 const QString fileName(applicationName % QChar(
'_') % localeName);
189 QCoreApplication::installTranslator(appTranslator);
190 }
else if (appTranslator->load(fileName, QStringLiteral(
"."))) {
191 QCoreApplication::installTranslator(appTranslator);
192 }
else if (appTranslator->load(fileName, QStringLiteral(
"./translations"))) {
193 QCoreApplication::installTranslator(appTranslator);
194 }
else if (appTranslator->load(fileName, QStringLiteral(APP_INSTALL_PREFIX
"/share/") % applicationName % QStringLiteral(
"/translations"))) {
195 QCoreApplication::installTranslator(appTranslator);
196 }
else if (appTranslator->load(fileName, QStringLiteral(
"../share/") % applicationName % QStringLiteral(
"/translations"))) {
197 QCoreApplication::installTranslator(appTranslator);
198 }
else if (appTranslator->load(fileName, QStringLiteral(
":/translations"))) {
199 QCoreApplication::installTranslator(appTranslator);
201 delete appTranslator;
202 if (localeName.startsWith(QLatin1String(
"en"))) {
206 cerr <<
"Unable to load translation file for \"" << applicationName.toLocal8Bit().data() <<
"\" and the locale \"" 207 << localeName.toLocal8Bit().data() <<
"\"." << endl;
218 for (
const QString &applicationName : applicationNames) {
231 for (
const QString &applicationName : applicationNames) {
244 #if defined(QT_UTILITIES_GUI_QTWIDGETS) 250 return qobject_cast<QApplication *>(QCoreApplication::instance()) !=
nullptr;
254 #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK) 260 return qobject_cast<QGuiApplication *>(QCoreApplication::instance()) !=
nullptr;
269 return qobject_cast<QCoreApplication *>(QCoreApplication::instance()) !=
nullptr;
284 QString
locateConfigFile(
const QString &applicationName,
const QString &fileName,
const QSettings *settings)
287 if (QFile::exists(fileName)) {
293 path = QFileInfo(settings->fileName()).absoluteDir().absoluteFilePath(fileName);
294 if (QFile::exists(path)) {
301 path = QStringLiteral(
"../etc/") % applicationName % QChar(
'/') % fileName;
302 if (QFile::exists(path)) {
307 path = QStringLiteral(
"../share/") % applicationName % QChar(
'/') % fileName;
308 if (QFile::exists(path)) {
315 path = QStringLiteral(
"/etc/") % applicationName % QChar(
'/') % fileName;
316 if (QFile::exists(path)) {
321 path = QStringLiteral(
"/usr/share/") % applicationName % QChar(
'/') % fileName;
322 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.