3 #include "resources/config.h" 9 #include <QLibraryInfo> 13 #include <QStringBuilder> 14 #include <QTranslator> 15 #if defined(QT_UTILITIES_GUI_QTWIDGETS) 16 #include <QApplication> 17 #elif defined(QT_UTILITIES_GUI_QTQUICK) 18 #include <QGuiApplication> 20 #include <QCoreApplication> 28 inline void initResources()
30 Q_INIT_RESOURCE(qtutilsicons);
33 inline void cleanupResources()
35 Q_CLEANUP_RESOURCE(qtutilsicons);
115 for (
const QString &repoName : repositoryNames) {
116 QTranslator *qtTranslator =
new QTranslator;
117 const QString fileName(repoName % QChar(
'_') % localeName);
119 QCoreApplication::installTranslator(qtTranslator);
120 }
else if (qtTranslator->load(fileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
121 QCoreApplication::installTranslator(qtTranslator);
122 }
else if (qtTranslator->load(fileName, QStringLiteral(
"../share/qt/translations"))) {
123 QCoreApplication::installTranslator(qtTranslator);
124 }
else if (qtTranslator->load(fileName, QStringLiteral(
":/translations"))) {
125 QCoreApplication::installTranslator(qtTranslator);
128 if (localeName.startsWith(QLatin1String(
"en"))) {
132 cerr <<
"Unable to load translation file for Qt repository \"" << repoName.toLocal8Bit().data() <<
"\" and locale " 133 << localeName.toLocal8Bit().data() <<
"." << endl;
159 if (QLocale().name() != QLatin1String(
"en_US")) {
183 QTranslator *appTranslator =
new QTranslator;
184 const QString fileName(applicationName % QChar(
'_') % 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/") % applicationName % QStringLiteral(
"/translations"))) {
192 QCoreApplication::installTranslator(appTranslator);
193 }
else if (appTranslator->load(fileName, QStringLiteral(
"../share/") % applicationName % QStringLiteral(
"/translations"))) {
194 QCoreApplication::installTranslator(appTranslator);
195 }
else if (appTranslator->load(fileName, QStringLiteral(
":/translations"))) {
196 QCoreApplication::installTranslator(appTranslator);
198 delete appTranslator;
199 if (localeName.startsWith(QLatin1String(
"en"))) {
203 cerr <<
"Unable to load translation file for \"" << applicationName.toLocal8Bit().data() <<
"\" and the locale \"" 204 << localeName.toLocal8Bit().data() <<
"\"." << endl;
215 for (
const QString &applicationName : applicationNames) {
228 for (
const QString &applicationName : applicationNames) {
241 #if defined(QT_UTILITIES_GUI_QTWIDGETS) 247 return qobject_cast<QApplication *>(QCoreApplication::instance()) !=
nullptr;
251 #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK) 257 return qobject_cast<QGuiApplication *>(QCoreApplication::instance()) !=
nullptr;
266 return qobject_cast<QCoreApplication *>(QCoreApplication::instance()) !=
nullptr;
281 QString
locateConfigFile(
const QString &applicationName,
const QString &fileName,
const QSettings *settings)
284 if (QFile::exists(fileName)) {
290 path = QFileInfo(settings->fileName()).absoluteDir().absoluteFilePath(fileName);
291 if (QFile::exists(path)) {
298 path = QStringLiteral(
"../etc/") % applicationName % QChar(
'/') % fileName;
299 if (QFile::exists(path)) {
304 path = QStringLiteral(
"../share/") % applicationName % QChar(
'/') % fileName;
305 if (QFile::exists(path)) {
312 path = QStringLiteral(
"/etc/") % applicationName % QChar(
'/') % fileName;
313 if (QFile::exists(path)) {
318 path = QStringLiteral(
"/usr/share/") % applicationName % QChar(
'/') % fileName;
319 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.