Skip to content

Commit 2bfe229

Browse files
committedAug 7, 2018
tests, project store and other
not yet working all translations project handling can be improved project store after translation working and fix some errors tests not ready yet
1 parent 778734f commit 2bfe229

19 files changed

+1110
-68
lines changed
 

‎python/core/auto_generated/qgseditformconfig.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ Write XML information
264264
Serialize on project save
265265
%End
266266

267-
QgsAttributeEditorElement *attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent );
267+
QgsAttributeEditorElement *attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent, const QString &layerId = 0 );
268268
%Docstring
269269
Deserialize drag and drop designer elements.
270270
%End

‎python/core/auto_generated/qgsproject.sip.in

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ Returns last modified time of the project file as returned by the file system (o
135135
Returns full absolute path to the project file if the project is stored in a file system - derived from fileName().
136136
Returns empty string when the project is stored in a project storage (there is no concept of paths for custom project storages).
137137

138+
.. versionadded:: 3.2
139+
%End
140+
141+
QString absolutePath() const;
142+
%Docstring
143+
Returns full absolute path to the project folder if the project is stored in a file system - derived from fileName().
144+
Returns empty string when the project is stored in a project storage (there is no concept of paths for custom project storages).
145+
138146
.. versionadded:: 3.2
139147
%End
140148

@@ -983,6 +991,25 @@ and it is mainly a hint for the user interface to protect users from removing la
983991
in the project. The removeMapLayer(), removeMapLayers() calls do not block removal of layers listed here.
984992

985993
.. versionadded:: 3.2
994+
%End
995+
996+
void generateTsFile( const QString &locale );
997+
%Docstring
998+
Triggers the collection strings of .qgs to be included in ts file and calls writeTsFile()
999+
1000+
.. versionadded:: 3.2
1001+
%End
1002+
1003+
QString translate( const QString &context, const QString &sourceText, const char *disambiguation = 0, int n = -1 );
1004+
%Docstring
1005+
Translates the project with QTranslator and qm file
1006+
1007+
:return: the result string (in case there is no QTranslator loaded the sourceText)
1008+
1009+
:param context: describing layer etc.
1010+
:param sourceText: is the identifier of this text
1011+
:param disambiguation: it's the disambiguation
1012+
:param n: if -1 uses the appropriate form
9861013
%End
9871014

9881015
signals:
@@ -1335,6 +1362,16 @@ home path will be automatically determined from the project's file path.
13351362
.. seealso:: :py:func:`homePathChanged`
13361363

13371364
.. versionadded:: 3.2
1365+
%End
1366+
1367+
void registerTranslatableObjects( QgsTranslationContext *translationContext );
1368+
%Docstring
1369+
Registers the translatable objects into the tranlationContext
1370+
so there can be created a ts file these values
1371+
1372+
.. versionadded:: 3.2
1373+
1374+
:param translationContext: where the objects will be registered
13381375
%End
13391376

13401377
};
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgstranslationcontext.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsTranslationContext
13+
{
14+
%Docstring
15+
used for the collecting of strings of .qgs to be translated and writing of ts file
16+
17+
.. versionadded:: 3.2
18+
%End
19+
20+
%TypeHeaderCode
21+
#include "qgstranslationcontext.h"
22+
%End
23+
public:
24+
25+
QgsTranslationContext( );
26+
%Docstring
27+
Constructor
28+
%End
29+
30+
QgsProject *project() const;
31+
%Docstring
32+
Returns the project
33+
34+
.. seealso:: :py:func:`setProject`
35+
%End
36+
37+
void setProject( QgsProject *project );
38+
%Docstring
39+
Sets the ``project`` where the translation need to be done for
40+
41+
.. seealso:: :py:func:`project`
42+
%End
43+
44+
QString fileName() const;
45+
%Docstring
46+
Returns the TS fileName
47+
48+
.. seealso:: :py:func:`setFileName`
49+
%End
50+
51+
void setFileName( const QString &fileName );
52+
%Docstring
53+
Sets the ``name`` of the TS file
54+
55+
.. seealso:: :py:func:`fileName`
56+
%End
57+
58+
void registerTranslation( const QString &context, const QString &source );
59+
%Docstring
60+
Registers the ``string`` to be translated
61+
62+
:param translationString: name and path of the object need to be translated
63+
:param layerName: the name of the layer
64+
%End
65+
66+
void writeTsFile( );
67+
%Docstring
68+
Writes the Ts-file
69+
%End
70+
71+
};
72+
73+
/************************************************************************
74+
* This file has been generated automatically from *
75+
* *
76+
* src/core/qgstranslationcontext.h *
77+
* *
78+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
79+
************************************************************************/

‎python/core/core_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@
352352
%Include auto_generated/qgssnappingconfig.sip
353353
%Include auto_generated/qgstaskmanager.sip
354354
%Include auto_generated/qgstolerance.sip
355+
%Include auto_generated/qgstranslationcontext.sip
355356
%Include auto_generated/qgstrackedvectorlayertools.sip
356357
%Include auto_generated/qgstransaction.sip
357358
%Include auto_generated/qgstransactiongroup.sip

