Skip to content
Permalink
Browse files

translator basics

basics work - translation of layername, relations
fields and aliases have a problem
  • Loading branch information
signedav committed May 31, 2018
1 parent f5a4a42 commit 778734fd9486c333b12f2ab5a816608ed474cd86
@@ -252,12 +252,23 @@ QgsLayerTreeGroup *QgsLayerTreeGroup::findGroup( const QString &name )
return nullptr;
}

QList<QgsLayerTreeGroup *> QgsLayerTreeGroup::findGroups() const
{
QList<QgsLayerTreeGroup *> list;
Q_FOREACH ( QgsLayerTreeNode *child, mChildren )
{
if ( QgsLayerTree::isGroup( child ) )
list << QgsLayerTree::toGroup( child );
}
return list;
}

QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element, const QgsReadWriteContext &context )
{
if ( element.tagName() != QLatin1String( "layer-tree-group" ) )
return nullptr;

QString name = element.attribute( QStringLiteral( "name" ) );
QString name = QgsProject::instance()->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" );
@@ -141,6 +141,11 @@ class CORE_EXPORT QgsLayerTreeGroup : public QgsLayerTreeNode
*/
QgsLayerTreeGroup *findGroup( const QString &name );

/**
* Find all group layer nodes
*/
QList<QgsLayerTreeGroup *> findGroups() const;

/**
* Read group (tree) from XML element <layer-tree-group> and return the newly created group (or null on error).
* Does not resolve textual references to layers. Call resolveReferences() afterwards to do it.
@@ -22,6 +22,7 @@

#include "qgis.h"
#include "qgsconfig.h"
#include "qgstranslationcontext.h"

class Qgs3DRendererRegistry;
class QgsActionScopeRegistry;
@@ -743,7 +744,7 @@ class CORE_EXPORT QgsApplication : public QApplication
int maxConcurrentConnectionsPerPool() const;

/**
* dave : to write
* Emits the signal to collect all the strings of .qgs to be included in ts file
*
* \since QGIS 3.2
*/
@@ -772,6 +773,13 @@ class CORE_EXPORT QgsApplication : public QApplication
*/
void nullRepresentationChanged();

/**
* dave : to write
*
* \since QGIS 3.2
*/
void requestForTranslatableObjects( QgsTranslationContext *translationContext );

private:

static void copyPath( const QString &src, const QString &dst );
@@ -524,7 +524,7 @@ QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomEleme

if ( elem.tagName() == QLatin1String( "attributeEditorContainer" ) )
{
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( elem.attribute( QStringLiteral( "name" ) ), parent );
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:formcontainers" ).arg( "testdave" ), elem.attribute( QStringLiteral( "name" ) ) ), parent );
bool ok;
int cc = elem.attribute( QStringLiteral( "columnCount" ) ).toInt( &ok );
if ( !ok )
@@ -314,7 +314,8 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCo
// set name
mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
mne = mnl.toElement();
setName( mne.text() );
//trans dave
setName( QgsProject::instance()->translate( QStringLiteral( "project:layers:%1" ).arg( layerElement.namedItem( QStringLiteral( "id" ) ).toElement().text() ), mne.text() ) );

//short name
QDomElement shortNameElem = layerElement.firstChildElement( QStringLiteral( "shortname" ) );
@@ -444,9 +444,38 @@ void QgsProject::setPresetHomePath( const QString &path )

void QgsProject::registerTranslatableObjects( QgsTranslationContext *translationContext )
{
for ( auto layer : mRootGroup->layerOrder() )
//register layers
for ( QgsLayerTreeLayer *layer : mRootGroup->findLayers() )
{
translationContext->registerTranslation( QStringLiteral( "project:layers:{layer_id}" ), layer->name() );
translationContext->registerTranslation( QStringLiteral( "project:layers:%1" ).arg( layer->layerId() ), layer->name() );

QgsMapLayer *mapLayer = layer->layer();
if ( mapLayer && mapLayer->type() == QgsMapLayer::VectorLayer )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mapLayer );

const QgsFields fields = vlayer->fields();
for ( const QgsField &field : fields )
{
if ( field.alias().isEmpty() )
translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( vlayer->id() ), field.name() );
else
translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( vlayer->id() ), field.alias() );
}
}
}

//register layergroups
for ( const QgsLayerTreeGroup *groupLayer : mRootGroup->findGroups() )
{
translationContext->registerTranslation( QStringLiteral( "project:layergroups" ), groupLayer->name() );
}

//register relations
const QList<QgsRelation> &relations = mRelationManager->relations().values();
for ( const QgsRelation &relation : relations )
{
translationContext->registerTranslation( QStringLiteral( "project:relations" ), relation.name() );
}
}

