From 120ae773b68827e08ae3ef41e201ee86efad2f0a Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Mon, 1 Apr 2019 16:14:03 +0200 Subject: [PATCH] [server][feature][needs-docs] Add env vars to override locale ... ...and group separator: QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE (default '') QGIS_SERVER_SHOW_GROUP_SEPARATOR (default false) --- .../auto_generated/qgsserversettings.sip.in | 14 +++++ src/server/qgsserver.cpp | 23 ++++++++ src/server/qgsserver.h | 3 + src/server/qgsserversettings.cpp | 34 +++++++++++ src/server/qgsserversettings.h | 58 ++++++++++++------- tests/src/python/CMakeLists.txt | 1 + 6 files changed, 111 insertions(+), 22 deletions(-) diff --git a/python/server/auto_generated/qgsserversettings.sip.in b/python/server/auto_generated/qgsserversettings.sip.in index bcc5a1a5baee..d69c36040a64 100644 --- a/python/server/auto_generated/qgsserversettings.sip.in +++ b/python/server/auto_generated/qgsserversettings.sip.in @@ -117,6 +117,20 @@ Returns the cache size. Returns the cache directory. :return: the directory. +%End + + QString overrideSystemLocale() const; +%Docstring +Overrides system locale + +:return: the optional override for system locale. +%End + + bool showGroupSeparator() const; +%Docstring +Show group (thousand) separator + +:return: if group separator must be shown, default to ``False``. %End }; diff --git a/src/server/qgsserver.cpp b/src/server/qgsserver.cpp index a48d0e759689..3ccb18575f2e 100644 --- a/src/server/qgsserver.cpp +++ b/src/server/qgsserver.cpp @@ -156,6 +156,26 @@ QString QgsServer::configPath( const QString &defaultConfigPath, const QString & return cfPath; } +void QgsServer::initLocale() +{ + // System locale override + if ( ! sSettings.overrideSystemLocale().isEmpty() ) + { + QLocale::setDefault( QLocale( sSettings.overrideSystemLocale() ) ); + } + // Number group separator settings + QLocale currentLocale; + if ( sSettings.showGroupSeparator() ) + { + currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator ); + } + else + { + currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator ); + } + QLocale::setDefault( currentLocale ); +} + bool QgsServer::init() { if ( sInitialized ) @@ -191,6 +211,9 @@ bool QgsServer::init() QgsServerLogger::instance()->setLogStderr(); } + // Configure locale + initLocale(); + // log settings currently used sSettings.logSummary(); diff --git a/src/server/qgsserver.h b/src/server/qgsserver.h index 705f6c288487..636194513e7b 100644 --- a/src/server/qgsserver.h +++ b/src/server/qgsserver.h @@ -144,5 +144,8 @@ class SERVER_EXPORT QgsServer //! cache QgsConfigCache *mConfigCache = nullptr; + + //! Initialize locale + static void initLocale(); }; #endif // QGSSERVER_H diff --git a/src/server/qgsserversettings.cpp b/src/server/qgsserversettings.cpp index e5590d26f74d..b0be9feb6a4e 100644 --- a/src/server/qgsserversettings.cpp +++ b/src/server/qgsserversettings.cpp @@ -139,6 +139,29 @@ void QgsServerSettings::initSettings() QVariant() }; mSettings[ sCacheSize.envVar ] = sCacheSize; + + // system locale override + const Setting sOverrideSystemLocale = { QgsServerSettingsEnv::QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE, + QgsServerSettingsEnv::DEFAULT_VALUE, + QStringLiteral( "Override system locale" ), + QStringLiteral( "/locale/userLocale" ), + QVariant::String, + QVariant( "" ), + QVariant() + }; + mSettings[ sOverrideSystemLocale.envVar ] = sOverrideSystemLocale; + + // show group separator + const Setting sShowGroupSeparator = { QgsServerSettingsEnv::QGIS_SERVER_SHOW_GROUP_SEPARATOR, + QgsServerSettingsEnv::DEFAULT_VALUE, + QStringLiteral( "Show group (thousands) separator" ), + QStringLiteral( "/locale/showGroupSeparator" ), + QVariant::String, + QVariant( false ), + QVariant() + }; + mSettings[ sShowGroupSeparator.envVar ] = sShowGroupSeparator; + } void QgsServerSettings::load() @@ -319,3 +342,14 @@ QString QgsServerSettings::cacheDirectory() const { return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_DIRECTORY ).toString(); } + +QString QgsServerSettings::overrideSystemLocale() const +{ + return value( QgsServerSettingsEnv::QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE ).toString(); +} + +bool QgsServerSettings::showGroupSeparator() const +{ + return value( QgsServerSettingsEnv::QGIS_SERVER_SHOW_GROUP_SEPARATOR ).toBool(); +} + diff --git a/src/server/qgsserversettings.h b/src/server/qgsserversettings.h index 37c29dba7684..68960006480e 100644 --- a/src/server/qgsserversettings.h +++ b/src/server/qgsserversettings.h @@ -60,7 +60,9 @@ class SERVER_EXPORT QgsServerSettingsEnv : public QObject QGIS_PROJECT_FILE, MAX_CACHE_LAYERS, QGIS_SERVER_CACHE_DIRECTORY, - QGIS_SERVER_CACHE_SIZE + QGIS_SERVER_CACHE_SIZE, + QGIS_SERVER_SHOW_GROUP_SEPARATOR, //! Show group (thousands) separator when formatting numeric values, defaults to FALSE + QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE, //! Override system locale }; Q_ENUM( EnvVar ) }; @@ -88,41 +90,41 @@ class SERVER_EXPORT QgsServerSettings /** * Constructor. - */ + */ QgsServerSettings(); /** * Load settings according to current environment variables. - */ + */ void load(); /** * Load setting for a specific environment variable name. - * \returns TRUE if loading is successful, FALSE in case of an invalid name. - */ + * \returns TRUE if loading is successful, FALSE in case of an invalid name. + */ bool load( const QString &envVarName ); /** * Log a summary of settings currently loaded. - */ + */ void logSummary() const; /** * Returns the ini file loaded by QSetting. - * \returns the path of the ini file or an empty string if none is loaded. - */ + * \returns the path of the ini file or an empty string if none is loaded. + */ QString iniFile() const; /** * Returns parallel rendering setting. - * \returns TRUE if parallel rendering is activated, FALSE otherwise. - */ + * \returns TRUE if parallel rendering is activated, FALSE otherwise. + */ bool parallelRendering() const; /** * Returns the maximum number of threads to use. - * \returns the number of threads. - */ + * \returns the number of threads. + */ int maxThreads() const; /** @@ -133,20 +135,20 @@ class SERVER_EXPORT QgsServerSettings /** * Returns the log level. - * \returns the log level. - */ + * \returns the log level. + */ Qgis::MessageLevel logLevel() const; /** * Returns the QGS project file to use. - * \returns the path of the QGS project or an empty string if none is defined. - */ + * \returns the path of the QGS project or an empty string if none is defined. + */ QString projectFile() const; /** * Returns the log file. - * \returns the path of the log file or an empty string if none is defined. - */ + * \returns the path of the log file or an empty string if none is defined. + */ QString logFile() const; /** @@ -158,16 +160,28 @@ class SERVER_EXPORT QgsServerSettings /** * Returns the cache size. - * \returns the cache size. - */ + * \returns the cache size. + */ qint64 cacheSize() const; /** * Returns the cache directory. - * \returns the directory. - */ + * \returns the directory. + */ QString cacheDirectory() const; + /** + * Overrides system locale + * \returns the optional override for system locale. + */ + QString overrideSystemLocale() const; + + /** + * Show group (thousand) separator + * \returns if group separator must be shown, default to FALSE. + */ + bool showGroupSeparator() const; + private: void initSettings(); QVariant value( QgsServerSettingsEnv::EnvVar envVar ) const; diff --git a/tests/src/python/CMakeLists.txt b/tests/src/python/CMakeLists.txt index b8ca41bd819b..14f13e9ab47d 100644 --- a/tests/src/python/CMakeLists.txt +++ b/tests/src/python/CMakeLists.txt @@ -300,6 +300,7 @@ IF (WITH_SERVER) ADD_PYTHON_TEST(PyQgsServerWMTS test_qgsserver_wmts.py) ADD_PYTHON_TEST(PyQgsServerWFS test_qgsserver_wfs.py) ADD_PYTHON_TEST(PyQgsServerWFST test_qgsserver_wfst.py) + ADD_PYTHON_TEST(PyQgsServerLocaleOverride test_qgsserver_locale_override.py) ADD_PYTHON_TEST(PyQgsOfflineEditingWFS test_offline_editing_wfs.py) ADD_PYTHON_TEST(PyQgsAuthManagerPasswordOWSTest test_authmanager_password_ows.py) ADD_PYTHON_TEST(PyQgsAuthManagerPKIOWSTest test_authmanager_pki_ows.py)