Skip to content

Commit 8a7929e

Browse files
committed
add translation strings for expression builder
1 parent 8841d32 commit 8a7929e

File tree

5 files changed

+102
-89
lines changed

5 files changed

+102
-89
lines changed

src/core/qgsexpression.cpp

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -370,46 +370,53 @@ static QVariant fcnGeomPerimeter( const QVariantList& , QgsFeature* f, QgsExpres
370370
return QVariant( calc->measurePerimeter( f->geometry() ) );
371371
}
372372

373-
typedef QgsExpression::FunctionDef FnDef;
374-
375-
FnDef QgsExpression::BuiltinFunctions[] =
376-
{
377-
// math
378-
FnDef( "sqrt", 1, fcnSqrt, "Math" ),
379-
FnDef( "sin", 1, fcnSin, "Math" ),
380-
FnDef( "cos", 1, fcnCos, "Math" ),
381-
FnDef( "tan", 1, fcnTan, "Math" ),
382-
FnDef( "asin", 1, fcnAsin, "Math" ),
383-
FnDef( "acos", 1, fcnAcos, "Math" ),
384-
FnDef( "atan", 1, fcnAtan, "Math" ),
385-
FnDef( "atan2", 2, fcnAtan2, "Math" ),
386-
FnDef( "exp", 1, fcnExp, "Math" ),
387-
FnDef( "ln", 1, fcnLn, "Math" ),
388-
FnDef( "log10", 1, fcnLog10, "Math" ),
389-
FnDef( "log", 2, fcnLog, "Math" ),
390-
// casts
391-
FnDef( "toint", 1, fcnToInt, "Conversions" ),
392-
FnDef( "toreal", 1, fcnToReal, "Conversions" ),
393-
FnDef( "tostring", 1, fcnToString, "Conversions" ),
394-
// string manipulation
395-
FnDef( "lower", 1, fcnLower, "String" ),
396-
FnDef( "upper", 1, fcnUpper, "String" ),
397-
FnDef( "length", 1, fcnLength, "String" ),
398-
FnDef( "replace", 3, fcnReplace, "String" ),
399-
FnDef( "regexp_replace", 3, fcnRegexpReplace, "String" ),
400-
FnDef( "substr", 3, fcnSubstr, "String" ),
401-
// geometry accessors
402-
FnDef( "xat", 1, fcnXat, "Geometry", "", true ),
403-
FnDef( "yat", 1, fcnYat, "Geometry", "", true ),
404-
FnDef( "$area", 0, fcnGeomArea, "Geometry", "", true ),
405-
FnDef( "$length", 0, fcnGeomLength, "Geometry", "", true ),
406-
FnDef( "$perimeter", 0, fcnGeomPerimeter, "Geometry", "", true ),
407-
FnDef( "$x", 0, fcnX, "Geometry", "", true ),
408-
FnDef( "$y", 0, fcnY, "Geometry", "" , true ),
409-
// special columns
410-
FnDef( "$rownum", 0, fcnRowNumber, "Record" ),
411-
FnDef( "$id", 0, fcnFeatureId, "Record" )
412-
};
373+
QList<QgsExpression::FunctionDef> QgsExpression::gmBuiltinFunctions;
374+
375+
const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions()
376+
{
377+
if ( gmBuiltinFunctions.isEmpty() )
378+
{
379+
// math
380+
gmBuiltinFunctions
381+
<< FunctionDef( "sqrt", 1, fcnSqrt, QObject::tr( "Math" ) )
382+
<< FunctionDef( "sin", 1, fcnSin, QObject::tr( "Math" ) )
383+
<< FunctionDef( "cos", 1, fcnCos, QObject::tr( "Math" ) )
384+
<< FunctionDef( "tan", 1, fcnTan, QObject::tr( "Math" ) )
385+
<< FunctionDef( "asin", 1, fcnAsin, QObject::tr( "Math" ) )
386+
<< FunctionDef( "acos", 1, fcnAcos, QObject::tr( "Math" ) )
387+
<< FunctionDef( "atan", 1, fcnAtan, QObject::tr( "Math" ) )
388+
<< FunctionDef( "atan2", 2, fcnAtan2, QObject::tr( "Math" ) )
389+
<< FunctionDef( "exp", 1, fcnExp, QObject::tr( "Math" ) )
390+
<< FunctionDef( "ln", 1, fcnLn, QObject::tr( "Math" ) )
391+
<< FunctionDef( "log10", 1, fcnLog10, QObject::tr( "Math" ) )
392+
<< FunctionDef( "log", 2, fcnLog, QObject::tr( "Math" ) )
393+
// casts
394+
<< FunctionDef( "toint", 1, fcnToInt, QObject::tr( "Conversions" ) )
395+
<< FunctionDef( "toreal", 1, fcnToReal, QObject::tr( "Conversions" ) )
396+
<< FunctionDef( "tostring", 1, fcnToString, QObject::tr( "Conversions" ) )
397+
// string manipulation
398+
<< FunctionDef( "lower", 1, fcnLower, QObject::tr( "String" ) )
399+
<< FunctionDef( "upper", 1, fcnUpper, QObject::tr( "String" ) )
400+
<< FunctionDef( "length", 1, fcnLength, QObject::tr( "String" ) )
401+
<< FunctionDef( "replace", 3, fcnReplace, QObject::tr( "String" ) )
402+
<< FunctionDef( "regexp_replace", 3, fcnRegexpReplace, QObject::tr( "String" ) )
403+
<< FunctionDef( "substr", 3, fcnSubstr, QObject::tr( "String" ) )
404+
// geometry accessors
405+
<< FunctionDef( "xat", 1, fcnXat, QObject::tr( "Geometry" ), "", true )
406+
<< FunctionDef( "yat", 1, fcnYat, QObject::tr( "Geometry" ), "", true )
407+
<< FunctionDef( "$area", 0, fcnGeomArea, QObject::tr( "Geometry" ), "", true )
408+
<< FunctionDef( "$length", 0, fcnGeomLength, QObject::tr( "Geometry" ), "", true )
409+
<< FunctionDef( "$perimeter", 0, fcnGeomPerimeter, QObject::tr( "Geometry" ), "", true )
410+
<< FunctionDef( "$x", 0, fcnX, QObject::tr( "Geometry" ), "", true )
411+
<< FunctionDef( "$y", 0, fcnY, QObject::tr( "Geometry" ), "" , true )
412+
// special columns
413+
<< FunctionDef( "$rownum", 0, fcnRowNumber, QObject::tr( "Record" ) )
414+
<< FunctionDef( "$id", 0, fcnFeatureId, QObject::tr( "Record" ) )
415+
;
416+
}
417+
418+
return gmBuiltinFunctions;
419+
}
413420

