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. QSettings and localized key
QtWS25 Last Chance

QSettings and localized key

Scheduled Pinned Locked Moved Solved General and Desktop
qsettings
8 Posts 3 Posters 815 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.
  • push_backP Offline
    push_backP Offline
    push_back
    wrote on last edited by
    #1

    Hello everyone!
    How to read localized key?

    For example here is the ini file app.desktop:

    app.desktop:

    [Destop Entry]
    name=AppName
    name[fr]=NomApp
    

    To read the name key, one can do that:

    QSettings setting(app.desktop)
    qDebug() << setting.value("name");
    

    But how to read the name in the localized field [fr] ?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      AFAIK, this is usually a file that is used by the desktop environment rather than your application directly. You should rather do the translation as usual within your application.

      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
      • push_backP Offline
        push_backP Offline
        push_back
        wrote on last edited by
        #3

        Thank you for your quick reply!
        I need to do that because I'm writing a program to allow user create desktop shortcut:

        screenshot

        I want to get the application's name and description from user's locale to be displayed.

        Github

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          @push_back said in QSettings and localized key:

          How to read localized key?

          Same as any other:

          #include <QCoreApplication>
          #include <QSettings>
          #include <QDebug>
          
          int main(int argc, char **argv) {
                  QCoreApplication app (argc, argv);
                  QSettings setting("/tmp/test/test.ini", QSettings::NativeFormat);
                  qDebug() << setting.value("Desktop Entry/name");
                  qDebug() << setting.value("Desktop Entry/name[fr]");
                  return 0;
          }
          

          Then the question will become, how do I determine the locale code, e.g. "fr"?

          push_backP 1 Reply Last reply
          0
          • C ChrisW67

            @push_back said in QSettings and localized key:

            How to read localized key?

            Same as any other:

            #include <QCoreApplication>
            #include <QSettings>
            #include <QDebug>
            
            int main(int argc, char **argv) {
                    QCoreApplication app (argc, argv);
                    QSettings setting("/tmp/test/test.ini", QSettings::NativeFormat);
                    qDebug() << setting.value("Desktop Entry/name");
                    qDebug() << setting.value("Desktop Entry/name[fr]");
                    return 0;
            }
            

            Then the question will become, how do I determine the locale code, e.g. "fr"?

            push_backP Offline
            push_backP Offline
            push_back
            wrote on last edited by
            #5

            @ChrisW67 Thank you, I found that too. I will use that!
            Yes I need a way to find the locale (I think I have to look in QLocale).
            I also have to deal with encoding, for example if the key name[fr] is "Système".

            1 Reply Last reply
            0
            • push_backP Offline
              push_backP Offline
              push_back
              wrote on last edited by
              #6

              I'm stuck with encoding issue:

              Let be the file test.ini encoded with UTF-8

              [Destop Entry]
              Name=System
              Name[fr]=Système
              

              I use the code from @ChrisW67 :

              #include <QCoreApplication>
              #include <QSettings>
              #include <QDebug>
              
              int main(int argc, char **argv) {
                      QCoreApplication app (argc, argv);
                      QSettings setting("/tmp/test/test.ini", QSettings::NativeFormat);
                      qDebug() << setting.value("Desktop Entry/Name");
                      qDebug() << setting.value("Desktop Entry/Name[fr]");
                      return 0;
              }
              

              "Système" is turned into "système". So it means that the string was encoded with UTF-8 (in the file test.ini) then decoded with latin1 (by the value method of QSettings). Is that correct? If yes why it behaves like this?
              Note: my system uses UTF-8. I tried to use QSettings::IniFormat.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #7

                In Qt 6.3, 'In line with most implementations today, QSettings will assume the INI file is utf-8 encoded. This means that keys and values will be decoded as utf-8 encoded entries and written back as utf-8.'
                Also, 'Please note that this behavior is different to how QSettings behaved in versions of Qt prior to Qt 6. INI files written with Qt 5 or earlier are however fully readable by a Qt 6 based application (unless a ini codec different from utf8 had been set). But INI files written with Qt 6 will only be readable by older Qt versions if you set the "iniCodec" to a utf-8 textcodec." '

                If you are using Qt5 then see QSettings::setIniCodec

                push_backP 1 Reply Last reply
                3
                • C ChrisW67

                  In Qt 6.3, 'In line with most implementations today, QSettings will assume the INI file is utf-8 encoded. This means that keys and values will be decoded as utf-8 encoded entries and written back as utf-8.'
                  Also, 'Please note that this behavior is different to how QSettings behaved in versions of Qt prior to Qt 6. INI files written with Qt 5 or earlier are however fully readable by a Qt 6 based application (unless a ini codec different from utf8 had been set). But INI files written with Qt 6 will only be readable by older Qt versions if you set the "iniCodec" to a utf-8 textcodec." '

                  If you are using Qt5 then see QSettings::setIniCodec

                  push_backP Offline
                  push_backP Offline
                  push_back
                  wrote on last edited by
                  #8

                  @ChrisW67 That was exactly what I was looking for!
                  I am indeed using Qt5, and did not notice that there is QSettings::setIniCodec.
                  Thanks a lot!

                  1 Reply Last reply
                  0

                  • Login

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