Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt C++ How to get locale language ?
QtWS25 Last Chance

Qt C++ How to get locale language ?

Scheduled Pinned Locked Moved Solved General and Desktop
qt c++qlocale
9 Posts 4 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gilboonet
    wrote on 27 Jun 2023, 18:42 last edited by Gilboonet
    #1

    Hello, my C++ application works but it's only in my language which is french. I'm trying to make it appear in English when user's locale is not french. I used QT Linguist and create two .tr files, one for french and another one for english, then I created the .qm files and now I'm trying to use those .qm files into my main function that is now :

    #include "mainwindow.h"
    
    #include <QApplication>
    #include <QTranslator>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QTranslator translator;
        QLocale locale = QLocale();
        QString lang = QLocale::languageToString(locale.language());
        qInfo() << locale;
        if (translator.load(QLocale(), lang == "French" ? "UI1_fr.qm" : "UI1_en.qm"))
            QApplication::installTranslator(&translator);
    
        MainWindow w;
        w.show();
        return a.exec();
    }
    

    To test I changed my system language to English but my code always shows "French" as language string. Maybe

    QLocale locale = QLocale();
    

    is not the good way to retrieve the system locale ? Or maybe do I need to restart the system (I'm on Ubuntu 22.04 and will also test on Windows 11) ?

    M M 2 Replies Last reply 27 Jun 2023, 18:57
    0
  • G Offline
    G Offline
    Gilboonet
    wrote on 27 Jun 2023, 19:47 last edited by
    #8

    Well, with your help I was able to make it work. I needed to replace
    ":/Translations"
    by
    "Translations"

    I don't know the meaning of the ":/" at the beginning of the string, (maybe does it mean "from the current directory" ?) but without it it worked.

    here's my working main()

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QTranslator translator;
    
        // fichier de la forme ":/Translations/App_xx.qm"
        QString tr_folder="Translations";
        QString tr_file="UI1_"+QLocale::system().name();
    
        if(translator.load(tr_file,tr_folder))
            QApplication::installTranslator(&translator);
        else
        {	// pas trouvé
         qCritical("\"%s\" file not Found. No translation will occur.",qPrintable(tr_folder+"/"+tr_file));
        }
    
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    
    1 Reply Last reply
    0
  • M Offline
    M Offline
    mzimmers
    replied to Gilboonet on 27 Jun 2023, 18:57 last edited by
    #2

    @Gilboonet well, what is your system's locale? And, if you want to change that for your application, you can use the set default method.

    G 1 Reply Last reply 27 Jun 2023, 19:06
    0
  • G Offline
    G Offline
    Gilboonet
    replied to mzimmers on 27 Jun 2023, 19:06 last edited by Gilboonet
    #3

    @mzimmers I'm not willing to change my locale at runtime, only read what is the default locale and if it's french load the french .qm otherwise load the english .qm

    @mzimmers I just reboot and after that, my system's locale (which I changed from French to English) was correctly retrieved. But my Application is still in french, so I supposed I did something wrong with QT Linguist.

    Here's what I did with QT Linguist :

    • edit each string (mainly menus entries) and translate them
    • save the .tr file

    And from the C++ project, I used Tools/Extern/Linguist/lrelease

    S 1 Reply Last reply 27 Jun 2023, 19:12
    0
  • S Offline
    S Offline
    SGaist Lifetime Qt Champion
    replied to Gilboonet on 27 Jun 2023, 19:12 last edited by
    #4

    Hi,

    Just in case, you don't do anything if the translator load method fails. You should at least print that something went wrong.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
  • S Offline
    S Offline
    SGaist Lifetime Qt Champion
    wrote on 27 Jun 2023, 19:16 last edited by
    #5

    On a side note, you are not building the file path correctly. See the load method you are using documentation .

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    G 1 Reply Last reply 27 Jun 2023, 19:22
    1
  • M Offline
    M Offline
    mpergand
    replied to Gilboonet on 27 Jun 2023, 19:21 last edited by mpergand
    #6

    Hi @Gilboonet

    Here the code that I'm using:

    // fichier de la forme ":/Translations/App_xx.qm"
    QString tr_folder=":/Translations";
    QString tr_file="App_"+QLocale::system().name();
    	translator=new QTranslator(this);
    
    if(translator->load(tr_file,tr_folder))
        installTranslator(translator);
    else
    {	// pas trouvé
     qCritical("\"%s\" file not Found. No translation will occur.",qPrintable(tr_folder+"/"+tr_file));
    }
    
    1 Reply Last reply
    1
  • G Offline
    G Offline
    Gilboonet
    replied to SGaist on 27 Jun 2023, 19:22 last edited by
    #7

    @SGaist You are right, the load method fails and it's because I didn't follow the rules that are stated on the documentation, I was thinking it only needs a file name.

    1 Reply Last reply
    0
  • G Offline
    G Offline
    Gilboonet
    wrote on 27 Jun 2023, 19:47 last edited by
    #8

    Well, with your help I was able to make it work. I needed to replace
    ":/Translations"
    by
    "Translations"

    I don't know the meaning of the ":/" at the beginning of the string, (maybe does it mean "from the current directory" ?) but without it it worked.

    here's my working main()

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QTranslator translator;
    
        // fichier de la forme ":/Translations/App_xx.qm"
        QString tr_folder="Translations";
        QString tr_file="UI1_"+QLocale::system().name();
    
        if(translator.load(tr_file,tr_folder))
            QApplication::installTranslator(&translator);
        else
        {	// pas trouvé
         qCritical("\"%s\" file not Found. No translation will occur.",qPrintable(tr_folder+"/"+tr_file));
        }
    
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    
    1 Reply Last reply
    0
  • G Gilboonet has marked this topic as solved on 27 Jun 2023, 19:47
  • S Offline
    S Offline
    SGaist Lifetime Qt Champion
    wrote on 27 Jun 2023, 20:39 last edited by
    #9

    The ":/" is the root of Qt's resource system path.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    2

8/9

27 Jun 2023, 19:47

topic:navigator.unread, 1
  • Login

  • Login or register to search.
8 out of 9
  • First post
    8/9
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved