Skip to content

Commit f5a4a42

Browse files
committedAug 7, 2018
generate ts file
with the structure for it - not finished
1 parent 5e1184a commit f5a4a42

File tree

9 files changed

+352
-0
lines changed

9 files changed

+352
-0
lines changed
 

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ SET(QGIS_CORE_SRCS
305305
qgstextrenderer.cpp
306306
qgstolerance.cpp
307307
qgstracer.cpp
308+
qgstranslationcontext.cpp
308309
qgstrackedvectorlayertools.cpp
309310
qgstransaction.cpp
310311
qgstransactiongroup.cpp
@@ -635,6 +636,7 @@ SET(QGIS_CORE_MOC_HDRS
635636
qgstaskmanager.h
636637
qgstolerance.h
637638
qgstracer.h
639+
qgstranslationcontext.h
638640
qgstrackedvectorlayertools.h
639641
qgstransaction.h
640642
qgstransactiongroup.h

‎src/core/qgsapplication.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,11 @@ int QgsApplication::maxConcurrentConnectionsPerPool() const
14681468
return CONN_POOL_MAX_CONCURRENT_CONNS;
14691469
}
14701470

1471+
void QgsApplication::collectTranslatableObjects( QgsTranslationContext *translationContext )
1472+
{
1473+
emit requestForTranslatableObjects( translationContext );
1474+
}
1475+
14711476
QString QgsApplication::nullRepresentation()
14721477
{
14731478
ApplicationMembers *appMembers = members();

‎src/core/qgsapplication.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,13 @@ class CORE_EXPORT QgsApplication : public QApplication
742742
*/
743743
int maxConcurrentConnectionsPerPool() const;
744744

745+
/**
746+
* dave : to write
747+
*
748+
* \since QGIS 3.2
749+
*/
750+
void collectTranslatableObjects( QgsTranslationContext *translationContext );
751+
745752
#ifdef SIP_RUN
746753
SIP_IF_FEATURE( ANDROID )
747754
//dummy method to workaround sip generation issue

‎src/core/qgsproject.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ QgsProject::QgsProject( QObject *parent )
366366
connect( mLayerStore.get(), &QgsMapLayerStore::allLayersRemoved, this, &QgsProject::removeAll );
367367
connect( mLayerStore.get(), &QgsMapLayerStore::layersAdded, this, &QgsProject::layersAdded );
368368
connect( mLayerStore.get(), &QgsMapLayerStore::layerWasAdded, this, &QgsProject::layerWasAdded );
369+
connect( QgsApplication::instance(), &QgsApplication::requestForTranslatableObjects, this, &QgsProject::registerTranslatableObjects );
369370
}
370371

371372

@@ -441,6 +442,14 @@ void QgsProject::setPresetHomePath( const QString &path )
441442
setDirty( true );
442443
}
443444

445+
void QgsProject::registerTranslatableObjects( QgsTranslationContext *translationContext )
446+
{
447+
for ( auto layer : mRootGroup->layerOrder() )
448+
{
449+
translationContext->registerTranslation( QStringLiteral( "project:layers:{layer_id}" ), layer->name() );
450+
}
451+
}
452+
444453
void QgsProject::setFileName( const QString &name )
445454
{
446455
if ( name == mFile.fileName() )
@@ -487,6 +496,17 @@ QDateTime QgsProject::lastModified() const
487496
}
488497
}
489498

499+
QString QgsProject::absolutePath() const
500+
{
501+
if ( projectStorage() )
502+
return QString();
503+
504+
if ( mFile.fileName().isEmpty() )
505+
return QString(); // this is to protect ourselves from getting current directory from QFileInfo::absoluteFilePath()
506+
507+
return QFileInfo( mFile.fileName() ).absolutePath();
508+
}
509+
490510
QString QgsProject::absoluteFilePath() const
491511
{
492512
if ( projectStorage() )
@@ -2698,3 +2718,38 @@ void QgsProject::setRequiredLayers( const QSet<QgsMapLayer *> &layers )
26982718
}
26992719
writeEntry( QStringLiteral( "RequiredLayers" ), QStringLiteral( "Layers" ), layerIds );
27002720
}
2721+
2722+
void QgsProject::generateTsFile()
2723+
{
2724+
QgsTranslationContext translationContext;
2725+
translationContext.setProject( this );
2726+
translationContext.setFileName( QStringLiteral( "%1/%2.ts" ).arg( absolutePath(), baseName() ) );
2727+
2728+
emit QgsApplication::instance()->collectTranslatableObjects( &translationContext );
2729+
2730+
translationContext.writeTsFile();
2731+
}
2732+
2733+
bool QgsProject::translate( const QString &translationCode )
2734+
{
2735+
/*
2736+
QgsTranslationContext translationContext;
2737+
translationContext.setProject( this );
2738+
translationContext.setFileName( filePath() );
2739+
2740+
QgsApplication::instance()->collectTranslatableObjects( &translationContext );
2741+
2742+
QTranslator projectTranslator( nullptr );
2743+
2744+
if ( projectTranslator.load( fileInfo().baseName() + translationCode, fileInfo().path() ) )
2745+
{
2746+
//translationContext.translations.projectTranslator.translate( )
2747+
}
2748+
else
2749+
{
2750+
return false;
2751+
}
2752+
*/
2753+
return true;
2754+
}
2755+

‎src/core/qgsproject.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <QPair>
3232
#include <QFileInfo>
3333
#include <QStringList>
34+
#include <QTranslator>
3435

3536
#include "qgsunittypes.h"
3637
#include "qgssnappingconfig.h"
@@ -44,6 +45,7 @@
4445
#include "qgsarchive.h"
4546
#include "qgsreadwritecontext.h"
4647
#include "qgsprojectmetadata.h"
48+
#include "qgstranslationcontext.h"
4749

4850
class QFileInfo;
4951
class QDomDocument;
@@ -183,6 +185,13 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
183185
*/
184186
QString absoluteFilePath() const;
185187

188+
/**
189+
* Returns full absolute path to the project folder if the project is stored in a file system - derived from fileName().
190+
* Returns empty string when the project is stored in a project storage (there is no concept of paths for custom project storages).
191+
* \since QGIS 3.2
192+
*/
193+
QString absolutePath() const;
194+
186195
/**
187196
* Returns the base name of the project file without the path and without extension - derived from fileName().
188197
* \since QGIS 3.2
@@ -955,6 +964,18 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
955964
*/
956965
void setRequiredLayers( const QSet<QgsMapLayer *> &layers );
957966

967+
/**
968+
* dave : to write
969+
* \since QGIS 3.2
970+
*/
971+
void generateTsFile( );
972+
973+
/**
974+
* Translates the project with QTranslator and qm-file
975+
* \returns true if project file has been translated
976+
*/
977+
bool translate( const QString &translationCode );
978+
958979
signals:
959980

960981
/**
@@ -1269,6 +1290,15 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
12691290
*/
12701291
void setPresetHomePath( const QString &path );
12711292

1293+
/**
1294+
* Registers the translatable objects into the tranlationContext
1295+
* so there can be created a TS file with etc. dave: write
1296+
* \since QGIS 3.2
1297+
*
1298+
* \param translationContext where the objects will be registered
1299+
*/
1300+
void registerTranslatableObjects( QgsTranslationContext *translationContext );
1301+
12721302
private slots:
12731303
void onMapLayersAdded( const QList<QgsMapLayer *> &layers );
12741304
void onMapLayersRemoved( const QList<QgsMapLayer *> &layers );

