From b92c329fa526dd710299963a3a156af7cb4af5b8 Mon Sep 17 00:00:00 2001 From: Jordi Vilalta Prat Date: Wed, 8 Sep 2010 17:01:21 +0000 Subject: I18N: Don't use fuzzy translations. - Tweaked create_translations to ignore fuzzy translations. - Fixed some formatting and removed extra whitespaces. svn-id: r52638 --- tools/create_translations/create_translations.cpp | 22 +++---- tools/create_translations/po_parser.cpp | 75 ++++++++++++++--------- tools/create_translations/po_parser.h | 12 ++-- 3 files changed, 64 insertions(+), 45 deletions(-) (limited to 'tools/create_translations') diff --git a/tools/create_translations/create_translations.cpp b/tools/create_translations/create_translations.cpp index fab35cdfd5..9fcf3b4a31 100644 --- a/tools/create_translations/create_translations.cpp +++ b/tools/create_translations/create_translations.cpp @@ -52,7 +52,7 @@ void writeUint16BE(FILE *fp, uint16 value) { writeByte(fp, (uint8)(value & 0xFF)); } -int stringSize(const char* string) { +int stringSize(const char *string) { // Each string is preceded by its size coded on 2 bytes if (string == NULL) return 2; @@ -63,7 +63,7 @@ int stringSize(const char* string) { // return 2 + len + pad; } -void writeString(FILE *fp, const char* string) { +void writeString(FILE *fp, const char *string) { // Each string is preceded by its size coded on 2 bytes if (string == NULL) { writeUint16BE(fp, 0); @@ -86,12 +86,12 @@ int main(int argc, char *argv[]) { PoMessageList messageIds; PoMessageEntryList **translations = new PoMessageEntryList*[argc - 1]; int numLangs = 0; - for (int i = 1 ; i < argc ; ++i) { + for (int i = 1; i < argc; ++i) { translations[numLangs] = parsePoFile(argv[i], messageIds); if (translations[numLangs] != NULL) ++numLangs; } - + FILE *outFile; int i, lang; int len; @@ -110,7 +110,7 @@ int main(int argc, char *argv[]) { // Write number of translations writeUint16BE(outFile, numLangs); - + // Write the length of each data block here. // We could write it at the start of each block but that would mean that // to go to block 4 we would have to go at the start of each preceding block, @@ -123,7 +123,7 @@ int main(int argc, char *argv[]) { // 3. First translation // 4. Second translation // ... - + // Write length for translation description len = 0; for (lang = 0; lang < numLangs; lang++) { @@ -131,7 +131,7 @@ int main(int argc, char *argv[]) { len += stringSize(translations[lang]->languageName()); } writeUint16BE(outFile, len); - + // Write size for the original language (english) block // It starts with the number of strings coded on 2 bytes followed by each // string (two bytes for the number of chars and the string itself). @@ -158,13 +158,13 @@ int main(int argc, char *argv[]) { writeString(outFile, translations[lang]->language()); writeString(outFile, translations[lang]->languageName()); } - + // Write original messages writeUint16BE(outFile, messageIds.size()); for (i = 0; i < messageIds.size(); ++i) { writeString(outFile, messageIds[i]); } - + // Write translations for (lang = 0; lang < numLangs; lang++) { writeUint16BE(outFile, translations[lang]->size()); @@ -177,11 +177,11 @@ int main(int argc, char *argv[]) { } fclose(outFile); - + // Clean the memory for (i = 0; i < numLangs; ++i) delete translations[i]; - delete [] translations; + delete[] translations; return 0; } diff --git a/tools/create_translations/po_parser.cpp b/tools/create_translations/po_parser.cpp index 3d8e2547a0..a6a773bece 100644 --- a/tools/create_translations/po_parser.cpp +++ b/tools/create_translations/po_parser.cpp @@ -34,14 +34,14 @@ PoMessageList::PoMessageList() : _messages(NULL), _size(0), _allocated(0) { PoMessageList::~PoMessageList() { for (int i = 0; i < _size; ++i) - delete [] _messages[i]; - delete [] _messages; + delete[] _messages[i]; + delete[] _messages; } void PoMessageList::insert(const char *msg) { if (msg == NULL || *msg == '\0') return; - + // binary-search for the insertion index int leftIndex = 0; int rightIndex = _size - 1; @@ -64,7 +64,7 @@ void PoMessageList::insert(const char *msg) { newMessages[i] = _messages[i]; for (int i = leftIndex; i < _size; ++i) newMessages[i + 1] = _messages[i]; - delete [] _messages; + delete[] _messages; _messages = newMessages; } else { for (int i = _size - 1; i >= leftIndex; --i) @@ -78,11 +78,11 @@ void PoMessageList::insert(const char *msg) { int PoMessageList::findIndex(const char *msg) { if (msg == NULL || *msg == '\0') return -1; - + // binary-search for the message int leftIndex = 0; int rightIndex = _size - 1; - + while (rightIndex >= leftIndex) { const int midIndex = (leftIndex + rightIndex) / 2; const int compareResult = strcmp(msg, _messages[midIndex]); @@ -122,12 +122,12 @@ PoMessageEntryList::PoMessageEntryList(const char *lang) : } PoMessageEntryList::~PoMessageEntryList() { - delete [] _lang; - delete [] _charset; - delete [] _langName; + delete[] _lang; + delete[] _charset; + delete[] _langName; for (int i = 0; i < _size; ++i) delete _list[i]; - delete [] _list; + delete[] _list; } void PoMessageEntryList::addMessageEntry(const char *translation, const char *message, const char *context) { @@ -136,17 +136,17 @@ void PoMessageEntryList::addMessageEntry(const char *translation, const char *me // We get the charset and the language name from the translation string char *str = parseLine(translation, "Language:"); if (str != NULL) { - delete [] _langName; + delete[] _langName; _langName = str; } str = parseLine(translation, "charset="); if (str != NULL) { - delete [] _charset; + delete[] _charset; _charset = str; } return; - } - + } + // binary-search for the insertion index int leftIndex = 0; int rightIndex = _size - 1; @@ -189,8 +189,8 @@ void PoMessageEntryList::addMessageEntry(const char *translation, const char *me if (contextIndex < leftIndex && _list[contextIndex]->msgctxt == NULL && strcmp(translation, _list[contextIndex]->msgstr) == 0) return; } - - + + if (_size + 1 > _allocated) { _allocated += 100; PoMessageEntry **newList = new PoMessageEntry*[_allocated]; @@ -198,7 +198,7 @@ void PoMessageEntryList::addMessageEntry(const char *translation, const char *me newList[i] = _list[i]; for (int i = leftIndex; i < _size; ++i) newList[i + 1] = _list[i]; - delete [] _list; + delete[] _list; _list = newList; } else { for (int i = _size - 1; i >= leftIndex; --i) @@ -206,7 +206,7 @@ void PoMessageEntryList::addMessageEntry(const char *translation, const char *me } _list[leftIndex] = new PoMessageEntry(translation, message, context); ++_size; - + if (context == NULL || *context == '\0') { // Remove identical translations for a specific context (see comment above) int contextIndex = leftIndex + 1; @@ -228,7 +228,7 @@ void PoMessageEntryList::addMessageEntry(const char *translation, const char *me } _size -= removed; } - + } const char *PoMessageEntryList::language() const { @@ -258,10 +258,10 @@ PoMessageEntryList *parsePoFile(const char *file, PoMessageList& messages) { FILE *inFile = fopen(file, "r"); if (!inFile) return NULL; - + char msgidBuf[1024], msgctxtBuf[1024], msgstrBuf[1024]; - char line[1024], *currentBuf = NULL; - + char line[1024], *currentBuf = msgstrBuf; + // Get language from file name and create PoMessageEntryList int index = 0, start_index = strlen(file) - 1; while (start_index > 0 && file[start_index - 1] != '/' && file[start_index - 1] != '\\') { @@ -273,34 +273,53 @@ PoMessageEntryList *parsePoFile(const char *file, PoMessageList& messages) { } msgidBuf[index] = '\0'; PoMessageEntryList *list = new PoMessageEntryList(msgidBuf); - + + // Initialize the message attributes. + bool fuzzy = false; + bool fuzzy_next = false; + // Parse the file line by line. // The msgstr is always the last line of an entry (i.e. msgid and msgctxt always // precede the corresponding msgstr). msgidBuf[0] = msgstrBuf[0] = msgctxtBuf[0] = '\0'; while (!feof(inFile) && fgets(line, 1024, inFile)) { + if (line[0] == '#' && line[1] == ',') { + // Handle message attributes. + if (strstr(line, "fuzzy")) { + fuzzy_next = true; + continue; + } + } // Skip empty and comment line if (*line == '\n' || *line == '#') continue; if (strncmp(line, "msgid", 5) == 0) { if (currentBuf == msgstrBuf) { // add previous entry - if (*msgstrBuf != '\0') { + if (*msgstrBuf != '\0' && !fuzzy) { messages.insert(msgidBuf); list->addMessageEntry(msgstrBuf, msgidBuf, msgctxtBuf); } msgidBuf[0] = msgstrBuf[0] = msgctxtBuf[0] = '\0'; + + // Reset the attribute flags. + fuzzy = fuzzy_next; + fuzzy_next = false; } strcpy(msgidBuf, stripLine(line)); currentBuf = msgidBuf; } else if (strncmp(line, "msgctxt", 7) == 0) { if (currentBuf == msgstrBuf) { // add previous entry - if (*msgstrBuf != '\0') { + if (*msgstrBuf != '\0' && !fuzzy) { messages.insert(msgidBuf); list->addMessageEntry(msgstrBuf, msgidBuf, msgctxtBuf); } msgidBuf[0] = msgstrBuf[0] = msgctxtBuf[0] = '\0'; + + // Reset the attribute flags + fuzzy = fuzzy_next; + fuzzy_next = false; } strcpy(msgctxtBuf, stripLine(line)); currentBuf = msgctxtBuf; @@ -313,7 +332,7 @@ PoMessageEntryList *parsePoFile(const char *file, PoMessageList& messages) { strcat(currentBuf, stripLine(line)); } } - + fclose(inFile); return list; } @@ -327,8 +346,8 @@ char *stripLine(char *line) { while (start < len && line[start++] != '"') {} // shift characters until we reach the end of the string or an unprotected quote int i = 0; - while (start+i < len && (line[start+i] != '"' || (i > 0 && line[start+i-1] == '\\'))) { - line[i] = line[start+i]; + while (start + i < len && (line[start + i] != '"' || (i > 0 && line[start + i - 1] == '\\'))) { + line[i] = line[start + i]; ++i; } line[i] = '\0'; diff --git a/tools/create_translations/po_parser.h b/tools/create_translations/po_parser.h index ac54a7fdae..6991b1d11e 100644 --- a/tools/create_translations/po_parser.h +++ b/tools/create_translations/po_parser.h @@ -34,7 +34,7 @@ public: void insert(const char *msg); int findIndex(const char *msg); - + int size() const; const char *operator[](int) const; @@ -69,9 +69,9 @@ struct PoMessageEntry { } } ~PoMessageEntry() { - delete [] msgstr; - delete [] msgid; - delete [] msgctxt; + delete[] msgstr; + delete[] msgid; + delete[] msgctxt; } }; @@ -80,11 +80,11 @@ struct PoMessageEntry { */ class PoMessageEntryList { public: - PoMessageEntryList(const char* language); + PoMessageEntryList(const char *language); ~PoMessageEntryList(); void addMessageEntry(const char *translation, const char *message, const char *context = NULL); - + const char *language() const; const char *languageName() const; const char *charset() const; -- cgit v1.2.3