Skip to content

Commit

Permalink
Fix QTranslator not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ authored and nyalldawson committed Apr 24, 2023
1 parent e29b054 commit 3c5f120
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 109 deletions.
134 changes: 69 additions & 65 deletions src/app/main.cpp
Expand Up @@ -1010,10 +1010,77 @@ int main( int argc, char *argv[] )
}
}

// Create the application. At this point, the profile is not yet selected
// But we need the Qt Application to be created to be able to display the
// profile selection dialog if needed
QgsApplication myApp( argc, argv, myUseGuiFlag, QString(), QStringLiteral( "desktop" ) );

QString rootProfileFolder = QgsUserProfileManager::resolveProfilesFolder( configLocalStorageLocation );
QgsUserProfileManager manager( rootProfileFolder );

// If profile name was not explicitly set, use the policy to determine which profile to use
if ( profileName.isEmpty() )
{

// If no profiles exist, use the default profile
if ( manager.allProfiles().isEmpty() )
{
profileName = manager.defaultProfileName();
}
else
{
switch ( manager.userProfileSelectionPolicy() )
{
// Use the last closed profile (default behavior prior to QGIS 3.32)
case QgsUserProfileManager::UserProfileSelectionPolicy::LastProfile:
profileName = manager.lastProfileName();
// If last used profile no longer exists, use the default profile
if ( !manager.profileExists( profileName ) )
{
profileName = manager.defaultProfileName();
}
break;

// Ask the user to select a profile (if more than one exists)
case QgsUserProfileManager::UserProfileSelectionPolicy::AskUser:
{
if ( manager.allProfiles().size() == 1 )
{
profileName = manager.allProfiles()[0];
break;
}
QgsUserProfileSelectionDialog dlg( &manager );
if ( dlg.exec() == QDialog::Accepted )
{
profileName = dlg.selectedProfileName();
}
else
{
// Exit QGIS if the user cancels the profile selection dialog
return 0;
}
break;
}

// Use the default profile
case QgsUserProfileManager::UserProfileSelectionPolicy::DefaultProfile:
default:
profileName = manager.defaultProfileName();
break;
}
}
}

// Calling getProfile() will create the profile if it doesn't exist, and init the QgsSettings
QgsUserProfile *profile = manager.getProfile( profileName, true );
QString profileFolder = profile->folder();
profileName = profile->name();
delete profile;



{
/* Translation file for QGIS.
*/
// The profile is selected, we can now set up the translation file for QGIS.
QString myUserTranslation = QgsApplication::settingsLocaleUserLocale->value();
QString myGlobalLocale = QgsApplication::settingsLocaleGlobalLocale->value();
bool myShowGroupSeparatorFlag = false; // Default to false
Expand Down Expand Up @@ -1076,69 +1143,6 @@ int main( int argc, char *argv[] )
QgsApplication::setTranslation( translationCode );
}

QgsApplication myApp( argc, argv, myUseGuiFlag, QString(), QStringLiteral( "desktop" ) );

QString rootProfileFolder = QgsUserProfileManager::resolveProfilesFolder( configLocalStorageLocation );
QgsUserProfileManager manager( rootProfileFolder );

// If profile name was not explicitly set, use the policy to determine which profile to use
if ( profileName.isEmpty() )
{

// If no profiles exist, use the default profile
if ( manager.allProfiles().isEmpty() )
{
profileName = manager.defaultProfileName();
}
else
{
switch ( manager.userProfileSelectionPolicy() )
{
// Use the last closed profile (default behavior prior to QGIS 3.32)
case QgsUserProfileManager::UserProfileSelectionPolicy::LastProfile:
profileName = manager.lastProfileName();
// If last used profile no longer exists, use the default profile
if ( !manager.profileExists( profileName ) )
{
profileName = manager.defaultProfileName();
}
break;

// Ask the user to select a profile (if more than one exists)
case QgsUserProfileManager::UserProfileSelectionPolicy::AskUser:
{
if ( manager.allProfiles().size() == 1 )
{
profileName = manager.allProfiles()[0];
break;
}
QgsUserProfileSelectionDialog dlg( &manager );
if ( dlg.exec() == QDialog::Accepted )
{
profileName = dlg.selectedProfileName();
}
else
{
// Exit QGIS if the user cancels the profile selection dialog
return 0;
}
break;
}

// Use the default profile
case QgsUserProfileManager::UserProfileSelectionPolicy::DefaultProfile:
default:
profileName = manager.defaultProfileName();
break;
}
}
}

QgsUserProfile *profile = manager.getProfile( profileName, true );
QString profileFolder = profile->folder();
profileName = profile->name();
delete profile;

// Set locale to emit QgsApplication's localeChanged signal
QgsApplication::setLocale( QLocale() );

Expand Down
96 changes: 52 additions & 44 deletions src/core/qgsapplication.cpp
Expand Up @@ -202,50 +202,6 @@ 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
{
QgsDebugMsgLevel( QStringLiteral( "loading of qgis translation failed %1/qgis_%2" ).arg( i18nPath(), *sTranslation() ), 2 );
}