414421

415422
bool QgsExpression::isFunctionName( QString name )
@@ -422,15 +429,15 @@ int QgsExpression::functionIndex( QString name )
422429
int count = functionCount();
423430
for ( int i = 0; i < count; i++ )
424431
{
425-
if ( QString::compare( name, BuiltinFunctions[i].mName, Qt::CaseInsensitive ) == 0 )
432+
if ( QString::compare( name, BuiltinFunctions()[i].mName, Qt::CaseInsensitive ) == 0 )
426433
return i;
427434
}
428435
return -1;
429436
}
430437

431438
int QgsExpression::functionCount()
432439
{
433-
return ( sizeof( BuiltinFunctions ) / sizeof( FunctionDef ) );
440+
return BuiltinFunctions().size();
434441
}
435442

436443

@@ -527,7 +534,8 @@ QVariant QgsExpression::evaluate( QgsFeature* f, const QgsFieldMap& fields )
527534

528535
QString QgsExpression::dump() const
529536
{
530-
if ( !mRootNode ) return "(no root)";
537+
if ( !mRootNode )
538+
return QObject::tr( "(no root)" );
531539

532540
return mRootNode->dump();
533541
}
@@ -861,7 +869,7 @@ QString QgsExpression::NodeInOperator::dump() const
861869