@@ -797,7 +826,7 @@ bool QgsProject::_getMapLayers( const QDomDocument &doc, QList<QDomNode> &broken
{
QDomElement element = node.toElement();

QString name = node.namedItem( QStringLiteral( "layername" ) ).toElement().text();
QString name = translate( QStringLiteral( "project:layers:%1" ).arg( node.namedItem( QStringLiteral( "id" ) ).toElement().text() ), node.namedItem( QStringLiteral( "layername" ) ).toElement().text() );
if ( !name.isNull() )
emit loadingLayer( tr( "Loading layer %1" ).arg( name ) );

@@ -936,11 +965,30 @@ bool QgsProject::read()
return rc;
}

//dave put to another place
QString QgsProject::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n )
{
if ( mTranslator.isEmpty() )
{
return sourceText;
}

return mTranslator.translate( context.toUtf8(), sourceText.toUtf8(), disambiguation, n );
}


bool QgsProject::readProjectFile( const QString &filename )
{
QFile projectFile( filename );
clearError();

QgsSettings settings;

if ( mTranslator.load( QStringLiteral( "%1_%2" ).arg( QFileInfo( projectFile.fileName() ).baseName(), settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString() ), QFileInfo( projectFile.fileName() ).absolutePath() ) )
{
QgsDebugMsg( "Translation loaded" );
}

std::unique_ptr<QDomDocument> doc( new QDomDocument( QStringLiteral( "qgis" ) ) );

if ( !projectFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
@@ -978,7 +1026,6 @@ bool QgsProject::readProjectFile( const QString &filename )

projectFile.close();


QgsDebugMsg( "Opened document " + projectFile.fileName() );

// get project version string, if any
@@ -1208,7 +1255,6 @@ bool QgsProject::readProjectFile( const QString &filename )
}

mSnappingConfig.readProject( *doc );

//add variables defined in project file
QStringList variableNames = readListEntry( QStringLiteral( "Variables" ), QStringLiteral( "/variableNames" ) );
QStringList variableValues = readListEntry( QStringLiteral( "Variables" ), QStringLiteral( "/variableValues" ) );
@@ -2729,27 +2775,3 @@ void QgsProject::generateTsFile()

translationContext.writeTsFile();
}

bool QgsProject::translate( const QString &translationCode )
{
/*
QgsTranslationContext translationContext;
translationContext.setProject( this );
translationContext.setFileName( filePath() );
QgsApplication::instance()->collectTranslatableObjects( &translationContext );
QTranslator projectTranslator( nullptr );
if ( projectTranslator.load( fileInfo().baseName() + translationCode, fileInfo().path() ) )
{
//translationContext.translations.projectTranslator.translate( )
}
else
{
return false;
}
*/
return true;
}

@@ -97,6 +97,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
Q_PROPERTY( QgsRelationManager *relationManager READ relationManager )
Q_PROPERTY( QList<QgsVectorLayer *> avoidIntersectionsLayers READ avoidIntersectionsLayers WRITE setAvoidIntersectionsLayers NOTIFY avoidIntersectionsLayersChanged )
Q_PROPERTY( QgsProjectMetadata metadata READ metadata WRITE setMetadata NOTIFY metadataChanged )
//dave to do Q_PROPERTY( QTranslator translator READ translator )

public:
//! Returns the QgsProject singleton instance
@@ -968,13 +969,22 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
* dave : to write
* \since QGIS 3.2
*/
//QTranslator &translator() { return mTranslator; }

/**
* Triggers the collection strings of .qgs to be included in ts file and calls writeTsFile()
* \since QGIS 3.2
*/
void generateTsFile( );

/**
* Translates the project with QTranslator and qm-file
* \returns true if project file has been translated
* \returns the result string (in case there is no QTranslator loaded the sourceText)
*/
bool translate( const QString &translationCode );
QString translate( const QString &context, const QString &sourceText, const char *disambiguation = nullptr, int n = -1 );

//dave a function not used because of compiling
bool translate( const QString &context ) const {return context.isEmpty(); }

signals:

@@ -1422,6 +1432,8 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

QgsProjectMetadata mMetadata;

QTranslator mTranslator;

friend class QgsProjectDirtyBlocker;

// Required by QGIS Server for switching the current project instance
@@ -35,7 +35,7 @@ QgsRelation QgsRelation::createFromXml( const QDomNode &node )
QString referencingLayerId = elem.attribute( QStringLiteral( "referencingLayer" ) );
QString referencedLayerId = elem.attribute( QStringLiteral( "referencedLayer" ) );
QString id = elem.attribute( QStringLiteral( "id" ) );
QString name = elem.attribute( QStringLiteral( "name" ) );
QString name = QgsProject::instance()->translate( QStringLiteral( "project:relations" ), elem.attribute( QStringLiteral( "name" ) ) );
QString strength = elem.attribute( QStringLiteral( "strength" ) );

