Skip to content

Commit fc018da

Browse files
committed
QgsProjectTranslator
implementation of abstract class QgsProjectTranslator
1 parent 1bccedd commit fc018da

8 files changed

Lines changed: 60 additions & 8 deletions

File tree

src/core/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,6 @@ SET(QGIS_CORE_MOC_HDRS
636636
qgstaskmanager.h
637637
qgstolerance.h
638638
qgstracer.h
639-
qgstranslationcontext.h
640639
qgstrackedvectorlayertools.h
641640
qgstransaction.h
642641
qgstransactiongroup.h

src/core/layertree/qgslayertreegroup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element, const QgsRe
268268
if ( element.tagName() != QLatin1String( "layer-tree-group" ) )
269269
return nullptr;
270270

271-
QString name = QgsProject::instance()->translate( QStringLiteral( "project:layergroups" ), element.attribute( QStringLiteral( "name" ) ) );
271+
QString name = context.projectTranslator()->translate( QStringLiteral( "project:layergroups" ), element.attribute( QStringLiteral( "name" ) ) );
272272
bool isExpanded = ( element.attribute( QStringLiteral( "expanded" ), QStringLiteral( "1" ) ) == QLatin1String( "1" ) );
273273
bool checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( QStringLiteral( "checked" ) ) ) != Qt::Unchecked;
274274
bool isMutuallyExclusive = element.attribute( QStringLiteral( "mutually-exclusive" ), QStringLiteral( "0" ) ) == QLatin1String( "1" );

src/core/qgsmaplayer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCo
316316
mne = mnl.toElement();
317317

318318
//name can be translated
319-
setName( QgsProject::instance()->translate( QStringLiteral( "project:layers:%1" ).arg( layerElement.namedItem( QStringLiteral( "id" ) ).toElement().text() ), mne.text() ) );
319+
setName( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1" ).arg( layerElement.namedItem( QStringLiteral( "id" ) ).toElement().text() ), mne.text() ) );
320320

321321
//short name
322322
QDomElement shortNameElem = layerElement.firstChildElement( QStringLiteral( "shortname" ) );

src/core/qgsproject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2819,7 +2819,7 @@ void QgsProject::generateTsFile( const QString &locale )
28192819
translationContext.writeTsFile( locale );
28202820
}
28212821

2822-
QString QgsProject::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n )
2822+
QString QgsProject::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n ) const
28232823
{
28242824
if ( !mTranslator )
28252825
{

src/core/qgsproject.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "qgsprojectmetadata.h"
4848
#include "qgstranslationcontext.h"
4949
#include "qgsvectorlayer.h"
50+
#include "qgsprojecttranslator.h"
5051

5152
class QFileInfo;
5253
class QDomDocument;
@@ -85,7 +86,7 @@ class QgsAuxiliaryStorage;
8586
8687
*/
8788

88-
class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator
89+
class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator, public QgsProjectTranslator
8990
{
9091
Q_OBJECT
9192
Q_PROPERTY( QStringList nonIdentifiableLayers READ nonIdentifiableLayers WRITE setNonIdentifiableLayers NOTIFY nonIdentifiableLayersChanged )
@@ -980,7 +981,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
980981
* \param disambiguation it's the disambiguation
981982
* \param n if -1 uses the appropriate form
982983
*/
983-
QString translate( const QString &context, const QString &sourceText, const char *disambiguation = nullptr, int n = -1 );
984+
QString translate( const QString &context, const QString &sourceText, const char *disambiguation = nullptr, int n = -1 ) const override;
984985

985986
signals:
986987

src/core/qgsprojecttranslator.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/***************************************************************************
2+
qgsprojecttranslator.h
3+
4+
---------------------
5+
begin : 24.7.2018
6+
copyright : (C) 2018 by david signer
7+
email : david at opengis dot ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSPROJECTTRANSLATOR_H
17+
#define QGSPROJECTTRANSLATOR_H
18+
19+
#include "qgis_core.h"
20+
#include "qgis_sip.h"
21+
#include "qgis.h"
22+
#include <QString>
23+
24+
/**
25+
* \ingroup core
26+
* This abstract class is to call translate() for project data from wherever QgsReadWriteContext is available.
27+
*
28+
* \since QGIS 3.4
29+
*/
30+
31+
class CORE_EXPORT QgsProjectTranslator
32+
{
33+
public:
34+
35+
/**
36+
* This method needs to be reimplemented in all classes which implement this interface
37+
*
38+
* \since QGIS 3.4
39+
*/
40+
virtual QString translate( const QString &context, const QString &sourceText, const char *disambiguation = nullptr, int n = -1 ) const = 0;
41+
42+
virtual ~QgsProjectTranslator() = default;
43+
};
44+
45+
#endif // QGSPROJECTTRANSLATOR_H

src/core/qgsreadwritecontext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "qgspathresolver.h"
2222
#include "qgis.h"
23+
#include "qgsprojecttranslator.h"
2324

2425
class QgsReadWriteContextCategoryPopper;
2526

@@ -99,6 +100,11 @@ class CORE_EXPORT QgsReadWriteContext
99100
*/
100101
QList<QgsReadWriteContext::ReadWriteMessage> takeMessages();
101102

103+
/**
104+
* Returns the project translator
105+
* \since QGIS 3.2
106+
*/
107+
const QgsProjectTranslator *projectTranslator( ) const { return mProjectTranslator; }
102108

103109
private:
104110
//! Pop the last category
@@ -107,6 +113,7 @@ class CORE_EXPORT QgsReadWriteContext
107113
QgsPathResolver mPathResolver;
108114
QList<ReadWriteMessage> mMessages;
109115
QStringList mCategories = QStringList();
116+
QgsProjectTranslator *mProjectTranslator;
110117

111118
friend class QgsReadWriteContextCategoryPopper;
112119
};

src/core/qgsvectorlayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,13 +1907,13 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
19071907
if ( !aliasElem.attribute( QStringLiteral( "name" ) ).isEmpty() )
19081908
{
19091909
//if it has alias
1910-
alias = QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), aliasElem.attribute( QStringLiteral( "name" ) ) );
1910+
alias = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), aliasElem.attribute( QStringLiteral( "name" ) ) );
19111911
QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ) + " source " + aliasElem.attribute( QStringLiteral( "name" ) ), 1 );
19121912
}
19131913
else
19141914
{
19151915
//if it has no alias, it should be the fields translation
1916-
alias = QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), field );
1916+
alias = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), field );
19171917
QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ) + " source " + field, 1 );
19181918
//if it gets the exact field value, there has been no translation (or not even translation loaded) - so no alias should be generated;
19191919
if ( alias == aliasElem.attribute( QStringLiteral( "field" ) ) )

0 commit comments

Comments
 (0)