Skip to content

Commit

Permalink
generate ts file
Browse files Browse the repository at this point in the history
with the structure for it - not finished
  • Loading branch information
signedav committed Aug 7, 2018
1 parent 5e1184a commit f5a4a42
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -305,6 +305,7 @@ SET(QGIS_CORE_SRCS
qgstextrenderer.cpp
qgstolerance.cpp
qgstracer.cpp
qgstranslationcontext.cpp
qgstrackedvectorlayertools.cpp
qgstransaction.cpp
qgstransactiongroup.cpp
Expand Down Expand Up @@ -635,6 +636,7 @@ SET(QGIS_CORE_MOC_HDRS
qgstaskmanager.h
qgstolerance.h
qgstracer.h
qgstranslationcontext.h
qgstrackedvectorlayertools.h
qgstransaction.h
qgstransactiongroup.h
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsapplication.cpp
Expand Up @@ -1468,6 +1468,11 @@ int QgsApplication::maxConcurrentConnectionsPerPool() const
return CONN_POOL_MAX_CONCURRENT_CONNS;
}

void QgsApplication::collectTranslatableObjects( QgsTranslationContext *translationContext )
{
emit requestForTranslatableObjects( translationContext );
}

QString QgsApplication::nullRepresentation()
{
ApplicationMembers *appMembers = members();
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsapplication.h
Expand Up @@ -742,6 +742,13 @@ class CORE_EXPORT QgsApplication : public QApplication
*/
int maxConcurrentConnectionsPerPool() const;

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

#ifdef SIP_RUN
SIP_IF_FEATURE( ANDROID )
//dummy method to workaround sip generation issue
Expand Down
55 changes: 55 additions & 0 deletions src/core/qgsproject.cpp
Expand Up @@ -366,6 +366,7 @@ QgsProject::QgsProject( QObject *parent )
connect( mLayerStore.get(), &QgsMapLayerStore::allLayersRemoved, this, &QgsProject::removeAll );
connect( mLayerStore.get(), &QgsMapLayerStore::layersAdded, this, &QgsProject::layersAdded );
connect( mLayerStore.get(), &QgsMapLayerStore::layerWasAdded, this, &QgsProject::layerWasAdded );
connect( QgsApplication::instance(), &QgsApplication::requestForTranslatableObjects, this, &QgsProject::registerTranslatableObjects );
}


Expand Down Expand Up @@ -441,6 +442,14 @@ void QgsProject::setPresetHomePath( const QString &path )
setDirty( true );
}

void QgsProject::registerTranslatableObjects( QgsTranslationContext *translationContext )
{
for ( auto layer : mRootGroup->layerOrder() )
{
translationContext->registerTranslation( QStringLiteral( "project:layers:{layer_id}" ), layer->name() );
}
}

void QgsProject::setFileName( const QString &name )
{
if ( name == mFile.fileName() )
Expand Down Expand Up @@ -487,6 +496,17 @@ QDateTime QgsProject::lastModified() const
}
}

QString QgsProject::absolutePath() const
{
if ( projectStorage() )
return QString();

if ( mFile.fileName().isEmpty() )
return QString(); // this is to protect ourselves from getting current directory from QFileInfo::absoluteFilePath()

return QFileInfo( mFile.fileName() ).absolutePath();
}

QString QgsProject::absoluteFilePath() const
{
if ( projectStorage() )
Expand Down Expand Up @@ -2698,3 +2718,38 @@ void QgsProject::setRequiredLayers( const QSet<QgsMapLayer *> &layers )
}
writeEntry( QStringLiteral( "RequiredLayers" ), QStringLiteral( "Layers" ), layerIds );
}

void QgsProject::generateTsFile()
{
QgsTranslationContext translationContext;
translationContext.setProject( this );
translationContext.setFileName( QStringLiteral( "%1/%2.ts" ).arg( absolutePath(), baseName() ) );

emit QgsApplication::instance()->collectTranslatableObjects( &translationContext );

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;
}

30 changes: 30 additions & 0 deletions src/core/qgsproject.h
Expand Up @@ -31,6 +31,7 @@
#include <QPair>
#include <QFileInfo>
#include <QStringList>
#include <QTranslator>

#include "qgsunittypes.h"
#include "qgssnappingconfig.h"
Expand All @@ -44,6 +45,7 @@
#include "qgsarchive.h"
#include "qgsreadwritecontext.h"
#include "qgsprojectmetadata.h"
#include "qgstranslationcontext.h"

class QFileInfo;
class QDomDocument;
Expand Down Expand Up @@ -183,6 +185,13 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
QString absoluteFilePath() const;

/**
* Returns full absolute path to the project folder if the project is stored in a file system - derived from fileName().
* Returns empty string when the project is stored in a project storage (there is no concept of paths for custom project storages).
* \since QGIS 3.2
*/
QString absolutePath() const;