862870
QVariant QgsExpression::NodeFunction::eval( QgsExpression* parent, QgsFeature* f )
863871
{
864-
const FunctionDef& fd = BuiltinFunctions[mFnIndex];
872+
const FunctionDef& fd = BuiltinFunctions()[mFnIndex];
865873

866874
// evaluate arguments
867875
QVariantList argValues;
@@ -900,7 +908,7 @@ bool QgsExpression::NodeFunction::prepare( QgsExpression* parent, const QgsField
900908

901909
QString QgsExpression::NodeFunction::dump() const
902910
{
903-
const FnDef& fd = BuiltinFunctions[mFnIndex];
911+
const FunctionDef& fd = BuiltinFunctions()[mFnIndex];
904912
if ( fd.mParams == 0 )
905913
return fd.mName; // special column
906914
else
@@ -930,7 +938,7 @@ QString QgsExpression::NodeLiteral::dump() const
930938
case QVariant::Int: return QString::number( mValue.toInt() );
931939
case QVariant::Double: return QString::number( mValue.toDouble() );
932940
case QVariant::String: return QString( "'%1'" ).arg( mValue.toString() );
933-
default: return QString( "[unsupported type;%1; value:%2]" ).arg( mValue.typeName() ).arg( mValue.toString() );
941+
default: return QObject::tr( "[unsupported type;%1; value:%2]" ).arg( mValue.typeName() ).arg( mValue.toString() );
934942
}
935943
}
936944

src/core/qgsexpression.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <QStringList>
2020
#include <QVariant>
21+
#include <QList>
2122

2223
#include "qgsfield.h"
2324

@@ -122,7 +123,7 @@ class CORE_EXPORT QgsExpression
122123

123124
//! Return calculator used for distance and area calculations
124125
//! (used by internal functions)
125-
QgsDistanceArea* geomCalculator() { if ( mCalc == NULL ) initGeomCalculator(); return mCalc; }
126+
QgsDistanceArea* geomCalculator() { if ( !mCalc ) initGeomCalculator(); return mCalc; }
126127

127128
//
128129

@@ -186,7 +187,8 @@ class CORE_EXPORT QgsExpression
186187
QString mHelpText;
187188
};
188189

189-
static FunctionDef BuiltinFunctions[];
190+
static const QList<FunctionDef> &BuiltinFunctions();
191+
static QList<FunctionDef> gmBuiltinFunctions;
190192

191193
// tells whether the identifier is a name of existing function
192194
static bool isFunctionName( QString name );
@@ -301,7 +303,7 @@ class CORE_EXPORT QgsExpression
301303
virtual QVariant eval( QgsExpression* parent, QgsFeature* f );
302304
virtual QString dump() const;
303305
virtual QStringList referencedColumns() const { QStringList lst; if ( !mArgs ) return lst; foreach( Node* n, mArgs->list() ) lst.append( n->referencedColumns() ); return lst; }
304-
virtual bool needsGeometry() const { bool needs = BuiltinFunctions[mFnIndex].mUsesGeometry; if ( mArgs ) { foreach( Node* n, mArgs->list() ) needs |= n->needsGeometry(); } return needs; }
306+
virtual bool needsGeometry() const { bool needs = BuiltinFunctions()[mFnIndex].mUsesGeometry; if ( mArgs ) { foreach( Node* n, mArgs->list() ) needs |= n->needsGeometry(); } return needs; }
305307
protected:
306308
//QString mName;
307309
int mFnIndex;