‎src/app/qgisapp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5950,10 +5950,10 @@ void QgisApp::fileSaveAs()
59505950
return;
59515951

59525952
QFileInfo fullPath( path );
5953-
59545953
settings.setValue( QStringLiteral( "UI/lastProjectDir" ), fullPath.path() );
59555954

59565955
if ( filter == zipExt )
5956+
59575957
{
59585958
if ( fullPath.suffix().compare( QLatin1String( "qgz" ), Qt::CaseInsensitive ) != 0 )
59595959
fullPath.setFile( fullPath.filePath() + ".qgz" );

‎src/app/qgsprojectproperties.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa
830830
connect( mMetadataWidget, &QgsMetadataWidget::titleChanged, titleEdit, &QLineEdit::setText );
831831
connect( titleEdit, &QLineEdit::textChanged, mMetadataWidget, &QgsMetadataWidget::setTitle );
832832

833+
connect( generateTsFileButton, &QPushButton::clicked, this, &QgsProjectProperties::generateTsFileButton_clicked );
833834
projectionSelectorInitialized();
834835
populateRequiredLayers();
835836
restoreOptionsBaseUi();
@@ -2190,3 +2191,8 @@ void QgsProjectProperties::setCurrentPage( const QString &pageWidgetName )
21902191
}
21912192
}
21922193
}
2194+
2195+
void QgsProjectProperties::generateTsFileButton_clicked()
2196+
{
2197+
QgsProject::instance()->generateTsFile( "de" );
2198+
}

‎src/app/qgsprojectproperties.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui:
9292
//! A scale in the list of project scales changed
9393
void scaleItemChanged( QListWidgetItem *changedScaleItem );
9494

95+
//dave to document
96+
void generateTsFileButton_clicked();
97+
9598
/**
9699
* Set WMS default extent to current canvas extent
97100
*/

‎src/core/qgseditformconfig.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ void QgsEditFormConfig::readXml( const QDomNode &node, QgsReadWriteContext &cont
399399
{
400400
QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();
401401

402-
QgsAttributeEditorElement *attributeEditorWidget = attributeEditorElementFromDomElement( elem, nullptr );
402+
QgsAttributeEditorElement *attributeEditorWidget = attributeEditorElementFromDomElement( elem, nullptr, node.namedItem( QStringLiteral( "id" ) ).toElement().text() );
403403
addTab( attributeEditorWidget );
404404
}
405405

@@ -518,13 +518,13 @@ void QgsEditFormConfig::writeXml( QDomNode &node, const QgsReadWriteContext &con
518518
//// END TODO
519519
}
520520

521-
QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent )
521+
QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent, const QString &layerId )
522522
{
523523
QgsAttributeEditorElement *newElement = nullptr;
524524

525525
if ( elem.tagName() == QLatin1String( "attributeEditorContainer" ) )
526526
{
527-
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:formcontainers" ).arg( "testdave" ), elem.attribute( QStringLiteral( "name" ) ) ), parent );
527+
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:formcontainers" ).arg( layerId ), elem.attribute( QStringLiteral( "name" ) ) ), parent );
528528
bool ok;
529529
int cc = elem.attribute( QStringLiteral( "columnCount" ) ).toInt( &ok );
530530
if ( !ok )
@@ -551,7 +551,7 @@ QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomEleme
551551
for ( int i = 0; i < childNodeList.size(); i++ )
552552
{
553553
QDomElement childElem = childNodeList.at( i ).toElement();
554-
QgsAttributeEditorElement *myElem = attributeEditorElementFromDomElement( childElem, container );
554+
QgsAttributeEditorElement *myElem = attributeEditorElementFromDomElement( childElem, container, layerId );
555555
if ( myElem )
556556
container->addChildElement( myElem );
557557
}

‎src/core/qgseditformconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class CORE_EXPORT QgsEditFormConfig
288288
/**
289289
* Deserialize drag and drop designer elements.
290290
*/
291-
QgsAttributeEditorElement *attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent );
291+
QgsAttributeEditorElement *attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent, const QString &layerId = nullptr );
292292