const QMap<QString, QgsMapLayer *> &mapLayers = QgsProject::instance()->mapLayers();
@@ -53,10 +53,13 @@ void QgsTranslationContext::writeTsFile()
//write xml
QDomDocument doc( QStringLiteral( "TS" ) );

QDomElement tsElement = doc.createElement( QStringLiteral( "TS" ) );
doc.appendChild( tsElement );

for ( QPair < QString, QString > translatableObject : mTranslatableObjects )
{
QDomElement contextElement = doc.createElement( QStringLiteral( "context" ) );
doc.appendChild( contextElement );
tsElement.appendChild( contextElement );

QDomElement nameElement = doc.createElement( QStringLiteral( "name" ) );
QDomText nameText = doc.createTextNode( translatableObject.first );
@@ -72,7 +75,7 @@ void QgsTranslationContext::writeTsFile()
messageElement.appendChild( sourceElement );

QDomElement translationElement = doc.createElement( QStringLiteral( "translation" ) );
QDomText translationText = doc.createTextNode( QStringLiteral( "testtranslation" ) );
QDomText translationText = doc.createTextNode( translatableObject.second );
translationElement.appendChild( translationText );
messageElement.appendChild( translationElement );
}
@@ -1,10 +1,10 @@
/***************************************************************************
qgstranslationcontext.h - %{Cpp:License:ClassName}
qgstranslationcontext.h
---------------------
begin : 23.5.2018
copyright : (C) 2018 by david
email : [your-email-here]
copyright : (C) 2018 by David Signer
email : david at opengis dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
@@ -25,7 +25,7 @@ class QgsProject;
/**
* \ingroup core
* \class QgsTranslationContext
* \brief dave: write
* \brief used for the collecting of strings of .qgs to be translated and writing of ts file
*
* \since QGIS 3.2
*/
@@ -80,7 +80,7 @@ class CORE_EXPORT QgsTranslationContext

private:

QgsProject *mProject;
QgsProject *mProject = nullptr;
QString mFileName;
QList < QPair< QString, QString > > mTranslatableObjects;

@@ -1902,7 +1902,26 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
field = fields().at( index ).name();
}

mAttributeAliasMap.insert( field, aliasElem.attribute( QStringLiteral( "name" ) ) );
QString alias;

if ( !aliasElem.attribute( QStringLiteral( "name" ) ).isEmpty() )
{
//if it has alias
alias = QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( this->name() ), aliasElem.attribute( QStringLiteral( "name" ) ) );
QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( this->name() ) + " source " + aliasElem.attribute( QStringLiteral( "name" ) ), 1 );
}
else
{
//if it has no alias, but alias is should be the fields translation
alias = QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( this->name() ), aliasElem.attribute( QStringLiteral( "name" ) ) );
QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( this->name() ) + " source " + field, 1 );
//if it gets the exact field value, there has been no translation (no translation loaded);
if ( alias == aliasElem.attribute( QStringLiteral( "field" ) ) )
alias.clear();
}

QgsDebugMsgLevel( "field " + field + " origalias " + aliasElem.attribute( QStringLiteral( "name" ) ) + " trans " + alias, 1 );
mAttributeAliasMap.insert( field, alias );
}
}

@@ -1916,6 +1935,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
{
QDomElement defaultElem = defaultNodeList.at( i ).toElement();

//dave muss hier übersetzt werden?
QString field = defaultElem.attribute( QStringLiteral( "field" ), QString() );
QString expression = defaultElem.attribute( QStringLiteral( "expression" ), QString() );
bool applyOnUpdate = defaultElem.attribute( QStringLiteral( "applyOnUpdate" ), QStringLiteral( "0" ) ) == QLatin1String( "1" );
@@ -1937,6 +1957,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
{
QDomElement constraintElem = constraintNodeList.at( i ).toElement();

//dave muss hier übersetzt werden?
QString field = constraintElem.attribute( QStringLiteral( "field" ), QString() );
int constraints = constraintElem.attribute( QStringLiteral( "constraints" ), QStringLiteral( "0" ) ).toInt();
if ( field.isEmpty() || constraints == 0 )
@@ -1962,6 +1983,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
{
QDomElement constraintElem = constraintNodeList.at( i ).toElement();

//dave muss hier übersetzt werden?
QString field = constraintElem.attribute( QStringLiteral( "field" ), QString() );
QString exp = constraintElem.attribute( QStringLiteral( "exp" ), QString() );
QString desc = constraintElem.attribute( QStringLiteral( "desc" ), QString() );
@@ -57,7 +57,6 @@ void TestQgsTranslateProject::generateTsFile()

//test wheter there is the entry for our honeybee


// QgsProject::instance()->generateTsFile();

// all cases start with all items checked

0 comments on commit 778734f

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