forked from vslavik/poedit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qa_checks.cpp
377 lines (319 loc) · 12 KB
/
qa_checks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* This file is part of Poedit (https://poedit.net)
*
* Copyright (C) 2017-2020 Vaclav Slavik
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#include "qa_checks.h"
#include <unicode/uchar.h>
#include <wx/translation.h>
// -------------------------------------------------------------
// QACheck implementations
// -------------------------------------------------------------
namespace QA
{
class NotAllPlurals : public QACheck
{
public:
bool CheckItem(CatalogItemPtr item) override
{
if (!item->HasPlural())
return false;
bool foundTranslated = false;
bool foundEmpty = false;
for (auto& s: item->GetTranslations())
{
if (s.empty())
foundEmpty = true;
else
foundTranslated = true;
}
if (foundEmpty && foundTranslated)
{
item->SetIssue(CatalogItem::Issue::Warning, _("Not all plural forms are translated."));
return true;
}
return false;
}
};
class CaseMismatch : public QACheck
{
public:
CaseMismatch(Language lang) : m_lang(lang.Lang())
{
}
bool CheckString(CatalogItemPtr item, const wxString& source, const wxString& translation) override
{
if (u_isupper(source[0]) && u_islower(translation[0]))
{
item->SetIssue(CatalogItem::Issue::Warning, _("The translation should start as a sentence."));
return true;
}
if (u_islower(source[0]) && u_isupper(translation[0]))
{
if (m_lang != "de")
{
item->SetIssue(CatalogItem::Issue::Warning, _("The translation should start with a lowercase character."));
return true;
}
// else: German nouns start uppercased, this would cause too many false positives
}
return false;
}
private:
std::string m_lang;
};
class WhitespaceMismatch : public QACheck
{
public:
bool CheckString(CatalogItemPtr item, const wxString& source, const wxString& translation) override
{
if (u_isspace(source[0]) && !u_isspace(translation[0]))
{
item->SetIssue(CatalogItem::Issue::Warning, _(L"The translation doesn’t start with a space."));
return true;
}
if (!u_isspace(source[0]) && u_isspace(translation[0]))
{
item->SetIssue(CatalogItem::Issue::Warning, _(L"The translation starts with a space, but the source text doesn’t."));
return true;
}
if (source.Last() == '\n' && translation.Last() != '\n')
{
item->SetIssue(CatalogItem::Issue::Warning, _(L"The translation is missing a newline at the end."));
return true;
}
if (source.Last() != '\n' && translation.Last() == '\n')
{
item->SetIssue(CatalogItem::Issue::Warning, _(L"The translation ends with a newline, but the source text doesn’t."));
return true;
}
if (u_isspace(source.Last()) && !u_isspace(translation.Last()))
{
item->SetIssue(CatalogItem::Issue::Warning, _(L"The translation is missing a space at the end."));
return true;
}
if (!u_isspace(source.Last()) && u_isspace(translation.Last()))
{
item->SetIssue(CatalogItem::Issue::Warning, _(L"The translation ends with a space, but the source text doesn’t."));
return true;
}
return false;
}
};
class PunctuationMismatch : public QACheck
{
public:
PunctuationMismatch(Language lang) : m_lang(lang.Lang())
{
}
bool CheckString(CatalogItemPtr item, const wxString& source, const wxString& translation) override
{
if (m_lang == "th" || m_lang == "lo" || m_lang == "km" || m_lang == "my")
{
// For Thai, Lao, Khmer and Burmese,
// the punctuation rules are so different that these checks don't
// apply at all (with the possible exception of quote marks - TODO).
// It's better to skip them than to spam the user with bogus warnings
// on _everything_.
// See https://www.ccjk.com/punctuation-rule-for-bahasa-vietnamese-and-thai/
return false;
}
const UChar32 s_last = source.Last();
const UChar32 t_last = translation.Last();
const bool s_punct = u_hasBinaryProperty(s_last, UCHAR_TERMINAL_PUNCTUATION) || u_hasBinaryProperty(s_last, UCHAR_QUOTATION_MARK);
const bool t_punct = u_hasBinaryProperty(t_last, UCHAR_TERMINAL_PUNCTUATION) || u_hasBinaryProperty(t_last, UCHAR_QUOTATION_MARK);
if (u_getIntPropertyValue(s_last, UCHAR_BIDI_PAIRED_BRACKET_TYPE) == U_BPT_CLOSE ||
u_getIntPropertyValue(t_last, UCHAR_BIDI_PAIRED_BRACKET_TYPE) == U_BPT_CLOSE)
{
// too many reordering related false positives for brackets
// e.g. "your {site} account" -> "váš účet na {site}"
if ((wchar_t)u_getBidiPairedBracket(s_last) != (wchar_t)source[0])
{
return false;
}
else
{
// OTOH, it's desirable to check strings fully enclosed in brackets like "(unsaved)"
if (source.find_first_of((wchar_t)s_last, 1) != source.size() - 1)
{
// it's more complicated, possibly something like "your {foo} on {bar}"
return false;
}
}
}
if (u_hasBinaryProperty(s_last, UCHAR_QUOTATION_MARK) || (!s_punct && u_hasBinaryProperty(t_last, UCHAR_QUOTATION_MARK)))
{
// quoted fragments can move around, e.g., so ignore quotes in reporting:
// >> Invalid value for ‘{fieldName}’ field
// >> Valor inválido para el campo ‘{fieldName}’
// TODO: count quote characters to check if used correctly in translation; don't check position
return false;
}
if (s_punct && !t_punct)
{
item->SetIssue(CatalogItem::Issue::Warning,
wxString::Format(_(L"The translation should end with “%s”."), wxString(wxUniChar(s_last))));
return true;
}
else if (!s_punct && t_punct)
{
item->SetIssue(CatalogItem::Issue::Warning,
wxString::Format(_(L"The translation should not end with “%s”."), wxString(wxUniChar(t_last))));
return true;
}
else if (s_punct && t_punct && s_last != t_last)
{
if (t_last == L'…' && source.EndsWith("..."))
{
// as a special case, allow translating ... (3 dots) as … (ellipsis)
}
else if (u_hasBinaryProperty(s_last, UCHAR_QUOTATION_MARK) && u_hasBinaryProperty(t_last, UCHAR_QUOTATION_MARK))
{
// don't check for correct quotes for now, accept any quotations marks as equal
}
else if (IsEquivalent(s_last, t_last))
{
// some characters are mostly equivalent and we shouldn't warn about them
}
else
{
item->SetIssue(CatalogItem::Issue::Warning,
wxString::Format(_(L"The translation ends with “%s”, but the source text ends with “%s”."),
wxString(wxUniChar(t_last)), wxString(wxUniChar(s_last))));
return true;
}
}
return false;
}
private:
bool IsEquivalent(UChar32 src, UChar32 trans) const
{
if (src == trans)
return true;
if (m_lang == "zh" || m_lang == "ja")
{
// Chinese uses full-width punctuation.
// See https://en.wikipedia.org/wiki/Chinese_punctuation
switch (src)
{
case '.':
return trans == L'。';
case ',':
return trans == L'、';
case '!':
return trans == L'!';
case '?':
return trans == L'?';
case ':':
return trans == L':';
case '(':
return trans == L'(';
case ')':
return trans == L')';
default:
break;
}
}
else if (m_lang == "ar" || m_lang == "fa")
{
// In Arabic (but not other RTL languages), some punctuation is mirrored.
switch (src)
{
case ';':
return trans == L'؛';
case '?':
return trans == L'؟';
case ',':
return trans == L'،';
default:
break;
}
}
else if (m_lang == "el")
{
// In Greek, questions end with ';' and not '?'.
if (src == '?')
return trans == ';';
}
return false;
}
std::string m_lang;
};
} // namespace QA
// -------------------------------------------------------------
// QACheck support code
// -------------------------------------------------------------
bool QACheck::CheckItem(CatalogItemPtr item)
{
if (!item->GetTranslation().empty() && CheckString(item, item->GetString(), item->GetTranslation()))
return true;
if (item->HasPlural())
{
unsigned count = item->GetNumberOfTranslations();
for (unsigned i = 1; i < count; i++)
{
auto t = item->GetTranslation(i);
if (!t.empty() && CheckString(item, item->GetPluralString(), t))
return true;
}
}
return false;
}
bool QACheck::CheckString(CatalogItemPtr /*item*/, const wxString& /*source*/, const wxString& /*translation*/)
{
wxFAIL_MSG("not implemented - must override CheckString OR CheckItem");
return false;
}
// -------------------------------------------------------------
// QAChecker
// -------------------------------------------------------------
int QAChecker::Check(Catalog& catalog)
{
// TODO: parallelize this (make async tasks for chunks of the catalog)
// doing it per-checker is MT-unsafe with API that calls SetIssue()!
int issues = 0;
for (auto& i: catalog.items())
issues += Check(i);
return issues;
}
int QAChecker::Check(CatalogItemPtr item)
{
int issues = 0;
for (auto& c: m_checks)
{
if (item->GetString().empty() || (item->HasPlural() && item->GetPluralString().empty()))
continue;
if (c->CheckItem(item))
issues++;
}
return issues;
}
std::shared_ptr<QAChecker> QAChecker::GetFor(Catalog& catalog)
{
auto lang = catalog.GetLanguage();
auto c = std::make_shared<QAChecker>();
c->AddCheck<QA::NotAllPlurals>();
c->AddCheck<QA::CaseMismatch>(lang);
c->AddCheck<QA::WhitespaceMismatch>();
c->AddCheck<QA::PunctuationMismatch>(lang);
return c;
}