qtlocalizationtranslationqlocale

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

Solution

  • 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"