QtLingo 1
QtLingo is an Application to make Qt Computer Translations easier
ts_model.cpp
Go to the documentation of this file.
1#include "ts_model.h"
2
3
7namespace visitors
8{
9
13 void document_dump::visit(const document_node *node) const
14 {
15 m_writer.writeStartDocument();
16 std::for_each(node->m_childs.begin(), node->m_childs.end(), [this](const base_node::base_node_ptr node){ node->visit(*this); } );
17 m_writer.writeEndDocument();
18 }
19
23 void document_dump::visit(const DTD_node *node) const
24 {
25 m_writer.writeDTD(node->id());
26 }
27
31 void document_dump::visit(const element_node *node) const
32 {
33 m_writer.writeStartElement(node->name());
34 m_writer.writeAttributes(node->attributes());
35 m_writer.writeCharacters(node->text());
36 std::for_each(node->m_childs.begin(), node->m_childs.end(), [this](const base_node::base_node_ptr node){ node->visit(*this); } );
37 m_writer.writeEndElement();
38 }
39
44 {
45 std::for_each(node->m_childs.begin(), node->m_childs.end(), [this](const base_node::base_node_ptr node){ node->visit(*this); } );
46 }
47
51 void string_extractor_replacer::visit(const DTD_node * /*node*/) { }
52
57 {
58 if(st_WaitForMessage == m_state && element_node::ent_message == node->element_node_type())
59 {
60 m_state = st_WaitForSource | st_WaitForTranslation;
61 }
62 else if(st_WaitForSource & m_state && element_node::ent_source == node->element_node_type())
63 {
64 assert(!source);
65 source = node;
66 m_state &= ~st_WaitForSource;
67
68 if(!(m_state & st_WaitForTranslation))
69 {
70 m_state |= st_Complete;
71 }
72 else
73 {
74 return;
75 }
76 }
77 else if(st_WaitForTranslation & m_state && element_node::ent_translation == node->element_node_type())
78 {
79 assert(!translation);
80 translation = node;
81 m_state &= ~st_WaitForTranslation;
82
83 if(!(m_state & st_WaitForSource))
84 {
85 m_state |= st_Complete;
86 }
87 else
88 {
89 return;
90 }
91 }
92
93 if(st_Complete & m_state)
94 {
95 bool bSkipProcessing = false;
96
97 if(!m_with_unfinished || !m_with_vanished || !m_unfinished_only)
98 {
99 QString attr_type = translation->attributes().value("type").toString();
100 if(m_unfinished_only)
101 {
102 bSkipProcessing = "unfinished" != attr_type;
103 }
104 else
105 {
106 bSkipProcessing = ("unfinished" == attr_type && !m_with_unfinished)
107 || (("vanished" == attr_type || "obsolete" == attr_type) && !m_with_vanished);
108 }
109 }
110
111 if(!bSkipProcessing)
112 {
113 hash_t hash = 0;
114 QString text;
115
116 if(!translation->text().isEmpty())
117 {
118 text = translation->text();
119 }
120 else
121 {
122 text = source->text();
123 }
124
125 hash = efl_hash(text.toStdWString().c_str());
126 translation->set_text(QString("[[[%1]]]").arg(hash, 8, 16, QChar('0')).toUpper());
127 text = QString("%1 \"%2\"").arg(translation->text(), text);
128
129 text.replace("\n", "\\n");
130 text.replace("\r", "\\r");
131 text.replace("\t", "\\t");
132
133 m_vqs.insert(map_hashQString::value_type(hash, text));
134 }
135
136 source = translation = nullptr;
137 m_state = st_WaitForMessage;
138 }
139
140 std::for_each(node->m_childs.begin(), node->m_childs.end(), [this](const base_node::base_node_ptr node) { node->visit(*this); });
141 }
142
147 {
148 std::for_each(node->m_childs.begin(), node->m_childs.end(), [this](const base_node::base_node_ptr node){ node->visit(*this); } );
149 }
150
154 void back_string_replacer::visit(const DTD_node * /*node*/) { }
155
160 {
161 if(st_WaitForMessage == m_state && element_node::ent_message == node->element_node_type())
162 {
163 m_state = st_WaitForSource | st_WaitForTranslation;
164 }
165 else if(st_WaitForSource & m_state && element_node::ent_source == node->element_node_type())
166 {
167 assert(!source);
168 source = node;
169 m_state &= ~st_WaitForSource;
170
171 if(!(m_state & st_WaitForTranslation))
172 {
173 m_state |= st_Complete;
174 }
175 else
176 {
177 return;
178 }
179 }
180 else if(st_WaitForTranslation & m_state && element_node::ent_translation == node->element_node_type())
181 {
182 assert(!translation);
183 translation = node;
184 m_state &= ~st_WaitForTranslation;
185
186 if(!(m_state & st_WaitForSource))
187 {
188 m_state |= st_Complete;
189 }
190 else
191 {
192 return;
193 }
194 }
195
196 if(st_Complete & m_state)
197 {
198 map_QStringQString::const_iterator it = m_strings.find(translation->text());
199
200 if(m_strings.end() == it)
201 {
202 std::cerr << "Unprocessed tags <source>: " << source->text().toUtf8().constData()
203 << " <translation>: " << translation->text().toUtf8().constData() << std::endl;
204 }
205 else
206 {
207 QString text = it->second;
208
209 text.replace("\\n", "\n");
210 text.replace("\\r", "\r");
211 text.replace("\\t", "\t");
212
213 translation->set_text(text);
214 }
215
216 source = translation = nullptr;
217 m_state = st_WaitForMessage;
218 }
219
220 std::for_each(node->m_childs.begin(), node->m_childs.end(), [this](const base_node::base_node_ptr node){ node->visit(*this); } );
221 }
222
227 {
228 if(!m_langid.isEmpty())
229 {
230 node->replace_attribute_value("language", m_langid);
231 }
232
233 std::for_each(node->m_childs.begin(), node->m_childs.end(), [this](const base_node::base_node_ptr node)
234 {
235 node->visit(*this);
236 });
237 }
238}
239
unsigned long hash_t
type of efl_hash.
Definition: efl_hash.h:7
hash_t efl_hash(const wchar_t *s)
do some fancy bitwanking on the string. efl_hash
Definition: efl_hash.h:12
visitors/
DTD node.
Definition: ts_model.h:156
const QString & id() const
Definition: ts_model.h:163
TS node.
Definition: ts_model.h:202
void replace_attribute_value(const QString &att_name, const QString &value)
Definition: ts_model.h:209
std::shared_ptr< base_node > base_node_ptr
Definition: ts_model.h:116
document node.
Definition: ts_model.h:144
element node.
Definition: ts_model.h:172
const QString & text() const
Definition: ts_model.h:186
const QString & name() const
Definition: ts_model.h:188
EElementNodeType element_node_type() const
Definition: ts_model.h:183
void set_text(const QString &text)
Definition: ts_model.h:185
const QXmlStreamAttributes & attributes() const
Definition: ts_model.h:189
@ ent_translation
Definition: ts_model.h:173
void visit(const document_node *node)
visit(const document_node *node). visit
Definition: ts_model.cpp:146
void visit(const document_node *node) const
visit(const document_node *node). visit
Definition: ts_model.cpp:13
void visit(const document_node *node)
visit(const document_node *node). visit
Definition: ts_model.cpp:43