QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
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 "
qgssettings.h
"
19
20
#include <QDir>
21
#include <QDomDocument>
22
#include <QDomElement>
23
#include <QString>
24
#include <QTextStream>
25
26
using namespace
Qt::StringLiterals;
27
28
QgsProject
*
QgsTranslationContext::project
()
const
29
{
30
return
mProject;
31
}
32
33
void
QgsTranslationContext::setProject
(
QgsProject
*
project
)
34
{
35
mProject =
project
;
36
}
37
38
QString
QgsTranslationContext::fileName
()
const
39
{
40
return
mFileName;
41
}
42
43
void
QgsTranslationContext::setFileName
(
const
QString &
fileName
)
44
{
45
mFileName =
fileName
;
46
}
47
48
void
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
59
void
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
}
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition
qgsproject.h:114
QgsTranslationContext::fileName
QString fileName() const
Returns the TS fileName.
Definition
qgstranslationcontext.cpp:38
QgsTranslationContext::registerTranslation
void registerTranslation(const QString &context, const QString &source)
Registers the source to be translated.
Definition
qgstranslationcontext.cpp:48
QgsTranslationContext::project
QgsProject * project() const
Returns the project.
Definition
qgstranslationcontext.cpp:28
QgsTranslationContext::setFileName
void setFileName(const QString &fileName)
Sets the fileName of the TS file.
Definition
qgstranslationcontext.cpp:43
QgsTranslationContext::writeTsFile
void writeTsFile(const QString &locale) const
Writes the Ts-file.
Definition
qgstranslationcontext.cpp:59
QgsTranslationContext::setProject
void setProject(QgsProject *project)
Sets the project being translated.
Definition
qgstranslationcontext.cpp:33
QgsDebugError
#define QgsDebugError(str)
Definition
qgslogger.h:71
qgssettings.h
qgstranslationcontext.h
Generated on
for QGIS API Documentation by
1.17.0