QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 <QTextStream>
24
26{
27 return mProject;
28}
29
34
36{
37 return mFileName;
38}
39
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 if ( !mTranslatableObjects.contains( translatableObject ) )
51 {
52 mTranslatableObjects.append( translatableObject );
53 }
54}
55
56void QgsTranslationContext::writeTsFile( const QString &locale ) const
57{
58 //write xml
59 QDomDocument doc( QStringLiteral( "TS" ) );
60
61 QDomElement tsElement = doc.createElement( QStringLiteral( "TS" ) );
62 tsElement.setAttribute( QStringLiteral( "sourcelanguage" ), locale );
63 doc.appendChild( tsElement );
64
65 for ( const TranslatableObject &translatableObject : mTranslatableObjects )
66 {
67 QDomElement contextElement = doc.createElement( QStringLiteral( "context" ) );
68 tsElement.appendChild( contextElement );
69
70 QDomElement nameElement = doc.createElement( QStringLiteral( "name" ) );
71 const QDomText nameText = doc.createTextNode( translatableObject.context );
72 nameElement.appendChild( nameText );
73 contextElement.appendChild( nameElement );
74
75 QDomElement messageElement = doc.createElement( QStringLiteral( "message" ) );
76 contextElement.appendChild( messageElement );
77
78 QDomElement sourceElement = doc.createElement( QStringLiteral( "source" ) );
79 const QDomText sourceText = doc.createTextNode( translatableObject.source );
80 sourceElement.appendChild( sourceText );
81 messageElement.appendChild( sourceElement );
82
83 QDomElement translationElement = doc.createElement( QStringLiteral( "translation" ) );
84 translationElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "unfinished" ) );
85 messageElement.appendChild( translationElement );
86 }
87
88 //write file
89 QFile tsFile( fileName() );
90 if ( !tsFile.open( QIODevice::WriteOnly ) )
91 {
92 QgsDebugError( QStringLiteral( "Can't open file %1" ).arg( fileName() ) );
93 return;
94 }
95 QTextStream stream( &tsFile );
96#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
97 stream.setCodec( "UTF-8" );
98#endif
99 stream << doc.toString();
100 tsFile.close();
101}
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:109
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:57