/* 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.
*/
QString qtTranslationsPath = QLibraryInfo::location( QLibraryInfo::TranslationsPath );
#ifdef __MINGW32__
QString prefix = QDir( QString( "%1/../" ).arg( QApplication::applicationDirPath() ) ).absolutePath();
qtTranslationsPath = prefix + qtTranslationsPath.mid( QLibraryInfo::location( QLibraryInfo::PrefixPath ).length() );
#endif

mQtTranslator = new QTranslator();
if ( mQtTranslator->load( QStringLiteral( "qt_" ) + *sTranslation(), qtTranslationsPath ) )
{
installTranslator( mQtTranslator );
}
else
{
QgsDebugMsgLevel( QStringLiteral( "loading of qt translation failed %1/qt_%2" ).arg( qtTranslationsPath, *sTranslation() ), 2 );
}

mQtBaseTranslator = new QTranslator();
if ( mQtBaseTranslator->load( QStringLiteral( "qtbase_" ) + *sTranslation(), qtTranslationsPath ) )
{
installTranslator( mQtBaseTranslator );
}
else
{
QgsDebugMsgLevel( QStringLiteral( "loading of qtbase translation failed %1/qt_%2" ).arg( qtTranslationsPath, *sTranslation() ), 2 );
}
}

mApplicationMembers = new ApplicationMembers();

*sProfilePath() = profileFolder;
Expand Down Expand Up @@ -486,6 +442,54 @@ void QgsApplication::init( QString profileFolder )
ABISYM( mInitialized ) = true;
}


void QgsApplication::installTranslators()
{
if ( *sTranslation() != QLatin1String( "C" ) )
{
mQgisTranslator = new QTranslator();
if ( mQgisTranslator->load( QStringLiteral( "qgis_" ) + *sTranslation(), i18nPath() ) )
{
installTranslator( mQgisTranslator );
}
else
{
QgsDebugMsgLevel( QStringLiteral( "loading of qgis translation failed %1/qgis_%2" ).arg( i18nPath(), *sTranslation() ), 2 );
}

/* 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.
*/
QString qtTranslationsPath = QLibraryInfo::location( QLibraryInfo::TranslationsPath );
#ifdef __MINGW32__
QString prefix = QDir( QString( "%1/../" ).arg( QApplication::applicationDirPath() ) ).absolutePath();
qtTranslationsPath = prefix + qtTranslationsPath.mid( QLibraryInfo::location( QLibraryInfo::PrefixPath ).length() );
#endif

mQtTranslator = new QTranslator();
if ( mQtTranslator->load( QStringLiteral( "qt_" ) + *sTranslation(), qtTranslationsPath ) )
{
installTranslator( mQtTranslator );
}
else
{
QgsDebugMsgLevel( QStringLiteral( "loading of qt translation failed %1/qt_%2" ).arg( qtTranslationsPath, *sTranslation() ), 2 );
}

mQtBaseTranslator = new QTranslator();
if ( mQtBaseTranslator->load( QStringLiteral( "qtbase_" ) + *sTranslation(), qtTranslationsPath ) )
{
installTranslator( mQtBaseTranslator );
}
else
{
QgsDebugMsgLevel( QStringLiteral( "loading of qtbase translation failed %1/qt_%2" ).arg( qtTranslationsPath, *sTranslation() ), 2 );
}
}
}

QgsApplication::~QgsApplication()
{
mApplicationMembers->mSettingsRegistryCore->backwardCompatibility();
Expand Down Expand Up @@ -2074,6 +2078,10 @@ int QgsApplication::maxConcurrentConnectionsPerPool() const
void QgsApplication::setTranslation( const QString &translation )
{
*sTranslation() = translation;
if ( auto app = QgsApplication::instance() )
{
app->installTranslators();

This comment has been minimized.

Copy link
@phidrho

phidrho Jun 20, 2023

Contributor

Hi @YoannQDQ
could this be the reason my fresh installation of QGIS does not start?
I got these messages in debug mode:

C:\src\OSGeo4W\src\qgis-dev\qgis\src\app\main.cpp(514) : (main) [0ms] Starting qgis main
C:\src\OSGeo4W\src\qgis-dev\qgis\src\core\qgsapplication.cpp(470) : (QgsApplication::installTranslators) [13ms] loading of qgis translation failed C:/OSGeo4W/apps/qgis-dev/./i18n//qgis_hr_HR
C:\src\OSGeo4W\src\qgis-dev\qgis\src\core\qgsapplication.cpp(470) : (QgsApplication::installTranslators) [3ms] loading of qgis translation failed C:/OSGeo4W/apps/qgis-dev/./i18n//qgis_hr_HR

see messages on the mailing list: https://lists.osgeo.org/pipermail/qgis-developer/2023-June/065927.html

}
}

QString QgsApplication::translation() const
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsapplication.h
Expand Up @@ -1205,6 +1205,12 @@ class CORE_EXPORT QgsApplication : public QApplication

static void invalidateCaches();

/**
* Installs the translators (qgis, qt and qt_base) for the current locale.
* \since QGIS 3.22
*/
void installTranslators() SIP_SKIP;

friend class TestQgsApplication;
};

Expand Down

0 comments on commit 3c5f120

Please sign in to comment.