forked from vslavik/poedit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog.h
666 lines (540 loc) · 23.4 KB
/
catalog.h
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*
* This file is part of Poedit (http://www.poedit.net)
*
* Copyright (C) 1999-2010 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.
*
*/
#ifndef _CATALOG_H_
#define _CATALOG_H_
#include <wx/encconv.h>
#include <wx/regex.h>
#include <wx/arrstr.h>
#include <vector>
#include <map>
class WXDLLIMPEXP_FWD_BASE wxTextFile;
class ProgressInfo;
/// The possible bookmarks for a given item
typedef enum
{
NO_BOOKMARK = -1,
BOOKMARK_0,
BOOKMARK_1,
BOOKMARK_2,
BOOKMARK_3,
BOOKMARK_4,
BOOKMARK_5,
BOOKMARK_6,
BOOKMARK_7,
BOOKMARK_8,
BOOKMARK_9,
BOOKMARK_LAST
} Bookmark;
/** This class holds information about one particular string.
This includes source string and its occurrences in source code
(so-called references), translation and translation's status
(fuzzy, non translated, translated) and optional comment.
This class is mostly internal, used by Catalog to store data.
*/
class CatalogItem
{
public:
/// Ctor. Initializes the object with source string and translation.
CatalogItem(const wxString& str = wxEmptyString,
const wxString& str_plural = wxEmptyString)
: m_string(str),
m_plural(str_plural),
m_hasPlural(false),
m_hasContext(false),
m_references(),
m_isFuzzy(false),
m_isTranslated(false),
m_isModified(false),
m_isAutomatic(false),
m_validity(Val_Unknown),
m_lineNum(0),
m_bookmark(NO_BOOKMARK) {}
CatalogItem(const CatalogItem& dt)
: m_string(dt.m_string),
m_plural(dt.m_plural),
m_hasPlural(dt.m_hasPlural),
m_hasContext(dt.m_hasContext),
m_context(dt.m_context),
m_translations(dt.m_translations),
m_references(dt.m_references),
m_autocomments(dt.m_autocomments),
m_oldMsgid(dt.m_oldMsgid),
m_isFuzzy(dt.m_isFuzzy),
m_isTranslated(dt.m_isTranslated),
m_isModified(dt.m_isModified),
m_isAutomatic(dt.m_isAutomatic),
m_hasBadTokens(dt.m_hasBadTokens),
m_moreFlags(dt.m_moreFlags),
m_comment(dt.m_comment),
m_validity(dt.m_validity),
m_lineNum(dt.m_lineNum),
m_errorString(dt.m_errorString),
m_bookmark(dt.m_bookmark) {}
/// Returns the source string.
const wxString& GetString() const { return m_string; }
/// Does this entry have a msgid_plural?
bool HasPlural() const { return m_hasPlural; }
/// Returns the plural string.
const wxString& GetPluralString() const { return m_plural; }
/// Does this entry have a msgctxt?
bool HasContext() const { return m_hasContext; }
/// Returns context string (can only be called if HasContext() returns
/// true and empty string is accepted value).
const wxString& GetContext() const { return m_context; }
/// How many translations (plural forms) do we have?
unsigned GetNumberOfTranslations() const
{ return m_translations.size(); }
/// Returns number of plural forms in this translation; note that this
/// may be less than what the header says, because some may be
/// still untranslated
unsigned GetPluralFormsCount() const;
/// Returns the nth-translation.
wxString GetTranslation(unsigned n = 0) const;
/// Returns all translations.
const wxArrayString& GetTranslations() const
{ return m_translations; }
/// Returns references (#:) lines for the entry
const wxArrayString& GetRawReferences() const { return m_references; }
/// Returns array of all occurrences of this string in source code,
/// parsed into individual references
wxArrayString GetReferences() const;
/// Returns comment added by the translator to this entry
const wxString& GetComment() const { return m_comment; }
/// Returns array of all auto comments.
const wxArrayString& GetAutoComments() const { return m_autocomments; }
/// Convenience function: does this entry has a comment?
bool HasComment() const { return !m_comment.empty(); }
/// Adds new reference to the entry (used by SourceDigger).
void AddReference(const wxString& ref)
{
if (m_references.Index(ref) == wxNOT_FOUND)
m_references.Add(ref);
}
/// Sets the string.
void SetString(const wxString& s)
{
m_string = s;
m_validity = Val_Unknown;
}
/// Sets the plural form (if applicable).
void SetPluralString(const wxString& p)
{
m_plural = p;
m_hasPlural = true;
}
/// Sets the context (msgctxt0
void SetContext(const wxString& context)
{
m_hasContext = true;
m_context = context;
}
/** Sets the translation. Changes "translated" status to true
if \a t is not empty.
*/
void SetTranslation(const wxString& t, unsigned index = 0);
/// Sets all translations.
void SetTranslations(const wxArrayString& t);
/// Sets the comment.
void SetComment(const wxString& c)
{
m_comment = c;
}
/** Sets gettext flags directly in string format. It may be
either empty string or "#, fuzzy", "#, c-format",
"#, fuzzy, c-format" or others (not understood by Poedit).
*/
void SetFlags(const wxString& flags);
/// Gets gettext flags. \see SetFlags
wxString GetFlags() const;
/// Sets fuzzy flag.
void SetFuzzy(bool fuzzy) { m_isFuzzy = fuzzy; }
/// Gets value of fuzzy flag.
bool IsFuzzy() const { return m_isFuzzy; }
/// Sets translated flag.
void SetTranslated(bool t) { m_isTranslated = t; }
/// Gets value of translated flag.
bool IsTranslated() const { return m_isTranslated; }
/// Sets modified flag.
void SetModified(bool modified) { m_isModified = modified; }
/// Gets value of modified flag.
bool IsModified() const { return m_isModified; }
/// Sets automatic translation flag.
void SetAutomatic(bool automatic) { m_isAutomatic = automatic; }
/// Gets value of automatic translation flag.
bool IsAutomatic() const { return m_isAutomatic; }
/// Sets the number of the line this entry occurs on.
void SetLineNumber(unsigned line) { m_lineNum = line; }
/// Get line number of this entry.
unsigned GetLineNumber() const { return m_lineNum; }
/** Returns true if the gettext flags line contains "foo-format"
flag when called with "foo" as argument. */
bool IsInFormat(const wxString& format);
/// Adds new autocomments (#. )
void AddAutoComments(const wxString& com)
{
m_autocomments.Add(com);
}
/// Clears autocomments.
void ClearAutoComments()
{
m_autocomments.Clear();
}
void SetOldMsgid(const wxArrayString& data) { m_oldMsgid = data; }
const wxArrayString& GetOldMsgid() const { return m_oldMsgid; }
// Validity (syntax-checking) status of the entry:
enum Validity
{
Val_Unknown = -1,
Val_Invalid = 0,
Val_Valid = 1
};
/** Checks if %i etc. are correct in the translation (true if yes).
Strings that are not c-format are always correct. */
Validity GetValidity() const { return m_validity; }
void SetValidity(bool val)
{ m_validity = val ? Val_Valid : Val_Invalid; }
void SetErrorString(const wxString& str) { m_errorString = str; }
wxString GetErrorString() const { return m_errorString; }
/// Returns the bookmark for the item
Bookmark GetBookmark() const {return m_bookmark;}
/// Returns true if the item has a bookmark
bool HasBookmark() const {return (GetBookmark() != NO_BOOKMARK);}
/// Sets the bookmark
void SetBookmark(Bookmark bookmark) {m_bookmark = bookmark;}
private:
wxString m_string, m_plural;
bool m_hasPlural;
bool m_hasContext;
wxString m_context;
wxArrayString m_translations;
wxArrayString m_references, m_autocomments;
wxArrayString m_oldMsgid;
bool m_isFuzzy, m_isTranslated, m_isModified, m_isAutomatic;
bool m_hasBadTokens;
wxString m_moreFlags;
wxString m_comment;
Validity m_validity;
unsigned m_lineNum;
wxString m_errorString;
Bookmark m_bookmark;
};
/** This class holds information about one particular deleted item.
This includes deleted lines, references, translation's status
(fuzzy, non translated, translated) and optional comment(s).
This class is mostly internal, used by Catalog to store data.
*/
// FIXME: derive this from CatalogItem (or CatalogItemBase)
class CatalogDeletedData
{
public:
/// Ctor.
CatalogDeletedData()
: m_lineNum(0) {}
CatalogDeletedData(const wxArrayString& deletedLines)
: m_deletedLines(deletedLines),
m_lineNum(0) {}
CatalogDeletedData(const CatalogDeletedData& dt)
: m_deletedLines(dt.m_deletedLines),
m_references(dt.m_references),
m_autocomments(dt.m_autocomments),
m_flags(dt.m_flags),
m_comment(dt.m_comment),
m_lineNum(dt.m_lineNum) {}
/// Returns the deleted lines.
const wxArrayString& GetDeletedLines() const { return m_deletedLines; }
/// Returns references (#:) lines for the entry
const wxArrayString& GetRawReferences() const { return m_references; }
/// Returns comment added by the translator to this entry
const wxString& GetComment() const { return m_comment; }
/// Returns array of all auto comments.
const wxArrayString& GetAutoComments() const { return m_autocomments; }
/// Convenience function: does this entry has a comment?
bool HasComment() const { return !m_comment.empty(); }
/// Adds new reference to the entry (used by SourceDigger).
void AddReference(const wxString& ref)
{
if (m_references.Index(ref) == wxNOT_FOUND)
m_references.Add(ref);
}
/// Sets the string.
void SetDeletedLines(const wxArrayString& a)
{
m_deletedLines = a;
}
/// Sets the comment.
void SetComment(const wxString& c)
{
m_comment = c;
}
/** Sets gettext flags directly in string format. It may be
either empty string or "#, fuzzy", "#, c-format",
"#, fuzzy, c-format" or others (not understood by Poedit).
*/
void SetFlags(const wxString& flags) {m_flags = flags;};
/// Gets gettext flags. \see SetFlags
wxString GetFlags() const {return m_flags;};
/// Sets the number of the line this entry occurs on.
void SetLineNumber(unsigned line) { m_lineNum = line; }
/// Get line number of this entry.
unsigned GetLineNumber() const { return m_lineNum; }
/// Adds new autocomments (#. )
void AddAutoComments(const wxString& com)
{
m_autocomments.Add(com);
}
/// Clears autocomments.
void ClearAutoComments()
{
m_autocomments.Clear();
}
private:
wxArrayString m_deletedLines;
wxArrayString m_references, m_autocomments;
wxString m_flags;
wxString m_comment;
unsigned m_lineNum;
};
typedef std::vector<CatalogItem> CatalogItemArray;
typedef std::vector<CatalogDeletedData> CatalogDeletedDataArray;
typedef std::map<wxString, unsigned> CatalogItemIndex;
/** This class stores all translations, together with filelists, references
and other additional information. It can read .po files and save both
.mo and .po files. Furthermore, it provides facilities for updating the
catalog from source files.
*/
class Catalog
{
public:
/// PO file header information.
class HeaderData
{
public:
HeaderData() {}
/** Initializes the headers from string that is in msgid "" format
(i.e. list of key:value\n entries). */
void FromString(const wxString& str);
/** Converts the header into string representation that can be
directly written to .po file as msgid "". */
wxString ToString(const wxString& line_delim = wxEmptyString);
/// Updates headers list from parsed values entries below
void UpdateDict();
/// Reverse operation to UpdateDict
void ParseDict();
/// Returns value of header or empty string if missing.
wxString GetHeader(const wxString& key) const;
/// Returns true if given key is present in the header.
bool HasHeader(const wxString& key) const;
/** Sets header to given value. Overwrites old value if present,
appends to the end of header values otherwise. */
void SetHeader(const wxString& key, const wxString& value);
/// Like SetHeader, but deletes the header if value is empty
void SetHeaderNotEmpty(const wxString& key, const wxString& value);
/// Removes given header entry
void DeleteHeader(const wxString& key);
struct Entry
{
wxString Key, Value;
};
typedef std::vector<Entry> Entries;
const Entries& GetAllHeaders() const { return m_entries; }
// Parsed values:
wxString Language, Country, Project, CreationDate,
RevisionDate, Translator, TranslatorEmail,
Team, TeamEmail, Charset, SourceCodeCharset;
wxArrayString SearchPaths, Keywords;
int Bookmarks[BOOKMARK_LAST];
wxString BasePath;
wxString Comment;
protected:
Entries m_entries;
const Entry *Find(const wxString& key) const;
};
/// Default ctor. Creates empty catalog, you have to call Load.
Catalog();
/// Ctor that loads the catalog from \a po_file with Load.
Catalog(const wxString& po_file);
~Catalog();
/** Creates new, empty header. Sets Charset to something meaningful
("UTF-8", currently).
*/
void CreateNewHeader();
/** Creates new header initialized based on a POT file's header.
*/
void CreateNewHeader(const HeaderData& pot_header);
/// Clears the catalog, removes all entries from it.
void Clear();
/** Loads catalog from .po file.
If file named po_file ".poedit" (e.g. "cs.po.poedit") exists,
this function loads additional information from it. .po.poedit
file contains parts of catalog header data that are not part
of standard .po format, namely SearchPaths, Keywords, BasePath
and Language.
*/
bool Load(const wxString& po_file);
/** Saves catalog to file. Creates both .po (text) and .mo (binary)
version of the catalog (unless the latter was disabled in
preferences). Calls external xmsgfmt program to generate the .mo
file.
Note that \a po_file refers to .po file, .mo file will have same
name & location as .po file except for different extension.
*/
bool Save(const wxString& po_file, bool save_mo = true);
/// Exports the catalog to HTML format
bool ExportToHTML(const wxString& filename);
/** Updates the catalog from sources.
\see SourceDigger, Parser, UpdateFromPOT.
*/
bool Update(ProgressInfo *progress, bool summary = true);
/** Updates the catalog from POT file.
\see Update
*/
bool UpdateFromPOT(const wxString& pot_file,
bool summary = true,
bool replace_header = false);
/// Returns the number of strings/translations in the catalog.
size_t GetCount() const { return m_items.size(); }
/** Returns number of all, fuzzy, badtokens and untranslated items.
Any argument may be NULL if the caller is not interested in
given statistic value.
@note "untranslated" are entries without translation; "unfinished"
are entries with any problems
*/
void GetStatistics(int *all, int *fuzzy, int *badtokens,
int *untranslated, int *unfinished);
/// Gets n-th item in the catalog (read-write access).
CatalogItem& operator[](unsigned n) { return m_items[n]; }
/// Gets n-th item in the catalog (read-only access).
const CatalogItem& operator[](unsigned n) const { return m_items[n]; }
/// Gets catalog header (read-write access).
HeaderData& Header() { return m_header; }
/// Returns plural forms count: taken from Plural-Forms header if
/// present, 0 otherwise (unless there are existing plural forms
/// translations in the file)
unsigned GetPluralFormsCount() const;
/// Returns true if Plural-Forms header doesn't match plural forms
/// usage in catalog items
bool HasWrongPluralFormsCount() const;
/// Does this catalog have any items with plural forms?
bool HasPluralItems() const;
/** Returns status of catalog object: true if ok, false if damaged
(i.e. constructor or Load failed).
*/
bool IsOk() const { return m_isOk; }
/** Returns xx_YY ISO code of catalog's language if either the Poedit
extensions headers are present or if filename is known and is in
the xx[_YY] form, otherwise returns empty string. */
wxString GetLocaleCode() const;
/// Adds entry to the catalog (the catalog will take ownership of
/// the object).
void AddItem(const CatalogItem& data);
/// Adds entry to the catalog (the catalog will take ownership of
/// the object).
void AddDeletedItem(const CatalogDeletedData& data);
/// Returns true if the catalog contains obsolete entries (~.*)
bool HasDeletedItems();
/// Removes all obsolete translations from the catalog
void RemoveDeletedItems();
/// Sets the given item to have the given bookmark and returns the index
/// of the item that previously had this bookmark (or -1)
int SetBookmark(int id, Bookmark bookmark);
/// Returns the index of the item that has the given bookmark or -1
int GetBookmarkIndex(Bookmark bookmark) const
{
return m_header.Bookmarks[bookmark];
}
protected:
/** Merges the catalog with reference catalog
(in the sense of msgmerge -- this catalog is old one with
translations, \a refcat is reference catalog created by Update().)
\return true if the merge was successfull, false otherwise.
Note that if it returns false, the catalog was
\em not modified!
*/
bool Merge(Catalog *refcat);
/** Returns list of strings that are new in reference catalog
(compared to this one) and that are not present in \a refcat
(i.e. are obsoleted).
\see ShowMergeSummary
*/
void GetMergeSummary(Catalog *refcat,
wxArrayString& snew, wxArrayString& sobsolete);
/** Shows a dialog with merge summary.
\see GetMergeSummary, Merge
\return true if the merge was OK'ed by the user, false otherwise
*/
bool ShowMergeSummary(Catalog *refcat);
private:
CatalogItemArray m_items;
CatalogDeletedDataArray m_deletedItems;
bool m_isOk;
wxString m_fileName;
HeaderData m_header;
friend class LoadParser;
};
/// Internal class - used for parsing of po files.
class CatalogParser
{
public:
CatalogParser(wxTextFile *f) : m_textFile(f) {}
virtual ~CatalogParser() {}
/** Parses the entire file, calls OnEntry each time
new msgid/msgstr pair is found.
@return false if parsing failed, true otherwise
*/
bool Parse();
protected:
/** Called when new entry was parsed. Parsing continues
if returned value is true and is cancelled if it
is false.
*/
virtual bool OnEntry(const wxString& msgid,
const wxString& msgid_plural,
bool has_plural,
bool has_context,
const wxString& context,
const wxArrayString& mtranslations,
const wxString& flags,
const wxArrayString& references,
const wxString& comment,
const wxArrayString& autocomments,
const wxArrayString& msgid_old,
unsigned lineNumber) = 0;
/** Called when new deleted entry was parsed. Parsing continues
if returned value is true and is cancelled if it
is false. Defaults to an empty implementation.
*/
virtual bool OnDeletedEntry(const wxArrayString& /*deletedLines*/,
const wxString& /*flags*/,
const wxArrayString& /*references*/,
const wxString& /*comment*/,
const wxArrayString& /*autocomments*/,
unsigned /*lineNumber*/)
{
return true;
}
/// Textfile being parsed.
wxTextFile *m_textFile;
};
#endif // _CATALOG_H_