Skip to content

Commit d679581

Browse files
committed
Initialize translations before application members (fixes #19853)
1 parent 4002425 commit d679581

File tree

4 files changed

+121
-99
lines changed

4 files changed

+121
-99
lines changed

python/core/auto_generated/qgsapplication.sip.in

+7
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,13 @@ The maximum number of concurrent connections per connections pool.
807807
QGIS may in some situations allocate more than this amount
808808
of connections to avoid deadlocks.
809809

810+
.. versionadded:: 3.4
811+
%End
812+
813+
static void setTranslation( QString translation );
814+
%Docstring
815+
Set translation
816+
810817
.. versionadded:: 3.4
811818
%End
812819

src/app/main.cpp

+67-96
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <QStyle>
3232
#include <QStyleFactory>
3333
#include <QDesktopWidget>
34-
#include <QTranslator>
3534
#include <QImageReader>
3635
#include <QMessageBox>
3736

@@ -538,7 +537,7 @@ int main( int argc, char *argv[] )
538537

539538
// This behavior will allow you to force the use of a translation file
540539
// which is useful for testing
541-
QString myTranslationCode;
540+
QString translationCode;
542541

543542
// The user can specify a path which will override the default path of custom
544543
// user settings (~/.qgis) and it will be used for QgsSettings INI file
@@ -622,7 +621,7 @@ int main( int argc, char *argv[] )
622621
}
623622
else if ( i + 1 < argc && ( arg == QLatin1String( "--lang" ) || arg == QLatin1String( "-l" ) ) )
624623
{
625-
myTranslationCode = args[++i];
624+
translationCode = args[++i];
626625
}
627626
else if ( i + 1 < argc && ( arg == QLatin1String( "--project" ) || arg == QLatin1String( "-p" ) ) )
628627
{
@@ -815,6 +814,71 @@ int main( int argc, char *argv[] )
815814
QCoreApplication::setAttribute( Qt::AA_DisableWindowContextHelpButton, true );
816815
#endif
817816

817+
/* Translation file for QGIS.
818+
*/
819+
QString i18nPath = QgsApplication::i18nPath();
820+
QgsSettings mySettings;
821+
QString myUserTranslation = mySettings.value( QStringLiteral( "locale/userLocale" ), "" ).toString();
822+
QString myGlobalLocale = mySettings.value( QStringLiteral( "locale/globalLocale" ), "" ).toString();
823+
bool myShowGroupSeparatorFlag = false; // Default to false
824+
bool myLocaleOverrideFlag = mySettings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool();
825+
826+
// Override Show Group Separator if the global override flag is set
827+
if ( myLocaleOverrideFlag )
828+
{
829+
// Default to false again
830+
myShowGroupSeparatorFlag = mySettings.value( QStringLiteral( "locale/showGroupSeparator" ), false ).toBool();
831+
}
832+
833+
//
834+
// Priority of translation is:
835+
// - command line
836+
// - user specified in options dialog (with group checked on)
837+
// - system locale
838+
//
839+
// When specifying from the command line it will change the user
840+
// specified user locale
841+
//
842+
if ( !translationCode.isNull() && !translationCode.isEmpty() )
843+
{
844+
mySettings.setValue( QStringLiteral( "locale/userLocale" ), translationCode );
845+
}
846+
else
847+
{
848+
if ( !myLocaleOverrideFlag || myUserTranslation.isEmpty() )
849+
{
850+
translationCode = QLocale().name();
851+
//setting the locale/userLocale when the --lang= option is not set will allow third party
852+
//plugins to always use the same locale as the QGIS, otherwise they can be out of sync
853+
mySettings.setValue( QStringLiteral( "locale/userLocale" ), translationCode );
854+
}
855+
else
856+
{
857+
translationCode = myUserTranslation;
858+
}
859+
}
860+
861+
// Global locale settings
862+
if ( myLocaleOverrideFlag && ! myGlobalLocale.isEmpty( ) )
863+
{
864+
QLocale currentLocale( myGlobalLocale );
865+
QLocale::setDefault( currentLocale );
866+
}
867+
868+
// Number settings
869+
QLocale currentLocale;
870+
if ( myShowGroupSeparatorFlag )
871+
{
872+
currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
873+
}
874+
else
875+
{
876+
currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator );
877+
}
878+
QLocale::setDefault( currentLocale );
879+
880+
QgsApplication::setTranslation( translationCode );
881+
818882
QgsApplication myApp( argc, argv, myUseGuiFlag );
819883

820884
// SetUp the QgsSettings Global Settings:
@@ -1057,9 +1121,6 @@ int main( int argc, char *argv[] )
10571121
}
10581122
#endif
10591123

