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.3k 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.
  • GilboonetG Offline
    GilboonetG Offline
    Gilboonet
    wrote on 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) ?

    mzimmersM M 2 Replies Last reply
    0
    • GilboonetG Offline
      GilboonetG Offline
      Gilboonet
      wrote on 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
      • GilboonetG Gilboonet

        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) ?

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on 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.

        GilboonetG 1 Reply Last reply
        0
        • mzimmersM mzimmers

          @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.

          GilboonetG Offline
          GilboonetG Offline
          Gilboonet
          wrote on 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

          SGaistS 1 Reply Last reply
          0
          • GilboonetG Gilboonet

            @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

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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

              GilboonetG 1 Reply Last reply
              1
              • GilboonetG Gilboonet

                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 Offline
                M Offline
                mpergand
                wrote on 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
                • SGaistS SGaist

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

                  GilboonetG Offline
                  GilboonetG Offline
                  Gilboonet
                  wrote on 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
                  • GilboonetG Offline
                    GilboonetG Offline
                    Gilboonet
                    wrote on 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
                    • GilboonetG Gilboonet has marked this topic as solved on
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 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

                      • Login

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