src/core/qgsexpressionparser.yy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ expression:
160160
exp_error("Function is not known");
161161
YYERROR;
162162
}
163-
if (QgsExpression::BuiltinFunctions[fnIndex].mParams != $3->count())
163+
if (QgsExpression::BuiltinFunctions()[fnIndex].mParams != $3->count())
164164
{
165165
exp_error("Function is called with wrong number of arguments");
166166
YYERROR;

src/gui/qgsexpressionbuilderwidget.cpp

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,38 +52,41 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
5252

5353

5454
// TODO Can we move this stuff to QgsExpression, like the functions?
55-
registerItem( "Operators", "+", " + " );
56-
registerItem( "Operators", "-", " -" );
57-
registerItem( "Operators", "*", " * " );
58-
registerItem( "Operators", "/", " / " );
59-
registerItem( "Operators", "%", " % " );
60-
registerItem( "Operators", "^", " ^ " );
61-
registerItem( "Operators", "=", " = " );
62-
registerItem( "Operators", ">", " > " );
63-
registerItem( "Operators", "<", " < " );
64-
registerItem( "Operators", "<>", " <> " );
65-
registerItem( "Operators", "<=", " <= " );
66-
registerItem( "Operators", ">=", " >= " );
67-
registerItem( "Operators", "||", " || ", "<b>|| (String Concatenation)</b> "
68-
"<br> Joins two values together into a string "
69-
"<br> <i>Usage:</i><br>'Dia' || Diameter" );
70-
registerItem( "Operators", "LIKE", " LIKE " );
71-
registerItem( "Operators", "ILIKE", " ILIKE " );
72-
registerItem( "Operators", "IS", " IS NOT " );
73-
registerItem( "Operators", "OR", " OR " );
74-
registerItem( "Operators", "AND", " AND " );
75-
registerItem( "Operators", "NOT", " NOT " );
55+
registerItem( tr( "Operators" ), "+", " + " );
56+
registerItem( tr( "Operators" ), "-", " -" );
57+
registerItem( tr( "Operators" ), "*", " * " );
58+
registerItem( tr( "Operators" ), "/", " / " );
59+
registerItem( tr( "Operators" ), "%", " % " );
60+
registerItem( tr( "Operators" ), "^", " ^ " );
61+
registerItem( tr( "Operators" ), "=", " = " );
62+
registerItem( tr( "Operators" ), ">", " > " );
63+
registerItem( tr( "Operators" ), "<", " < " );
64+
registerItem( tr( "Operators" ), "<>", " <> " );
65+
registerItem( tr( "Operators" ), "<=", " <= " );
66+
registerItem( tr( "Operators" ), ">=", " >= " );
67+
registerItem( tr( "Operators" ), "||", " || ",
68+
QString( "<b>|| %1</b><br><i>%2</i><br><i>%3:</i>%4" )
69+
.arg( tr( "(String Concatenation)" ) )
70+
.arg( tr( "Joins two values together into a string" ) )
71+
.arg( tr( "Usage" ) )
72+
.arg( tr( "'Dia' || Diameter" ) ) );
73+
registerItem( tr( "Operators" ), "LIKE", " LIKE " );
74+
registerItem( tr( "Operators" ), "ILIKE", " ILIKE " );
75+
registerItem( tr( "Operators" ), "IS", " IS NOT " );
76+
registerItem( tr( "Operators" ), "OR", " OR " );
77+
registerItem( tr( "Operators" ), "AND", " AND " );
78+
registerItem( tr( "Operators" ), "NOT", " NOT " );
7679

7780

7881
// Load the fuctions from the QgsExpression class
7982
int count = QgsExpression::functionCount();
8083
for ( int i = 0; i < count; i++ )
8184
{
82-
QgsExpression::FunctionDef func = QgsExpression::BuiltinFunctions[i];
85+
QgsExpression::FunctionDef func = QgsExpression::BuiltinFunctions()[i];
8386
QString name = func.mName;
8487
if ( func.mParams >= 1 )
8588
name += "(";
86-
registerItem( func.mGroup, func.mName, " " + name + " ");
89+
registerItem( func.mGroup, func.mName, " " + name + " " );
8790
};
8891
}
8992

@@ -153,7 +156,7 @@ void QgsExpressionBuilderWidget::loadFieldNames()
153156
for ( ; fieldIt != fieldMap.constEnd(); ++fieldIt )
154157
{
155158
QString fieldName = fieldIt.value().name();
156-
registerItem( "Fields", fieldName, " " + fieldName + " ", "", QgsExpressionItem::Field );
159+
registerItem( tr( "Fields" ), fieldName, " " + fieldName + " ", "", QgsExpressionItem::Field );
157160
}
158161
}
159162