1060-
1061-
QgsSettings mySettings;
1062-
10631124
// update any saved setting for older themes to new default 'gis' theme (2013-04-15)
10641125
if ( mySettings.contains( QStringLiteral( "/Themes" ) ) )
10651126
{
@@ -1157,96 +1218,6 @@ int main( int argc, char *argv[] )
11571218
QApplication::setStyle( presetStyle );
11581219
mySettings.setValue( QStringLiteral( "qgis/style" ), QApplication::style()->objectName() );
11591220
}
1160-
/* Translation file for QGIS.
1161-
*/
1162-
QString i18nPath = QgsApplication::i18nPath();
1163-
QString myUserTranslation = mySettings.value( QStringLiteral( "locale/userLocale" ), "" ).toString();
1164-
QString myGlobalLocale = mySettings.value( QStringLiteral( "locale/globalLocale" ), "" ).toString();
1165-
bool myShowGroupSeparatorFlag = false; // Default to false
1166-
bool myLocaleOverrideFlag = mySettings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool();
1167-
1168-
// Override Show Group Separator if the global override flag is set
1169-
if ( myLocaleOverrideFlag )
1170-
{
1171-
// Default to false again
1172-
myShowGroupSeparatorFlag = mySettings.value( QStringLiteral( "locale/showGroupSeparator" ), false ).toBool();
1173-
}
1174-
1175-
//
1176-
// Priority of translation is:
1177-
// - command line
1178-
// - user specified in options dialog (with group checked on)
1179-
// - system locale
1180-
//
1181-
// When specifying from the command line it will change the user
1182-
// specified user locale
1183-
//
1184-
if ( !myTranslationCode.isNull() && !myTranslationCode.isEmpty() )
1185-
{
1186-
mySettings.setValue( QStringLiteral( "locale/userLocale" ), myTranslationCode );
1187-
}
1188-
else
1189-
{
1190-
if ( !myLocaleOverrideFlag || myUserTranslation.isEmpty() )
1191-
{
1192-
myTranslationCode = QLocale().name();
1193-
//setting the locale/userLocale when the --lang= option is not set will allow third party
1194-
//plugins to always use the same locale as the QGIS, otherwise they can be out of sync
1195-
mySettings.setValue( QStringLiteral( "locale/userLocale" ), myTranslationCode );
1196-
}
1197-
else
1198-
{
1199-
myTranslationCode = myUserTranslation;
1200-
}
1201-
}
1202-
1203-
// Global locale settings
1204-
if ( myLocaleOverrideFlag && ! myGlobalLocale.isEmpty( ) )
1205-
{
1206-
QLocale currentLocale( myGlobalLocale );
1207-
QLocale::setDefault( currentLocale );
1208-
}
1209-
1210-
// Number settings
1211-
QLocale currentLocale;
1212-
if ( myShowGroupSeparatorFlag )
1213-
{
1214-
currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
1215-
}
1216-
else
1217-
{
1218-
currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator );
1219-
}
1220-
QLocale::setDefault( currentLocale );
1221-
1222-
1223-
QTranslator qgistor( nullptr );
1224-
QTranslator qttor( nullptr );
1225-
if ( myTranslationCode != QLatin1String( "C" ) )
1226-
{
1227-
if ( qgistor.load( QStringLiteral( "qgis_" ) + myTranslationCode, i18nPath ) )
1228-
{
1229-
myApp.installTranslator( &qgistor );
1230-
}
1231-
else
1232-
{
1233-
QgsDebugMsg( QStringLiteral( "loading of qgis translation failed %1/qgis_%2" ).arg( i18nPath, myTranslationCode ) );
1234-
}
1235-
1236-
/* Translation file for Qt.
1237-
* The strings from the QMenuBar context section are used by Qt/Mac to shift
1238-
* the About, Preferences and Quit items to the Mac Application menu.
1239-
* These items must be translated identically in both qt_ and qgis_ files.
1240-
*/
1241-
if ( qttor.load( QStringLiteral( "qt_" ) + myTranslationCode, QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
1242-
{
1243-
myApp.installTranslator( &qttor );
1244-
}
1245-
else
1246-
{
1247-
QgsDebugMsg( QStringLiteral( "loading of qt translation failed %1/qt_%2" ).arg( QLibraryInfo::location( QLibraryInfo::TranslationsPath ), myTranslationCode ) );
1248-
}
1249-
}
12501221

12511222
// set authentication database directory
12521223
if ( !authdbdirectory.isEmpty() )

src/core/qgsapplication.cpp

+35-3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ QString ABISYM( QgsApplication::mAuthDbDirPath );
117117
QString QgsApplication::sUserName;
118118
QString QgsApplication::sUserFullName;
119119
QString QgsApplication::sPlatformName = QStringLiteral( "desktop" );
120+
QString QgsApplication::sTranslation;
120121

121122
const char *QgsApplication::QGIS_ORGANIZATION_NAME = "QGIS";
122123
const char *QgsApplication::QGIS_ORGANIZATION_DOMAIN = "qgis.org";
@@ -129,6 +130,34 @@ QgsApplication::QgsApplication( int &argc, char **argv, bool GUIenabled, const Q
129130
{
130131
sPlatformName = platformName;
131132

133+
if ( sTranslation != QLatin1String( "C" ) )
134+
{
135+
mQgisTranslator = new QTranslator();
136+
if ( mQgisTranslator->load( QStringLiteral( "qgis_" ) + sTranslation, i18nPath() ) )
137+
{
138+
installTranslator( mQgisTranslator );
139+
}
140+
else
141+
{
142+
QgsDebugMsg( QStringLiteral( "loading of qgis translation failed %1/qgis_%2" ).arg( i18nPath(), sTranslation ) );
143+
}
144+
145+
/* Translation file for Qt.
146+
* The strings from the QMenuBar context section are used by Qt/Mac to shift
147+
* the About, Preferences and Quit items to the Mac Application menu.
148+
* These items must be translated identically in both qt_ and qgis_ files.
149+
*/
150+
mQtTranslator = new QTranslator();
151+
if ( mQtTranslator->load( QStringLiteral( "qt_" ) + sTranslation, QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
152+
{
153+
installTranslator( mQtTranslator );
154+
}
155+
else
156+
{
157+
QgsDebugMsg( QStringLiteral( "loading of qt translation failed %1/qt_%2" ).arg( QLibraryInfo::location( QLibraryInfo::TranslationsPath ), sTranslation ) );
158+
}
159+
}
160+
132161
mApplicationMembers = new ApplicationMembers();
133162

134163
ABISYM( mProfilePath ) = profileFolder;
@@ -296,6 +325,8 @@ QgsApplication::~QgsApplication()
296325
{
297326
delete mDataItemProviderRegistry;
298327
delete mApplicationMembers;
328+
delete mQgisTranslator;
329+
delete mQtTranslator;
299330
}
300331

301332
QgsApplication *QgsApplication::instance()
@@ -649,7 +680,6 @@ QString QgsApplication::resolvePkgPath()
649680
{
650681
return prefixPath + '/' + QStringLiteral( QGIS_DATA_SUBDIR );
651682
}
652-
653683
}
654684

655685
QString QgsApplication::themeName()
@@ -772,8 +802,10 @@ QString QgsApplication::licenceFilePath()
772802

773803
QString QgsApplication::i18nPath()
774804
{
775-
if ( ABISYM( mRunningFromBuildDir ) )
776-
return ABISYM( mBuildOutputPath ) + QStringLiteral( "/i18n" );
805+
if ( !ABISYM( mInitialized ) )
806+
return resolvePkgPath() + QStringLiteral( "/i18n/" );
807+
else if ( ABISYM( mRunningFromBuildDir ) )
808+
return ABISYM( mBuildOutputPath ) + QStringLiteral( "/i18n/" );
777809
else
778810
return ABISYM( mPkgDataPath ) + QStringLiteral( "/i18n/" );
779811
}

src/core/qgsapplication.h

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class QgsPageSizeRegistry;
4848
class QgsLayoutItemRegistry;
4949
class QgsAuthManager;
5050
class QgsNetworkContentFetcherRegistry;
51+
class QTranslator;
5152

5253
/**
5354
* \ingroup core
@@ -742,6 +743,13 @@ class CORE_EXPORT QgsApplication : public QApplication
742743
*/
743744
int maxConcurrentConnectionsPerPool() const;
744745

746+
/**
747+
* Set translation
748+
*
749+
* \since QGIS 3.4
750+
*/
751+
static void setTranslation( QString translation ) { sTranslation = translation; }
752+
745753
/**
746754
* Emits the signal to collect all the strings of .qgs to be included in ts file
747755
*
@@ -829,10 +837,14 @@ class CORE_EXPORT QgsApplication : public QApplication
829837
static QString sUserName;
830838
static QString sUserFullName;
831839
static QString sPlatformName;
840+
static QString sTranslation;
832841

833842
QMap<QString, QIcon> mIconCache;
834843
QMap<Cursor, QCursor> mCursorCache;
835844

845+
QTranslator *mQgisTranslator = nullptr;
846+
QTranslator *mQtTranslator = nullptr;
847+
836848
QgsDataItemProviderRegistry *mDataItemProviderRegistry = nullptr;
837849
QgsAuthManager *mAuthManager = nullptr;
838850

0 commit comments

Comments
 (0)