Qt translation file (qm) information

I have some qm files for my application. (pr_en.qm, pr_ru.qm). I can upload them using

translator.load(fileName, '.');
qApp->installTranslator(translator);

I want to create a dynamic menu (in English, Russian) to select a language. But how can I extract such constants (English, Russian) from the qm file instead of the names (pr_en.qm, pr_ru.qm). Thank.

+3
source share
1 answer

I would suggest two ways to do this:

First, a special translator field would be declared, for example:

tr("__LANGNAME__")That would be in every translation file filled with the correct language name (even native). Then you can list all available translations, download them one at a time and use the method QTranslator::translate(const char * context, const char * sourceText, const char * disambiguation = 0).

Example:

QStringList availableLanguages;
QDirIterator qmIt(pathToQm, QStringList() << "*.qm", QDir::Files);
while(qmIt.hasNext())
{
    qmIt.next();
    QFileInfo finfo = qmIt.fileInfo();
    QTranslator translator;
    translator.load(finfo.baseName(), pathToQm);
    availableLanguages << translator.translate("__LANGNAME__");
}

qDebug() << availableLanguages;

aproach QLocale QLocale:: Language. QLocale qm dir, QLocale:: Language enum QLocale::languageToString.

+7

All Articles