‎src/core/qgstranslationcontext.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/***************************************************************************
2+
qgstranslationcontext.cpp - %{Cpp:License:ClassName}
3+
4+
---------------------
5+
begin : 23.5.2018
6+
copyright : (C) 2018 by david
7+
email : [your-email-here]
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+
#include "qgstranslationcontext.h"
17+
18+
#include <QDir>
19+
#include <QTextStream>
20+
#include <QDomElement>
21+
#include <QDomDocument>
22+
23+
QgsTranslationContext::QgsTranslationContext()
24+
{}
25+
26+
QgsProject *QgsTranslationContext::project() const
27+
{
28+
return mProject;
29+
}
30+
31+
void QgsTranslationContext::setProject( QgsProject *project )
32+
{
33+
mProject = project;
34+
}
35+
36+
QString QgsTranslationContext::fileName() const
37+
{
38+
return mFileName;
39+
}
40+
41+
void QgsTranslationContext::setFileName( const QString &fileName )
42+
{
43+
mFileName = fileName;
44+
}
45+
46+
void QgsTranslationContext::registerTranslation( const QString &context, const QString &source )
47+
{
48+
mTranslatableObjects.append( qMakePair( context, source ) );
49+
}
50+
51+
void QgsTranslationContext::writeTsFile()
52+
{
53+
//write xml
54+
QDomDocument doc( QStringLiteral( "TS" ) );
55+
56+
for ( QPair < QString, QString > translatableObject : mTranslatableObjects )
57+
{
58+
QDomElement contextElement = doc.createElement( QStringLiteral( "context" ) );
59+
doc.appendChild( contextElement );
60+
61+
QDomElement nameElement = doc.createElement( QStringLiteral( "name" ) );
62+
QDomText nameText = doc.createTextNode( translatableObject.first );
63+
nameElement.appendChild( nameText );
64+
contextElement.appendChild( nameElement );
65+
66+
QDomElement messageElement = doc.createElement( QStringLiteral( "message" ) );
67+
contextElement.appendChild( messageElement );
68+
69+
QDomElement sourceElement = doc.createElement( QStringLiteral( "source" ) );
70+
QDomText sourceText = doc.createTextNode( translatableObject.second );
71+
sourceElement.appendChild( sourceText );
72+
messageElement.appendChild( sourceElement );
73+
74+
QDomElement translationElement = doc.createElement( QStringLiteral( "translation" ) );
75+
QDomText translationText = doc.createTextNode( QStringLiteral( "testtranslation" ) );
76+
translationElement.appendChild( translationText );
77+
messageElement.appendChild( translationElement );
78+
}
79+
80+
//write file
81+
QFile tsFile( fileName() );
82+
tsFile.open( QIODevice::WriteOnly );
83+
QTextStream stream( &tsFile );
84+
stream << doc.toString();
85+
tsFile.close();
86+
}

‎src/core/qgstranslationcontext.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/***************************************************************************
2+
qgstranslationcontext.h - %{Cpp:License:ClassName}
3+
4+
---------------------
5+
begin : 23.5.2018
6+
copyright : (C) 2018 by david
7+
email : [your-email-here]
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 QGSTRANSLATIONCONTEXT_H
17+
#define QGSTRANSLATIONCONTEXT_H
18+
19+
#include "qgis_core.h"
20+
#include "qgis_sip.h"
21+
#include "qgis.h"
22+
23+
class QgsProject;
24+
25+
/**
26+
* \ingroup core
27+
* \class QgsTranslationContext
28+
* \brief dave: write
29+
*
30+
* \since QGIS 3.2
31+
*/
32+
33+
class CORE_EXPORT QgsTranslationContext
34+
{
35+
public:
36+
37+
/**
38+
* Constructor
39+
*/
40+
QgsTranslationContext( );
41+
42+
/**
43+
* Returns the project
44+
* \see setProject()
45+
*/
46+
QgsProject *project() const;
47+
48+
/**
49+
* Sets the \a project where the translation need to be done for
50+
*
51+
* \see project()
52+
*/
53+
void setProject( QgsProject *project );
54+
55+
/**
56+
* Returns the TS fileName
57+
* \see setFileName()
58+
*/
59+
QString fileName() const;
60+
61+
/**
62+
* Sets the \a name of the TS file
63+
*
64+
* \see fileName()
65+
*/
66+
void setFileName( const QString &fileName );
67+
68+
/**
69+
* Registers the \a string to be translated
70+
*
71+
* \param translationString name and path of the object need to be translated
72+
* \param layerName the name of the layer
73+
*/
74+
void registerTranslation( const QString &context, const QString &source );
75+
76+
/**
77+
* Writes the Ts-file
78+
*/
79+
void writeTsFile( );
80+
81+
private:
82+
83+
QgsProject *mProject;
84+
QString mFileName;
85+
QList < QPair< QString, QString > > mTranslatableObjects;
86+
87+
};
88+
89+
#endif // QGSTRANSLATIONCONTEXT_H

‎tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ SET(TESTS
198198
testqgssqliteutils.cpp
199199
testqgsmimedatautils.cpp
200200
testqgsofflineediting.cpp
201+
testqgstranslateproject.cpp
201202
)
202203

203204
IF(WITH_QTWEBKIT)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/***************************************************************************
2+
testqgstranslateproject.cpp
3+
4+
---------------------
5+
begin : 24.5.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+
#include "qgstest.h"
17+
18+
#include <QObject>
19+
#include <QDir>
20+
21+
#include "qgsapplication.h"
22+
#include "qgstranslationcontext.h"
23+
#include "qgsproject.h"
24+
25+
class TestQgsTranslateProject : public QObject
26+
{
27+
Q_OBJECT
28+
29+
public:
30+
31+
private slots:
32+
void initTestCase();
33+
void generateTsFile();
34+
void registerTranslatableObjects( QgsTranslationContext *translationContext );
35+
void cleanupTestCase();
36+
};
37+
38+
void TestQgsTranslateProject::initTestCase()
39+
{
40+
QgsApplication::init();
41+
QgsApplication::initQgis();
42+
43+
connect( QgsApplication::instance(), &QgsApplication::requestForTranslatableObjects, this, &TestQgsTranslateProject::registerTranslatableObjects );
44+
}
45+
46+
void TestQgsTranslateProject::generateTsFile()
47+
{
48+
QgsProject *prj = new QgsProject;
49+
prj->setFileName( "/home/qgis/a-project-file.qgs" ); // not expected to exist
50+
51+
prj->generateTsFile();
52+
53+
QFile tsFile( "/home/qgis/a-project-file.ts." );
54+
55+
//test whether the file has been created
56+
QVERIFY( tsFile.exists() );
57+
58+
//test wheter there is the entry for our honeybee
59+
60+
61+
// QgsProject::instance()->generateTsFile();
62+
63+
// all cases start with all items checked
64+
}
65+
66+
void TestQgsTranslateProject::registerTranslatableObjects( QgsTranslationContext *translationContext )
67+
{
68+
translationContext->registerTranslation( QStringLiteral( "testqgstranslateproject:test01" ), "honeybee" );
69+
}
70+
71+
void TestQgsTranslateProject::cleanupTestCase()
72+
{
73+
//delete ts file
74+
}
75+
76+
QGSTEST_MAIN( TestQgsTranslateProject )
77+
#include "testqgstranslateproject.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.