Skip to content

Commit 70dcc33

Browse files
authored
Merge pull request #7456 from signedav/translation_projects
Translation of .qgs project data
2 parents 6072350 + fea9de9 commit 70dcc33

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1954
-64
lines changed

python/core/auto_generated/layertree/qgslayertreegroup.sip.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ Find layer IDs used in all layer nodes. Searches recursively the whole sub-tree.
127127
QgsLayerTreeGroup *findGroup( const QString &name );
128128
%Docstring
129129
Find group node with specified name. Searches recursively the whole sub-tree.
130+
%End
131+
132+
QList<QgsLayerTreeGroup *> findGroups() const;
133+
%Docstring
134+
Find all group layer nodes
130135
%End
131136

132137
static QgsLayerTreeGroup *readXml( QDomElement &element, const QgsReadWriteContext &context ) /Factory/;

python/core/auto_generated/qgsapplication.sip.in

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,6 @@ Do not include generated variables (like system name, user name etc.)
791791
.. versionadded:: 3.0
792792
%End
793793

794-
795794
static void setCustomVariable( const QString &name, const QVariant &value );
796795
%Docstring
797796
Set a single custom expression variable.
@@ -808,6 +807,13 @@ The maximum number of concurrent connections per connections pool.
808807
QGIS may in some situations allocate more than this amount
809808
of connections to avoid deadlocks.
810809

810+
.. versionadded:: 3.4
811+
%End
812+
813+
void collectTranslatableObjects( QgsTranslationContext *translationContext );
814+
%Docstring
815+
Emits the signal to collect all the strings of .qgs to be included in ts file
816+
811817
.. versionadded:: 3.4
812818
%End
813819

@@ -829,6 +835,14 @@ Emitted whenever a custom global variable changes.
829835
void nullRepresentationChanged();
830836
%Docstring
831837
\copydoc nullRepresentation()
838+
%End
839+
840+
void requestForTranslatableObjects( QgsTranslationContext *translationContext );
841+
%Docstring
842+
Emitted when project strings which require translation are being collected for inclusion in a .ts file.
843+
In order to register translatable strings, connect to this signal and register the strings within the specified ``translationContext``.
844+
845+
.. versionadded:: 3.4
832846
%End
833847

834848
};

python/core/auto_generated/qgseditformconfig.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ Sets type of feature form pop-up suppression after feature creation (overrides a
252252
%End
253253

254254

255-
void readXml( const QDomNode &node, QgsReadWriteContext &context );
255+
void readXml( const QDomNode &node, QgsReadWriteContext &context );
256256
%Docstring
257257
Read XML information
258258
Deserialize on project load
@@ -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 = QString(), const QgsReadWriteContext &context = QgsReadWriteContext() );
268268
%Docstring
269269
Deserialize drag and drop designer elements.
270270
%End

python/core/auto_generated/qgsproject.sip.in

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515

16-
class QgsProject : QObject, QgsExpressionContextGenerator
16+
class QgsProject : QObject, QgsExpressionContextGenerator, QgsProjectTranslator
1717
{
1818
%Docstring
1919
Reads and writes project states.
@@ -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,28 @@ 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.4
1001+
%End
1002+
1003+
virtual QString translate( const QString &context, const QString &sourceText, const char *disambiguation = 0, int n = -1 ) const;
1004+
1005+
%Docstring
1006+
Translates the project with QTranslator and qm file
1007+
1008+
:return: the result string (in case there is no QTranslator loaded the sourceText)
1009+
1010+
:param context: describing layer etc.
1011+
:param sourceText: is the identifier of this text
1012+
:param disambiguation: it's the disambiguation
1013+
:param n: if -1 uses the appropriate form
1014+
1015+
.. versionadded:: 3.4
9861016
%End
9871017

9881018
signals:
@@ -1000,6 +1030,11 @@ just before a new project is read).
10001030
void readProject( const QDomDocument & );
10011031
%Docstring
10021032
Emitted when a project is being read.
1033+
%End
1034+
1035+
void readProjectWithContext( const QDomDocument &, QgsReadWriteContext &context );
1036+
%Docstring
1037+
Emitted when a project is being read. And passing the /a context
10031038
%End
10041039

