diff --git a/python/core/auto_generated/qgsapplication.sip.in b/python/core/auto_generated/qgsapplication.sip.in index bccee57c50f..71430f9651b 100644 --- a/python/core/auto_generated/qgsapplication.sip.in +++ b/python/core/auto_generated/qgsapplication.sip.in @@ -807,6 +807,13 @@ The maximum number of concurrent connections per connections pool. QGIS may in some situations allocate more than this amount of connections to avoid deadlocks. +.. versionadded:: 3.4 +%End + + static void setTranslation( QString translation ); +%Docstring +Set translation + .. versionadded:: 3.4 %End diff --git a/src/app/main.cpp b/src/app/main.cpp index 49539c41925..e0481f6b457 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include @@ -538,7 +537,7 @@ int main( int argc, char *argv[] ) // This behavior will allow you to force the use of a translation file // which is useful for testing - QString myTranslationCode; + QString translationCode; // The user can specify a path which will override the default path of custom // user settings (~/.qgis) and it will be used for QgsSettings INI file @@ -622,7 +621,7 @@ int main( int argc, char *argv[] ) } else if ( i + 1 < argc && ( arg == QLatin1String( "--lang" ) || arg == QLatin1String( "-l" ) ) ) { - myTranslationCode = args[++i]; + translationCode = args[++i]; } else if ( i + 1 < argc && ( arg == QLatin1String( "--project" ) || arg == QLatin1String( "-p" ) ) ) { @@ -815,6 +814,71 @@ int main( int argc, char *argv[] ) QCoreApplication::setAttribute( Qt::AA_DisableWindowContextHelpButton, true ); #endif + /* Translation file for QGIS. + */ + QString i18nPath = QgsApplication::i18nPath(); + QgsSettings mySettings; + QString myUserTranslation = mySettings.value( QStringLiteral( "locale/userLocale" ), "" ).toString(); + QString myGlobalLocale = mySettings.value( QStringLiteral( "locale/globalLocale" ), "" ).toString(); + bool myShowGroupSeparatorFlag = false; // Default to false + bool myLocaleOverrideFlag = mySettings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool(); + + // Override Show Group Separator if the global override flag is set + if ( myLocaleOverrideFlag ) + { + // Default to false again + myShowGroupSeparatorFlag = mySettings.value( QStringLiteral( "locale/showGroupSeparator" ), false ).toBool(); + } + + // + // Priority of translation is: + // - command line + // - user specified in options dialog (with group checked on) + // - system locale + // + // When specifying from the command line it will change the user + // specified user locale + // + if ( !translationCode.isNull() && !translationCode.isEmpty() ) + { + mySettings.setValue( QStringLiteral( "locale/userLocale" ), translationCode ); + } + else + { + if ( !myLocaleOverrideFlag || myUserTranslation.isEmpty() ) + { + translationCode = QLocale().name(); + //setting the locale/userLocale when the --lang= option is not set will allow third party + //plugins to always use the same locale as the QGIS, otherwise they can be out of sync + mySettings.setValue( QStringLiteral( "locale/userLocale" ), translationCode ); + } + else + { + translationCode = myUserTranslation; + } + } + + // Global locale settings + if ( myLocaleOverrideFlag && ! myGlobalLocale.isEmpty( ) ) + { + QLocale currentLocale( myGlobalLocale ); + QLocale::setDefault( currentLocale ); + } + + // Number settings + QLocale currentLocale; + if ( myShowGroupSeparatorFlag ) + { + currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator ); + } + else + { + currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator ); + } + QLocale::setDefault( currentLocale ); + + QgsApplication::setTranslation( translationCode ); + QgsApplication myApp( argc, argv, myUseGuiFlag ); // SetUp the QgsSettings Global Settings: @@ -1057,9 +1121,6 @@ int main( int argc, char *argv[] ) } #endif - - QgsSettings mySettings; - // update any saved setting for older themes to new default 'gis' theme (2013-04-15) if ( mySettings.contains( QStringLiteral( "/Themes" ) ) ) { @@ -1157,96 +1218,6 @@ int main( int argc, char *argv[] ) QApplication::setStyle( presetStyle ); mySettings.setValue( QStringLiteral( "qgis/style" ), QApplication::style()->objectName() ); } - /* Translation file for QGIS. - */ - QString i18nPath = QgsApplication::i18nPath(); - QString myUserTranslation = mySettings.value( QStringLiteral( "locale/userLocale" ), "" ).toString(); - QString myGlobalLocale = mySettings.value( QStringLiteral( "locale/globalLocale" ), "" ).toString(); - bool myShowGroupSeparatorFlag = false; // Default to false - bool myLocaleOverrideFlag = mySettings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool(); - - // Override Show Group Separator if the global override flag is set - if ( myLocaleOverrideFlag ) - { - // Default to false again - myShowGroupSeparatorFlag = mySettings.value( QStringLiteral( "locale/showGroupSeparator" ), false ).toBool(); - } - - // - // Priority of translation is: - // - command line - // - user specified in options dialog (with group checked on) - // - system locale - // - // When specifying from the command line it will change the user - // specified user locale - // - if ( !myTranslationCode.isNull() && !myTranslationCode.isEmpty() ) - { - mySettings.setValue( QStringLiteral( "locale/userLocale" ), myTranslationCode ); - } - else - { - if ( !myLocaleOverrideFlag || myUserTranslation.isEmpty() ) - { - myTranslationCode = QLocale().name(); - //setting the locale/userLocale when the --lang= option is not set will allow third party - //plugins to always use the same locale as the QGIS, otherwise they can be out of sync - mySettings.setValue( QStringLiteral( "locale/userLocale" ), myTranslationCode ); - } - else - { - myTranslationCode = myUserTranslation; - } - } - - // Global locale settings - if ( myLocaleOverrideFlag && ! myGlobalLocale.isEmpty( ) ) - { - QLocale currentLocale( myGlobalLocale ); - QLocale::setDefault( currentLocale ); - } - - // Number settings - QLocale currentLocale; - if ( myShowGroupSeparatorFlag ) - { - currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator ); - } - else - { - currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator ); - } - QLocale::setDefault( currentLocale ); - - - QTranslator qgistor( nullptr ); - QTranslator qttor( nullptr ); - if ( myTranslationCode != QLatin1String( "C" ) ) - { - if ( qgistor.load( QStringLiteral( "qgis_" ) + myTranslationCode, i18nPath ) ) - { - myApp.installTranslator( &qgistor ); - } - else - { - QgsDebugMsg( QStringLiteral( "loading of qgis translation failed %1/qgis_%2" ).arg( i18nPath, myTranslationCode ) ); - } - - /* Translation file for Qt. - * The strings from the QMenuBar context section are used by Qt/Mac to shift - * the About, Preferences and Quit items to the Mac Application menu. - * These items must be translated identically in both qt_ and qgis_ files. - */ - if ( qttor.load( QStringLiteral( "qt_" ) + myTranslationCode, QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) ) - { - myApp.installTranslator( &qttor ); - } - else - { - QgsDebugMsg( QStringLiteral( "loading of qt translation failed %1/qt_%2" ).arg( QLibraryInfo::location( QLibraryInfo::TranslationsPath ), myTranslationCode ) ); - } - } // set authentication database directory if ( !authdbdirectory.isEmpty() ) diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index ab70100904a..c671b243404 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -117,6 +117,7 @@ QString ABISYM( QgsApplication::mAuthDbDirPath ); QString QgsApplication::sUserName; QString QgsApplication::sUserFullName; QString QgsApplication::sPlatformName = QStringLiteral( "desktop" ); +QString QgsApplication::sTranslation; const char *QgsApplication::QGIS_ORGANIZATION_NAME = "QGIS"; const char *QgsApplication::QGIS_ORGANIZATION_DOMAIN = "qgis.org"; @@ -129,6 +130,34 @@ QgsApplication::QgsApplication( int &argc, char **argv, bool GUIenabled, const Q { sPlatformName = platformName; + if ( sTranslation != QLatin1String( "C" ) ) + { + mQgisTranslator = new QTranslator(); + if ( mQgisTranslator->load( QStringLiteral( "qgis_" ) + sTranslation, i18nPath() ) ) + { + installTranslator( mQgisTranslator ); + } + else + { + QgsDebugMsg( QStringLiteral( "loading of qgis translation failed %1/qgis_%2" ).arg( i18nPath(), sTranslation ) ); + } + + /* Translation file for Qt. + * The strings from the QMenuBar context section are used by Qt/Mac to shift + * the About, Preferences and Quit items to the Mac Application menu. + * These items must be translated identically in both qt_ and qgis_ files. + */ + mQtTranslator = new QTranslator(); + if ( mQtTranslator->load( QStringLiteral( "qt_" ) + sTranslation, QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) ) + { + installTranslator( mQtTranslator ); + } + else + { + QgsDebugMsg( QStringLiteral( "loading of qt translation failed %1/qt_%2" ).arg( QLibraryInfo::location( QLibraryInfo::TranslationsPath ), sTranslation ) ); + } + } + mApplicationMembers = new ApplicationMembers(); ABISYM( mProfilePath ) = profileFolder; @@ -296,6 +325,8 @@ QgsApplication::~QgsApplication() { delete mDataItemProviderRegistry; delete mApplicationMembers; + delete mQgisTranslator; + delete mQtTranslator; } QgsApplication *QgsApplication::instance() @@ -649,7 +680,6 @@ QString QgsApplication::resolvePkgPath() { return prefixPath + '/' + QStringLiteral( QGIS_DATA_SUBDIR ); } - } QString QgsApplication::themeName() @@ -772,8 +802,10 @@ QString QgsApplication::licenceFilePath() QString QgsApplication::i18nPath() { - if ( ABISYM( mRunningFromBuildDir ) ) - return ABISYM( mBuildOutputPath ) + QStringLiteral( "/i18n" ); + if ( !ABISYM( mInitialized ) ) + return resolvePkgPath() + QStringLiteral( "/i18n/" ); + else if ( ABISYM( mRunningFromBuildDir ) ) + return ABISYM( mBuildOutputPath ) + QStringLiteral( "/i18n/" ); else return ABISYM( mPkgDataPath ) + QStringLiteral( "/i18n/" ); } diff --git a/src/core/qgsapplication.h b/src/core/qgsapplication.h index 8a519a5975b..eb7e5b126b6 100644 --- a/src/core/qgsapplication.h +++ b/src/core/qgsapplication.h @@ -48,6 +48,7 @@ class QgsPageSizeRegistry; class QgsLayoutItemRegistry; class QgsAuthManager; class QgsNetworkContentFetcherRegistry; +class QTranslator; /** * \ingroup core @@ -742,6 +743,13 @@ class CORE_EXPORT QgsApplication : public QApplication */ int maxConcurrentConnectionsPerPool() const; + /** + * Set translation + * + * \since QGIS 3.4 + */ + static void setTranslation( QString translation ) { sTranslation = translation; } + /** * Emits the signal to collect all the strings of .qgs to be included in ts file * @@ -829,10 +837,14 @@ class CORE_EXPORT QgsApplication : public QApplication static QString sUserName; static QString sUserFullName; static QString sPlatformName; + static QString sTranslation; QMap mIconCache; QMap mCursorCache; + QTranslator *mQgisTranslator = nullptr; + QTranslator *mQtTranslator = nullptr; + QgsDataItemProviderRegistry *mDataItemProviderRegistry = nullptr; QgsAuthManager *mAuthManager = nullptr;