@@ -238,31 +241,31 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
238241
{
239242
if ( !mFeature.isValid() )
240243
{
241-
mLayer->select( mLayer->pendingAllAttributesList() );
242-
mLayer->nextFeature( mFeature );
244+
mLayer->select( mLayer->pendingAllAttributesList() );
245+
mLayer->nextFeature( mFeature );
243246
}
244247

245248
if ( mFeature.isValid() )
246249
{
247-
QVariant value = exp.evaluate( &mFeature, mLayer->pendingFields() );
248-
if ( !exp.hasEvalError() )
249-
lblPreview->setText( value.toString() );
250+
QVariant value = exp.evaluate( &mFeature, mLayer->pendingFields() );
251+
if ( !exp.hasEvalError() )
252+
lblPreview->setText( value.toString() );
250253
}
251254
else
252255
{
253-
// The feautre is invaild because we don't have one but that doesn't mean user can't
254-
// build a expression string. They just get no preview.
255-
lblPreview->setText("");
256+
// The feautre is invaild because we don't have one but that doesn't mean user can't
257+
// build a expression string. They just get no preview.
258+
lblPreview->setText( "" );
256259
}
257260
}
258261

259262
if ( exp.hasParserError() || exp.hasEvalError() )
260263
{
261-
QString tooltip = "<b>Parser Error:</b> <br>" + exp.parserErrorString();
264+
QString tooltip = QString( "<b>%1:</b><br>%2" ).arg( tr( "Parser Error" ) ).arg( exp.parserErrorString() );
262265
if ( exp.hasEvalError() )
263-
tooltip += "<br><br> <b>Eval Error:</b> <br>" + exp.evalErrorString();
266+
tooltip += QString( "<br><br><b>%1:</b><br>%2" ).arg( tr( "Eval Error" ) ).arg( exp.evalErrorString() );
264267

265-
lblPreview->setText( "Expression is invaild <a href=""more"">(more info)</a>" );
268+
lblPreview->setText( tr( "Expression is invalid <a href=""more"">(more info)</a>" ) );
266269
lblPreview->setStyleSheet( "color: rgba(255, 6, 10, 255);" );
267270
txtExpressionString->setToolTip( tooltip );
268271
lblPreview->setToolTip( tooltip );
@@ -291,7 +294,7 @@ void QgsExpressionBuilderWidget::on_lblPreview_linkActivated( QString link )
291294
{
292295
Q_UNUSED( link );
293296
QgsMessageViewer * mv = new QgsMessageViewer( this );
294-
mv->setWindowTitle( "More info on expression error" );
297+
mv->setWindowTitle( tr( "More info on expression error" ) );
295298
mv->setMessageAsHtml( txtExpressionString->toolTip() );
296299
mv->exec();
297300
}
@@ -395,13 +398,13 @@ QString QgsExpressionBuilderWidget::loadFunctionHelp( QgsExpressionItem* functio
395398
// translate this for us message
396399
if ( !lang.contains( "en_" ) )
397400
{
398-
helpContents = "<i>" + tr( "This help file is not available in your language %1. If you would like to translate it, please contact the QGIS development team." ).arg( lang ) + "</i><hr />";
401+
helpContents = "<i>" + tr( "This help file is not available in your language %1. If you would like to translate it, please contact the QGIS development team." ).arg( lang ) + "</i><hr />";
399402
}
400403

401404
}
402405
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
403406
{
404-
helpContents = tr( "This help file does not exist for your language:<p><b>%1</b><p>If you would like to create it, contact the QGIS development team" )
407+
helpContents = tr( "This help file does not exist for your language:<p><b>%1</b><p>If you would like to create it, contact the QGIS development team" )
405408
.arg( fullHelpPath );
406409
}
407410
else

src/helpviewer/qgshelpviewer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void QgsHelpViewer::loadContext( const QString &contextId )
137137
// translate this for us message
138138
if ( !lang.contains( "en_" ) )
139139
{
140-
helpContents = "<i>" + tr( "This help file is not available in your language %1. If you would like to translate it, please contact the QGIS development team." ).arg( lang ) + "</i><hr />";
140+
helpContents = "<i>" + tr( "This help file is not available in your language %1. If you would like to translate it, please contact the QGIS development team." ).arg( lang ) + "</i><hr />";
141141
}
142142
}
143143
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )

0 commit comments

Comments
 (0)