QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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 <QDir>
19#include <QTextStream>
20#include <QDomElement>
21#include <QDomDocument>
22
23#include "qgssettings.h"
24
26{
27 return mProject;
28}
29
31{
32 mProject = project;
33}
34
36{
37 return mFileName;
38}
39
40void QgsTranslationContext::setFileName( const QString &fileName )
41{
42 mFileName = fileName;
43}
44
45void QgsTranslationContext::registerTranslation( const QString &context, const QString &source )
46{
47 TranslatableObject translatableObject;
48 translatableObject.context = context;
49 translatableObject.source = source;
50 mTranslatableObjects.append( translatableObject );
51}
52
53void QgsTranslationContext::writeTsFile( const QString &locale ) const
54{
55 //write xml
56 QDomDocument doc( QStringLiteral( "TS" ) );
57
58 QDomElement tsElement = doc.createElement( QStringLiteral( "TS" ) );
59 tsElement.setAttribute( QStringLiteral( "sourcelanguage" ), locale );
60 doc.appendChild( tsElement );
61
62 for ( const TranslatableObject &translatableObject : mTranslatableObjects )
63 {
64 QDomElement contextElement = doc.createElement( QStringLiteral( "context" ) );
65 tsElement.appendChild( contextElement );
66
67 QDomElement nameElement = doc.createElement( QStringLiteral( "name" ) );
68 const QDomText nameText = doc.createTextNode( translatableObject.context );
69 nameElement.appendChild( nameText );
70 contextElement.appendChild( nameElement );
71
72 QDomElement messageElement = doc.createElement( QStringLiteral( "message" ) );
73 contextElement.appendChild( messageElement );
74
75 QDomElement sourceElement = doc.createElement( QStringLiteral( "source" ) );
76 const QDomText sourceText = doc.createTextNode( translatableObject.source );
77 sourceElement.appendChild( sourceText );
78 messageElement.appendChild( sourceElement );
79
80 QDomElement translationElement = doc.createElement( QStringLiteral( "translation" ) );
81 translationElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "unfinished" ) );
82 messageElement.appendChild( translationElement );
83 }
84
85 //write file
86 QFile tsFile( fileName() );
87 tsFile.open( QIODevice::WriteOnly );
88 QTextStream stream( &tsFile );
89#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
90 stream.setCodec( "UTF-8" );
91#endif
92 stream << doc.toString();
93 tsFile.close();
94}
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
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.