subsurface/gettextfromc.cpp
Dirk Hohndel 2e43769108 Avoid the memory leaks from translations
Instead use a hash to cache the translations (and allow for the ability to
clear the hash so we can even switch translations at runtime...).

Now Qt will keep track of the memory and release it for us when we are
done with it.

This avoids the memory leak introduced in commit 4ecb35bf5ff2 ("Make a
copy of the translated text").

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-10-08 23:11:28 -07:00

27 lines
530 B
C++

#include <QCoreApplication>
#include <QString>
#include <gettextfromc.h>
const char *gettextFromC::gettext(const char *text)
{
QByteArray &result = translationCache[text];
if (result.isEmpty())
result = tr(text).toUtf8();
return result.constData();
}
void gettextFromC::reset(void)
{
translationCache.clear();
}
gettextFromC* gettextFromC::instance()
{
static gettextFromC *self = new gettextFromC();
return self;
}
extern "C" const char *gettext(const char *text)
{
return gettextFromC::instance()->gettext(text);
}