QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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  ***************************************************************************/
16 #include "qgstranslationcontext.h"
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 
41 {
42  mFileName = fileName;
43 }
44 
45 void 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 
53 void QgsTranslationContext::writeTsFile( const QString &locale )
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  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  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  stream << doc.toString();
90  tsFile.close();
91 }
Reads and writes project states.
Definition: qgsproject.h:89
QString fileName() const
Returns the TS fileName.
void writeTsFile(const QString &locale)
Writes the Ts-file.
void setFileName(const QString &fileName)
Sets the fileName of the TS file.
void registerTranslation(const QString &context, const QString &source)
Registers the source to be translated.
void setProject(QgsProject *project)
Sets the project being translated.
QgsProject * project() const
Returns the project.