Initialize translations before application members (fixes #19853)

This commit is contained in:
Juergen E. Fischer 2018-09-16 21:16:28 +02:00
parent 40024255a5
commit d67958183e
4 changed files with 121 additions and 99 deletions

View File

@ -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

View File

@ -31,7 +31,6 @@
#include <QStyle>
#include <QStyleFactory>
#include <QDesktopWidget>
#include <QTranslator>
#include <QImageReader>
#include <QMessageBox>
@ -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() )

View File

@ -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/" );
}

View File

@ -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<QString, QIcon> mIconCache;
QMap<Cursor, QCursor> mCursorCache;
QTranslator *mQgisTranslator = nullptr;
QTranslator *mQtTranslator = nullptr;
QgsDataItemProviderRegistry *mDataItemProviderRegistry = nullptr;
QgsAuthManager *mAuthManager = nullptr;