QSettings and localized key
-
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] ?
-
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.
-
@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"?
-
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. -
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