From fc05e8a4ff8cc1f67d1ee1690bb6ef3ed8fd9a83 Mon Sep 17 00:00:00 2001 From: Thierry Crozat Date: Mon, 30 Aug 2010 22:10:32 +0000 Subject: I18N: Modify create-translations tool to remove duplicate translations The TranslationManager in ScummVM will pick up the translation associated to no context if present and if a translation could not be found for a specific context. Based on this, the create_translations tool will now remove the translation associated to a specific context if the same message has the same translation associated to no context. This generate a smaller translation.dat file, and this should also slightly improve performances (less strings to load from the file and smaller list in which to look for a translated message). svn-id: r52459 --- tools/create_translations/po_parser.cpp | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'tools/create_translations') diff --git a/tools/create_translations/po_parser.cpp b/tools/create_translations/po_parser.cpp index 84de73355f..3d8e2547a0 100644 --- a/tools/create_translations/po_parser.cpp +++ b/tools/create_translations/po_parser.cpp @@ -175,6 +175,22 @@ void PoMessageEntryList::addMessageEntry(const char *translation, const char *me } // We now have rightIndex = leftIndex - 1 and we need to insert the new message // between the two (i.a. at leftIndex). + // However since the TranslationManager will pick the translation associated to no + // context if it is not present for a specific context, we can optimize the file + // size, memory used at run-time and performances (less strings to read from the file + // and less strings to look for) by avoiding duplicate. + if (context != NULL && *context != '\0') { + // Check if we have the same translation for no context + int contextIndex = leftIndex - 1; + while (contextIndex >= 0 && strcmp (message, _list[contextIndex]->msgid) == 0) { + --contextIndex; + } + ++contextIndex; + 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]; @@ -190,6 +206,29 @@ 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; + int removed = 0; + while (contextIndex < _size && strcmp(message, _list[contextIndex]->msgid) == 0) { + if (strcmp(translation, _list[contextIndex]->msgstr) == 0) { + delete _list[contextIndex]; + ++removed; + } else { + _list[contextIndex - removed] = _list[contextIndex]; + } + ++contextIndex; + } + if (removed > 0) { + while (contextIndex < _size) { + _list[contextIndex - removed] = _list[contextIndex]; + ++contextIndex; + } + } + _size -= removed; + } + } const char *PoMessageEntryList::language() const { -- cgit v1.2.3