10051040
void writeProject( QDomDocument & );
@@ -1335,6 +1370,26 @@ home path will be automatically determined from the project's file path.
13351370
.. seealso:: :py:func:`homePathChanged`
13361371

13371372
.. versionadded:: 3.2
1373+
%End
1374+
1375+
void registerTranslatableContainers( QgsTranslationContext *translationContext, QgsAttributeEditorContainer *parent, const QString &layerId );
1376+
%Docstring
1377+
Registers the containers that require translation into the translationContext.
1378+
This is a recursive function to get all the child containers.
1379+
1380+
:param translationContext: where the objects will be registered
1381+
:param parent: parent-container containing list of children
1382+
:param layerId: to store under the correct context
1383+
1384+
.. versionadded:: 3.4
1385+
%End
1386+
1387+
void registerTranslatableObjects( QgsTranslationContext *translationContext );
1388+
%Docstring
1389+
Registers the objects that require translation into the ``translationContext``.
1390+
So there can be created a ts file with these values.
1391+
1392+
.. versionadded:: 3.4
13381393
%End
13391394

13401395
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsprojecttranslator.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
class QgsProjectTranslator
12+
{
13+
%Docstring
14+
Wherever an object of this class is available, the derived translate function can be called from.
15+
16+
.. versionadded:: 3.4
17+
%End
18+
19+
%TypeHeaderCode
20+
#include "qgsprojecttranslator.h"
21+
%End
22+
public:
23+
24+
25+
virtual QString translate( const QString &context, const QString &sourceText, const char *disambiguation = 0, int n = -1 ) const = 0;
26+
%Docstring
27+
The derived translate() translates with QTranslator and qm file the sourceText.
28+
It
29+
:return: the result string and in case there is no QTranslator loaded, the sourceText.
30+
This function can be called from wherever the QgsReadWriteContext is available.
31+
32+
:param context: describing layer etc.
33+
:param sourceText: is the identifier of this text
34+
:param disambiguation: it's the disambiguation
35+
:param n: if -1 uses the appropriate form
36+
37+
.. versionadded:: 3.4
38+
%End
39+
40+
virtual ~QgsProjectTranslator();
41+
};
42+
43+
/************************************************************************
44+
* This file has been generated automatically from *
45+
* *
46+
* src/core/qgsprojecttranslator.h *
47+
* *
48+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
49+
************************************************************************/

python/core/auto_generated/qgsreadwritecontext.sip.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,22 @@ Returns the stored messages and remove them
9898
.. versionadded:: 3.2
9999
%End
100100

101+
const QgsProjectTranslator *projectTranslator( ) const;
102+
%Docstring
103+
Returns the project translator
104+
105+
.. versionadded:: 3.4
106+
%End
107+
108+
void setProjectTranslator( QgsProjectTranslator *projectTranslator );
109+
%Docstring
110+
Sets the project translator. Means it shouldn't conform mDefaultTranslator anymore.
111+
It's usually the QgsProject where the function with the context is made and won't be changed anymore.
112+
113+
.. versionadded:: 3.4
114+
%End
101115

116+
public:
102117
};
103118

104119

python/core/auto_generated/qgsrelation.sip.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ class QgsRelation
3535
Default constructor. Creates an invalid relation.
3636
%End
3737

38-
static QgsRelation createFromXml( const QDomNode &node );
38+
static QgsRelation createFromXml( const QDomNode &node, QgsReadWriteContext &context );
3939
%Docstring
4040
Creates a relation from an XML structure. Used for reading .qgs projects.
4141

4242
:param node: The dom node containing the relation information
43+
:param context: to pass project translator
4344

