您需要保持字符串常量未翻译。但是用QT_TR_NOOP() 标记它,以便语言学家可以看到它,并将其存储为普通的char const*。
然后当你开始实际使用它时,像往常一样翻译它,用你班级的QApplication::translate,或tr():
// MyClass: Hello! -> Dobrý deň
static const uchar translations[] = {
60, 184, 100, 24, 202, 239, 156, 149, 205, 33, 28, 191, 96, 161, 189, 221,
66, 0, 0, 0, 8, 4, 236, 51, 17, 0, 0, 0, 0, 105, 0, 0,
0, 52, 3, 0, 0, 0, 18, 0, 68, 0, 111, 0, 98, 0, 114, 0,
253, 0, 32, 0, 100, 0, 101, 1, 72, 8, 0, 0, 0, 0, 6, 0,
0, 0, 6, 72, 101, 108, 108, 111, 33, 7, 0, 0, 0, 7, 77, 121,
67, 108, 97, 115, 115, 1, 47, 0, 0, 1, 58, 0, 151, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 77, 121,
67, 108, 97, 115, 115, 136, 0, 0, 0, 6, 1, 1, 255, 4, 2, 4,
};
#include <QCoreApplication>
#include <QDebug>
#include <QString>
#include <QTranslator>
int main(int argc, char **argv)
{
static const char *const message = QT_TR_NOOP("Hello!");
QCoreApplication app{argc, argv};
QTranslator translator;
if (!translator.load(translations, sizeof translations))
qFatal("Failed to load translations");
app.installTranslator(&translator);
qInfo() << app.translate("MyClass", message);
// or tr(message) in your own MyClass object
}