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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With