QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgstranslationcontext.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstranslationcontext.cpp
3
4 ---------------------
5 begin : 23.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 ***************************************************************************/
17
18#include "qgssettings.h"
19
20#include <QDir>
21#include <QDomDocument>
22#include <QDomElement>
23#include <QString>
24#include <QTextStream>
25
26using namespace Qt::StringLiterals;
27
29{
30 return mProject;
31}
32
37
39{
40 return mFileName;
41}
42
44{
45 mFileName = fileName;
46}
47
48void QgsTranslationContext::registerTranslation( const QString &context, const QString &source )
49{
50 TranslatableObject translatableObject;
51 translatableObject.context = context;
52 translatableObject.source = source;
53 if ( !mTranslatableObjects.contains( translatableObject ) )
54 {
55 mTranslatableObjects.append( translatableObject );
56 }
57}
58
59void QgsTranslationContext::writeTsFile( const QString &locale ) const
60{
61 //write xml
62 QDomDocument doc( u"TS"_s );
63
64 QDomElement tsElement = doc.createElement( u"TS"_s );
65 tsElement.setAttribute( u"sourcelanguage"_s, locale );
66 doc.appendChild( tsElement );
67
68 for ( const TranslatableObject &translatableObject : mTranslatableObjects )
69 {
70 QDomElement contextElement = doc.createElement( u"context"_s );
71 tsElement.appendChild( contextElement );
72
73 QDomElement nameElement = doc.createElement( u"name"_s );
74 const QDomText nameText = doc.createTextNode( translatableObject.context );
75 nameElement.appendChild( nameText );
76 contextElement.appendChild( nameElement );
77
78 QDomElement messageElement = doc.createElement( u"message"_s );
79 contextElement.appendChild( messageElement );
80
81 QDomElement sourceElement = doc.createElement( u"source"_s );
82 const QDomText sourceText = doc.createTextNode( translatableObject.source );
83 sourceElement.appendChild( sourceText );
84 messageElement.appendChild( sourceElement );
85
86 QDomElement translationElement = doc.createElement( u"translation"_s );
87 translationElement.setAttribute( u"type"_s, u"unfinished"_s );
88 messageElement.appendChild( translationElement );
89 }
90
91 //write file
92 QFile tsFile( fileName() );
93 if ( !tsFile.open( QIODevice::WriteOnly ) )
94 {
95 QgsDebugError( u"Can't open file %1"_s.arg( fileName() ) );
96 return;
97 }
98 QTextStream stream( &tsFile );
99 stream << doc.toString();
100 tsFile.close();
101}
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:112
QString fileName() const
Returns the TS fileName.
void registerTranslation(const QString &context, const QString &source)
Registers the source to be translated.
QgsProject * project() const
Returns the project.
void setFileName(const QString &fileName)
Sets the fileName of the TS file.
void writeTsFile(const QString &locale) const
Writes the Ts-file.
void setProject(QgsProject *project)
Sets the project being translated.
#define QgsDebugError(str)
Definition qgslogger.h:59