4445
:return: A relation
4546
%End
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 from projects for translation and creation of ts files.
16+
17+
.. versionadded:: 3.4
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`` being translated.
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 ``fileName`` 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 ``source`` to be translated. It's the text of the object needed to be translated.
61+
The ``context`` defines in what context the object is used. Means layer name and sub category of object needed to be translated.
62+
%End
63+
64+
void writeTsFile( const QString &locale );
65+
%Docstring
66+
Writes the Ts-file
67+
%End
68+
69+
};
70+
71+
/************************************************************************
72+
* This file has been generated automatically from *
73+
* *
74+
* src/core/qgstranslationcontext.h *
75+
* *
76+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
77+
************************************************************************/

python/core/core_auto.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@
366366
%Include auto_generated/qgsvectorlayertools.sip
367367
%Include auto_generated/qgsvectorsimplifymethod.sip
368368
%Include auto_generated/qgssettings.sip
369+
%Include auto_generated/qgsprojecttranslator.sip
369370
%Include auto_generated/annotations/qgsannotation.sip
370371
%Include auto_generated/annotations/qgsannotationmanager.sip
371372
%Include auto_generated/annotations/qgshtmlannotation.sip
@@ -441,4 +442,5 @@
441442
%Include auto_generated/layertree/qgslayertreeregistrybridge.sip
442443
%Include auto_generated/qgsuserprofilemanager.sip
443444
%Include auto_generated/symbology/qgsarrowsymbollayer.sip
445+
%Include auto_generated/qgstranslationcontext.sip
444446
%Include auto_generated/qgsuserprofile.sip

src/app/qgsprojectproperties.cpp

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

833+
//fill ts language checkbox
834+
QString i18nPath = QgsApplication::i18nPath();
835+
QDir i18Dir( i18nPath, QStringLiteral( "qgis*.qm" ) );
836+
const QStringList qmFileList = i18Dir.entryList();
837+
for ( const QString &qmFile : qmFileList )
838+
{
839+
// Ignore the 'en' translation file, already added as 'en_US'.
840+
if ( qmFile.compare( QLatin1String( "qgis_en.qm" ) ) == 0 ) continue;
841+
842+
QString qmFileName = qmFile;
843+
QString l = qmFileName.remove( QStringLiteral( "qgis_" ) ).remove( QStringLiteral( ".qm" ) );
844+
845+
// QTBUG-57802: eo locale is improperly handled
846+
QString displayName = l.startsWith( QLatin1String( "eo" ) ) ? QLocale::languageToString( QLocale::Esperanto ) : QLocale( l ).nativeLanguageName();
847+
cbtsLocale->addItem( QIcon( QStringLiteral( ":/images/flags/%1.svg" ).arg( l ) ), displayName, l );
848+
}
849+
850+
cbtsLocale->addItem( QIcon( QStringLiteral( ":/images/flags/%1.svg" ).arg( QStringLiteral( "en_US" ) ) ), QLocale( QStringLiteral( "en_US" ) ).nativeLanguageName(), QStringLiteral( "en_US" ) );
851+
cbtsLocale->setCurrentIndex( cbtsLocale->findData( settings.value( QStringLiteral( "locale/userLocale" ), QString() ).toString() ) );
852+
853+
connect( generateTsFileButton, &QPushButton::clicked, this, &QgsProjectProperties::onGenerateTsFileButton );
854+
833855
projectionSelectorInitialized();
834856
populateRequiredLayers();
835857
restoreOptionsBaseUi();
@@ -2190,3 +2212,9 @@ void QgsProjectProperties::setCurrentPage( const QString &pageWidgetName )
21902212
}
21912213
}
21922214
}
2215+
2216+
void QgsProjectProperties::onGenerateTsFileButton() const
2217+
{
2218+
QString l = cbtsLocale->currentData().toString();
2219+
QgsProject::instance()->generateTsFile( l );
2220+
}

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+
//! generate the ts file with the locale selected in the checkbox
96+
void onGenerateTsFileButton() const;
97+
9598
/**
9699
* Set WMS default extent to current canvas extent
97100
*/

0 commit comments

Comments
 (0)