SteveTJS
SteveTJS

Reputation: 705

QLocale Get real language name

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

Upvotes: 0

Views: 1293

Answers (2)

Former contributor
Former contributor

Reputation: 2576

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"

Upvotes: 1

Gabriella Giordano
Gabriella Giordano

Reputation: 1238

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.

Upvotes: 4

Related Questions