Skip to content

Commit c2bd4b9

Browse files
committed
[FEATURE] new global expression variable $qgis_locale
Returns current locale used by QGIS. By default this is current system locale. If user enabled locale override in QGIS settings overriden locale will be returned.
1 parent 68e09d4 commit c2bd4b9

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/core/qgsapplication.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <QIcon>
4949
#include <QPixmap>
5050
#include <QThreadPool>
51+
#include <QLocale>
5152

5253
#ifndef Q_OS_WIN
5354
#include <netinet/in.h>
@@ -902,6 +903,20 @@ QString QgsApplication::platform()
902903
return sPlatformName;
903904
}
904905

906+
QString QgsApplication::locale()
907+
{
908+
QSettings settings;
909+
bool overrideLocale = settings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool();
910+
if ( overrideLocale )
911+
{
912+
return settings.value( QStringLiteral( "locale/userLocale" ), QString() ).toString();
913+
}
914+
else
915+
{
916+
return QLocale::system().name().left( 2 );
917+
}
918+
}
919+
905920
QString QgsApplication::userThemesFolder()
906921
{
907922
return qgisSettingsDirPath() + QStringLiteral( "/themes" );

src/core/qgsapplication.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ class CORE_EXPORT QgsApplication : public QApplication
241241
*/
242242
static QString platform();
243243

244+
/** Returns the QGIS locale.
245+
* @note added in QGIS 3.0
246+
*/
247+
static QString locale();
248+
244249
//! Returns the path to user's themes folder
245250
static QString userThemesFolder();
246251

src/core/qgsexpressioncontext.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ QgsExpressionContextScope* QgsExpressionContextUtils::globalScope()
524524
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_release_name" ), Qgis::QGIS_RELEASE_NAME, true ) );
525525
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_platform" ), QgsApplication::platform(), true ) );
526526
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_os_name" ), QgsApplication::osName(), true ) );
527+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "qgis_locale" ), QgsApplication::locale(), true ) );
527528
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "user_account_name" ), QgsApplication::userLoginName(), true ) );
528529
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "user_full_name" ), QgsApplication::userFullName(), true ) );
529530

0 commit comments

Comments
 (0)