Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt translation file (qm) info

Tags:

qt

translation

I have some number of qm files for my app. (pr_en.qm, pr_ru.qm). I can load they by

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

I want build dynamic menu (English, Russian) to select language. But, how can I extract such constants (English, Russian) from qm file instead of it's names (pr_en.qm, pr_ru.qm). Thanks.

like image 479
dream2work Avatar asked Feb 20 '23 12:02

dream2work


1 Answers

I would suggest two ways of doing it:

First would be declaring special translator field like:

tr("__LANGNAME__") that would be in every translation file filled with proper language name (even native). Then you could list all available translations, load them one by one and use QTranslator::translate(const char * context, const char * sourceText, const char * disambiguation = 0) method.

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;

My second aproach would be with QLocale and QLocale::Language. I would create QLocale object for each base name of file in qm dir, and then use QLocale::Language enum to get language name with QLocale::languageToString method.

like image 110
Kamil Klimek Avatar answered Mar 05 '23 06:03

Kamil Klimek