QLocale Get real language name

1k Views Asked by At

I have a language code like fr_fr, fr_be. I would like to get French and Belgium using QLocale, but I can't find how to do it. I did:

QLocale locale("fr_fr"); // or fr_be
QString l = locale.languageToString(locale.language()); //returns French in both cases
2

There are 2 best solutions below

0
On BEST ANSWER

QLocale gives you the country and language names, in both native and English languages. Choose what you prefer:

#include <QCoreApplication>
#include <QLocale>
#include <QDebug>

void displayNames(QLocale& locale)
{
    qDebug() << locale.nativeLanguageName() << locale.nativeCountryName();
    qDebug() << locale.languageToString(locale.language()) << locale.countryToString(locale.country());
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qDebug() << "ca_ES";
    QLocale cat = QLocale("ca_ES");
    displayNames(cat);

    qDebug() << "es_ES";
    QLocale esp = QLocale("es_ES");
    displayNames(esp);
}

This program returns:

ca_ES
"català" "Espanya"
"Catalan" "Spain"
es_ES
"español de España" "España"
"Spanish" "Spain"
2
On

Your are querying the language name, that is French in both cases. Maybe you want to get the country name like this:

QLocale locale("fr_be");
QString l = locale.countryToString(locale.country());

Read here for more information.