/**
* Returns the base name of the project file without the path and without extension - derived from fileName().
* \since QGIS 3.2
Expand Down Expand Up @@ -955,6 +964,18 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
void setRequiredLayers( const QSet<QgsMapLayer *> &layers );

/**
* dave : to write
* \since QGIS 3.2
*/
void generateTsFile( );

/**
* Translates the project with QTranslator and qm-file
* \returns true if project file has been translated
*/
bool translate( const QString &translationCode );

signals:

/**
Expand Down Expand Up @@ -1269,6 +1290,15 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
void setPresetHomePath( const QString &path );

/**
* Registers the translatable objects into the tranlationContext
* so there can be created a TS file with etc. dave: write
* \since QGIS 3.2
*
* \param translationContext where the objects will be registered
*/
void registerTranslatableObjects( QgsTranslationContext *translationContext );

private slots:
void onMapLayersAdded( const QList<QgsMapLayer *> &layers );
void onMapLayersRemoved( const QList<QgsMapLayer *> &layers );
Expand Down
86 changes: 86 additions & 0 deletions src/core/qgstranslationcontext.cpp
@@ -0,0 +1,86 @@
/***************************************************************************
qgstranslationcontext.cpp - %{Cpp:License:ClassName}
---------------------
begin : 23.5.2018
copyright : (C) 2018 by david
email : [your-email-here]
***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "qgstranslationcontext.h"

#include <QDir>
#include <QTextStream>
#include <QDomElement>
#include <QDomDocument>

QgsTranslationContext::QgsTranslationContext()
{}

QgsProject *QgsTranslationContext::project() const
{
return mProject;
}

void QgsTranslationContext::setProject( QgsProject *project )
{
mProject = project;
}

QString QgsTranslationContext::fileName() const
{
return mFileName;
}

void QgsTranslationContext::setFileName( const QString &fileName )
{
mFileName = fileName;
}

void QgsTranslationContext::registerTranslation( const QString &context, const QString &source )
{
mTranslatableObjects.append( qMakePair( context, source ) );
}

void QgsTranslationContext::writeTsFile()
{
//write xml
QDomDocument doc( QStringLiteral( "TS" ) );

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

QDomElement nameElement = doc.createElement( QStringLiteral( "name" ) );
QDomText nameText = doc.createTextNode( translatableObject.first );
nameElement.appendChild( nameText );
contextElement.appendChild( nameElement );

QDomElement messageElement = doc.createElement( QStringLiteral( "message" ) );
contextElement.appendChild( messageElement );

QDomElement sourceElement = doc.createElement( QStringLiteral( "source" ) );
QDomText sourceText = doc.createTextNode( translatableObject.second );
sourceElement.appendChild( sourceText );
messageElement.appendChild( sourceElement );

QDomElement translationElement = doc.createElement( QStringLiteral( "translation" ) );
QDomText translationText = doc.createTextNode( QStringLiteral( "testtranslation" ) );
translationElement.appendChild( translationText );
messageElement.appendChild( translationElement );
}

//write file
QFile tsFile( fileName() );
tsFile.open( QIODevice::WriteOnly );
QTextStream stream( &tsFile );
stream << doc.toString();
tsFile.close();
}
89 changes: 89 additions & 0 deletions src/core/qgstranslationcontext.h
@@ -0,0 +1,89 @@
/***************************************************************************
qgstranslationcontext.h - %{Cpp:License:ClassName}
---------------------
begin : 23.5.2018
copyright : (C) 2018 by david
email : [your-email-here]
***************************************************************************
* *
* 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 QGSTRANSLATIONCONTEXT_H
#define QGSTRANSLATIONCONTEXT_H

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgis.h"

class QgsProject;

/**
* \ingroup core
* \class QgsTranslationContext
* \brief dave: write
*
* \since QGIS 3.2
*/

class CORE_EXPORT QgsTranslationContext
{
public:

/**
* Constructor
*/
QgsTranslationContext( );

/**
* Returns the project
* \see setProject()
*/
QgsProject *project() const;

/**
* Sets the \a project where the translation need to be done for
*
* \see project()
*/
void setProject( QgsProject *project );

/**
* Returns the TS fileName
* \see setFileName()
*/
QString fileName() const;

/**
* Sets the \a name of the TS file
*
* \see fileName()
*/
void setFileName( const QString &fileName );

/**
* Registers the \a string to be translated
*
* \param translationString name and path of the object need to be translated
* \param layerName the name of the layer
*/
void registerTranslation( const QString &context, const QString &source );

/**
* Writes the Ts-file
*/
void writeTsFile( );

private:

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

};

#endif // QGSTRANSLATIONCONTEXT_H
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -198,6 +198,7 @@ SET(TESTS
testqgssqliteutils.cpp
testqgsmimedatautils.cpp
testqgsofflineediting.cpp
testqgstranslateproject.cpp
)

IF(WITH_QTWEBKIT)
Expand Down

0 comments on commit f5a4a42

Please sign in to comment.