293293
/**
294294
* Create a new edit form config. Normally invoked by QgsVectorLayer

‎src/core/qgsmaplayer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCo
314314
// set name
315315
mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
316316
mne = mnl.toElement();
317-
//trans dave
317+
318+
//name can be translated
318319
setName( QgsProject::instance()->translate( QStringLiteral( "project:layers:%1" ).arg( layerElement.namedItem( QStringLiteral( "id" ) ).toElement().text() ), mne.text() ) );
319320

320321
//short name

‎src/core/qgsproject.cpp

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ QgsProject::QgsProject( QObject *parent )
367367
connect( mLayerStore.get(), &QgsMapLayerStore::layersAdded, this, &QgsProject::layersAdded );
368368
connect( mLayerStore.get(), &QgsMapLayerStore::layerWasAdded, this, &QgsProject::layerWasAdded );
369369
connect( QgsApplication::instance(), &QgsApplication::requestForTranslatableObjects, this, &QgsProject::registerTranslatableObjects );
370+
371+
mTranslator = new QTranslator();
372+
370373
}
371374

372375

@@ -928,6 +931,10 @@ bool QgsProject::read()
928931
QString filename = mFile.fileName();
929932
bool rc;
930933

934+
//dave dirty hack
935+
delete mTranslator;
936+
mTranslator = new QTranslator();
937+
931938
if ( QgsProjectStorage *storage = projectStorage() )
932939
{
933940
QTemporaryFile inDevice;
@@ -961,32 +968,30 @@ bool QgsProject::read()
961968
rc = readProjectFile( mFile.fileName() );
962969
}
963970

964-
mFile.setFileName( filename );
965-
return rc;
966-
}
967-
968-
//dave put to another place
969-
QString QgsProject::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n )
970-
{
971-
if ( mTranslator.isEmpty() )
971+
//on translation we should not change the filename back
972+
if ( mTranslator->isEmpty() )
972973
{
973-
return sourceText;
974+
mFile.setFileName( filename );
974975
}
975976

976-
return mTranslator.translate( context.toUtf8(), sourceText.toUtf8(), disambiguation, n );
977+
return rc;
977978
}
978979

979-
980980
bool QgsProject::readProjectFile( const QString &filename )
981981
{
982982
QFile projectFile( filename );
983983
clearError();
984984

985985
QgsSettings settings;
986986

987-
if ( mTranslator.load( QStringLiteral( "%1_%2" ).arg( QFileInfo( projectFile.fileName() ).baseName(), settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString() ), QFileInfo( projectFile.fileName() ).absolutePath() ) )
987+
QString localeFileName = QStringLiteral( "%1_%2" ).arg( QFileInfo( projectFile.fileName() ).baseName(), settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString() );
988+
989+
if ( QFile( QStringLiteral( "%1/%2.qm" ).arg( QFileInfo( projectFile.fileName() ).absolutePath(), localeFileName ) ).exists() )
988990
{
989-
QgsDebugMsg( "Translation loaded" );
991+
if ( mTranslator->load( localeFileName, QFileInfo( projectFile.fileName() ).absolutePath() ) )
992+
{
993+
QgsDebugMsg( "Translation loaded" );
994+
}
990995
}
991996

992997
std::unique_ptr<QDomDocument> doc( new QDomDocument( QStringLiteral( "qgis" ) ) );
@@ -1285,6 +1290,23 @@ bool QgsProject::readProjectFile( const QString &filename )
12851290

12861291
emit nonIdentifiableLayersChanged( nonIdentifiableLayers() );
12871292

1293+
if ( !mTranslator->isEmpty() )
1294+
{
1295+
//project possibly translated -> rename it with locale postfix
1296+
QString newFileName( QStringLiteral( "%1/%2.qgs" ).arg( QFileInfo( projectFile.fileName() ).absolutePath(), localeFileName ) );
1297+
QgsProject::instance()->setFileName( newFileName );
1298+
1299+
if ( QgsProject::instance()->write() )
1300+
{
1301+
QgsProject::instance()->setTitle( localeFileName );
1302+
1303+
QgsDebugMsg( "Translated project saved with locale prefix " + newFileName );
1304+
}
1305+
else
1306+
{
1307+
QgsDebugMsg( "Error saving translated project with locale prefix " + newFileName );
1308+
}
1309+
}
12881310
return true;
12891311
}
12901312

@@ -2765,13 +2787,23 @@ void QgsProject::setRequiredLayers( const QSet<QgsMapLayer *> &layers )
27652787
writeEntry( QStringLiteral( "RequiredLayers" ), QStringLiteral( "Layers" ), layerIds );
27662788
}
27672789

2768-
void QgsProject::generateTsFile()
2790+
void QgsProject::generateTsFile( const QString &locale )
27692791
{
27702792
QgsTranslationContext translationContext;
27712793
translationContext.setProject( this );
2772-
translationContext.setFileName( QStringLiteral( "%1/%2.ts" ).arg( absolutePath(), baseName() ) );
2794+
translationContext.setFileName( QStringLiteral( "%1/%2_%3.ts" ).arg( absolutePath(), baseName(), locale ) );
27732795

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

27762798
translationContext.writeTsFile();
27772799
}
2800+
2801+
QString QgsProject::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n )
2802+
{
2803+
if ( mTranslator->isEmpty() )
2804+
{
2805+
return sourceText;
2806+
}
2807+
2808+
return mTranslator->translate( context.toUtf8(), sourceText.toUtf8(), disambiguation, n );
2809+
}

0 commit comments

Comments
 (0)
Please sign in to comment.