Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qph.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "translator.h"
5
6#include <QtCore/QByteArray>
7#include <QtCore/QDebug>
8#include <QtCore/QTextStream>
9
10#include <QtCore/QXmlStreamReader>
11
13
15{
16public:
17 QPHReader(QIODevice &dev)
19 {}
20
21 // the "real thing"
22 bool read(Translator &translator);
23
24private:
25 bool isWhiteSpace() const
26 {
27 return isCharacters() && text().toString().trimmed().isEmpty();
28 }
29
30 enum DataField { NoField, SourceField, TargetField, DefinitionField };
31 DataField m_currentField;
32 QString m_currentSource;
33 QString m_currentTarget;
34 QString m_currentDefinition;
35};
36
37bool QPHReader::read(Translator &translator)
38{
39 m_currentField = NoField;
40 while (!atEnd()) {
41 readNext();
42 if (isStartElement()) {
43 if (name() == QLatin1String("source")) {
44 m_currentField = SourceField;
45 } else if (name() == QLatin1String("target")) {
46 m_currentField = TargetField;
47 } else if (name() == QLatin1String("definition")) {
48 m_currentField = DefinitionField;
49 } else {
50 m_currentField = NoField;
51 if (name() == QLatin1String("QPH")) {
52 QXmlStreamAttributes atts = attributes();
53 translator.setLanguageCode(atts.value(QLatin1String("language")).toString());
54 translator.setSourceLanguageCode(atts.value(QLatin1String("sourcelanguage")).toString());
55 }
56 }
57 } else if (isWhiteSpace()) {
58 // ignore these
59 } else if (isCharacters()) {
60 if (m_currentField == SourceField)
61 m_currentSource += text();
62 else if (m_currentField == TargetField)
63 m_currentTarget += text();
64 else if (m_currentField == DefinitionField)
65 m_currentDefinition += text();
66 } else if (isEndElement() && name() == QLatin1String("phrase")) {
67 m_currentTarget.replace(Translator::TextVariantSeparator, Translator::BinaryVariantSeparator);
69 msg.setSourceText(m_currentSource);
70 msg.setTranslation(m_currentTarget);
71 msg.setComment(m_currentDefinition);
72 translator.append(msg);
73 m_currentSource.clear();
74 m_currentTarget.clear();
75 m_currentDefinition.clear();
76 }
77 }
78 return true;
79}
80
81static bool loadQPH(Translator &translator, QIODevice &dev, ConversionData &)
82{
84 QPHReader reader(dev);
85 return reader.read(translator);
86}
87
88static QString qphProtect(const QString &str)
89{
90 QString result;
91 result.reserve(str.size() * 12 / 10);
92 for (int i = 0; i != str.size(); ++i) {
93 uint c = str.at(i).unicode();
94 switch (c) {
95 case '\"':
96 result += QLatin1String("&quot;");
97 break;
98 case '&':
99 result += QLatin1String("&amp;");
100 break;
101 case '>':
102 result += QLatin1String("&gt;");
103 break;
104 case '<':
105 result += QLatin1String("&lt;");
106 break;
107 case '\'':
108 result += QLatin1String("&apos;");
109 break;
110 default:
111 if (c < 0x20 && c != '\r' && c != '\n' && c != '\t')
112 result += QString(QLatin1String("&#%1;")).arg(c);
113 else // this also covers surrogates
114 result += QChar(c);
115 }
116 }
117 return result;
118}
119
120static bool saveQPH(const Translator &translator, QIODevice &dev, ConversionData &)
121{
122 QTextStream t(&dev);
123 t << "<!DOCTYPE QPH>\n<QPH";
124 QString languageCode = translator.languageCode();
125 if (!languageCode.isEmpty() && languageCode != QLatin1String("C"))
126 t << " language=\"" << languageCode << "\"";
127 languageCode = translator.sourceLanguageCode();
128 if (!languageCode.isEmpty() && languageCode != QLatin1String("C"))
129 t << " sourcelanguage=\"" << languageCode << "\"";
130 t << ">\n";
131 for (const TranslatorMessage &msg : translator.messages()) {
132 t << "<phrase>\n";
133 t << " <source>" << qphProtect(msg.sourceText()) << "</source>\n";
134 QString str = msg.translations().join(QLatin1Char('@'));
135 str.replace(QChar(Translator::BinaryVariantSeparator),
136 QChar(Translator::TextVariantSeparator));
137 t << " <target>" << qphProtect(str)
138 << "</target>\n";
139 if (!msg.comment().isEmpty())
140 t << " <definition>" << qphProtect(msg.comment()) << "</definition>\n";
141 t << "</phrase>\n";
142 }
143 t << "</QPH>\n";
144 return true;
145}
146
148{
149 Translator::FileFormat format;
150
151 format.extension = QLatin1String("qph");
152 format.untranslatedDescription = QT_TRANSLATE_NOOP("FMT", "Qt Linguist 'Phrase Book'");
154 format.priority = 0;
155 format.loader = &loadQPH;
156 format.saver = &saveQPH;
158
159 return 1;
160}
161
162Q_CONSTRUCTOR_FUNCTION(initQPH)
163
164QT_END_NAMESPACE
bool read(Translator &translator)
Definition qph.cpp:37
QPHReader(QIODevice &dev)
Definition qph.cpp:17
static void registerFileFormat(const FileFormat &format)
void append(const TranslatorMessage &msg)
void setLocationsType(LocationsType lt)
Definition translator.h:132
Combined button and popup list for selecting options.
static bool saveQPH(const Translator &translator, QIODevice &dev, ConversionData &)
Definition qph.cpp:120
static QString qphProtect(const QString &str)
Definition qph.cpp:88
static bool loadQPH(Translator &translator, QIODevice &dev, ConversionData &)
Definition qph.cpp:81
int initQPH()
Definition qph.cpp:147
const char * untranslatedDescription
Definition translator.h:171