Skip to content
Permalink
Browse files

QgsProjectTranslator

implementation of abstract class QgsProjectTranslator
  • Loading branch information
signedav committed Jul 24, 2018
1 parent 1bccedd commit fc018daa3fff25d04cd012bca4f057e0088e73ea
@@ -636,7 +636,6 @@ SET(QGIS_CORE_MOC_HDRS
qgstaskmanager.h
qgstolerance.h
qgstracer.h
qgstranslationcontext.h
qgstrackedvectorlayertools.h
qgstransaction.h
qgstransactiongroup.h
@@ -268,7 +268,7 @@ QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element, const QgsRe
if ( element.tagName() != QLatin1String( "layer-tree-group" ) )
return nullptr;

QString name = QgsProject::instance()->translate( QStringLiteral( "project:layergroups" ), element.attribute( QStringLiteral( "name" ) ) );
QString name = context.projectTranslator()->translate( QStringLiteral( "project:layergroups" ), element.attribute( QStringLiteral( "name" ) ) );
bool isExpanded = ( element.attribute( QStringLiteral( "expanded" ), QStringLiteral( "1" ) ) == QLatin1String( "1" ) );
bool checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( QStringLiteral( "checked" ) ) ) != Qt::Unchecked;
bool isMutuallyExclusive = element.attribute( QStringLiteral( "mutually-exclusive" ), QStringLiteral( "0" ) ) == QLatin1String( "1" );
@@ -316,7 +316,7 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCo
mne = mnl.toElement();

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

//short name
QDomElement shortNameElem = layerElement.firstChildElement( QStringLiteral( "shortname" ) );
@@ -2819,7 +2819,7 @@ void QgsProject::generateTsFile( const QString &locale )
translationContext.writeTsFile( locale );
}

QString QgsProject::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n )
QString QgsProject::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n ) const
{
if ( !mTranslator )
{
@@ -47,6 +47,7 @@
#include "qgsprojectmetadata.h"
#include "qgstranslationcontext.h"
#include "qgsvectorlayer.h"
#include "qgsprojecttranslator.h"

class QFileInfo;
class QDomDocument;
@@ -85,7 +86,7 @@ class QgsAuxiliaryStorage;
*/

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

signals:

@@ -0,0 +1,45 @@
/***************************************************************************
qgsprojecttranslator.h
---------------------
begin : 24.7.2018
copyright : (C) 2018 by david signer
email : david at opengis dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSPROJECTTRANSLATOR_H
#define QGSPROJECTTRANSLATOR_H

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgis.h"
#include <QString>

/**
* \ingroup core
* This abstract class is to call translate() for project data from wherever QgsReadWriteContext is available.
*
* \since QGIS 3.4
*/

class CORE_EXPORT QgsProjectTranslator
{
public:

/**
* This method needs to be reimplemented in all classes which implement this interface
*
* \since QGIS 3.4
*/
virtual QString translate( const QString &context, const QString &sourceText, const char *disambiguation = nullptr, int n = -1 ) const = 0;

virtual ~QgsProjectTranslator() = default;
};

#endif // QGSPROJECTTRANSLATOR_H
@@ -20,6 +20,7 @@

#include "qgspathresolver.h"
#include "qgis.h"
#include "qgsprojecttranslator.h"

class QgsReadWriteContextCategoryPopper;

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

/**
* Returns the project translator
* \since QGIS 3.2
*/
const QgsProjectTranslator *projectTranslator( ) const { return mProjectTranslator; }

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

friend class QgsReadWriteContextCategoryPopper;
};
@@ -1907,13 +1907,13 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
if ( !aliasElem.attribute( QStringLiteral( "name" ) ).isEmpty() )
{
//if it has alias
alias = QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), aliasElem.attribute( QStringLiteral( "name" ) ) );
alias = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), aliasElem.attribute( QStringLiteral( "name" ) ) );
QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ) + " source " + aliasElem.attribute( QStringLiteral( "name" ) ), 1 );
}
else
{
//if it has no alias, it should be the fields translation
alias = QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), field );
alias = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), field );
QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ) + " source " + field, 1 );
//if it gets the exact field value, there has been no translation (or not even translation loaded) - so no alias should be generated;
if ( alias == aliasElem.attribute( QStringLiteral( "field" ) ) )

0 comments on commit fc018da

Please sign in to comment.
You can’t perform that action at this time.