How to set file name according to current system language in windows
-
Hiii guys
I'm creating the file called Users.txt in Windows using Qt Application. When ever language settings are changed I would like display the name of the file in appropriate languages.
e.g English file name Users.txt
German File name Benutzer.txts
there a way to acheive this in Qt ? -
Hi,
See the Internationalization with Qt chapter in Qt's documentation.
-
Hi,
the way I see it you can achieve the functionality by doing one of the below:- create manual mappings (QMap or smth) where the key is lang (QString) (i.e. en_US, en_GB, de_DE etc.) and the value for given key is (QString) file name (user, user, benutzer, etc). This way you can automate this, if I understood your goal properly.
- other way is to rename your files to include lang value, either by prepending/adding to the file name (en_EN_user, en_GN_user, de_DE_benutzer - or user_en_EN, etc.) or by . notation (user.en_EN.txt, user.en_GB.txt, etc) so you can easily extract locale form file name using QString::split or QRegularExpression.
Edit: the second way with regular expressions is actual use case in one of my tools but it's designed to handle hundreds of files so might be a bit of overshot for you.
-
QFile f(pathToFile + tr("Users")+ QString(".txt"));
this way
Users
should change accordingly to the QTanslator installed.
Edit:
@Santhosh-AyanoorI would like display the name of the file in appropriate languages
please clarify, do you want to change the file name according to the language settings or do you want to display it (for example) on a QLabel as text, but the actual file name is always the same?
-
than QLocale is what you're looking for:
https://doc.qt.io/qt-5/qlocale.html#systemexample:
int main(int argc, char *argv[]) { QApplication a(argc, argv); QLocale currentLocale = QLocale::system(); qDebug() << "FileName should be:" << (currentLocale.language() == QLocale::German ? "Benutzer.txt" : "Users.txt"); return a.exec(); }
-
@J-Hilk Thanks for you answer. Your code will help me to create the file based on the current locale at start of my app.
Now if the user changes the locale of the system, how to change the file name from German to English or vice-versa ?
-
@Santhosh-Ayanoor
[Assuming you really do mean "change the filename on disk".] So you'll have to trap the system locale language change, and rename all the file(s) from their old language name to the new one. But: user may not change system locale while you app is running, he may change it when it isn't. So you'll want to do something like also check for/do this renaming every time your app starts up as well. But at that point, you won't know what the locale was the last time you ran, so you won't even know what the filename(s) were previously saved as so you can rename them now, so goodness knows how you think you're going to deal with that [you'll have to save somewhere what the locale/filename(s) was previously]....This file-rename-per-language all sounds hokey to me. Are you sure you really want to do this?!
-
Just in case, Windows might show you some translated folder name but if you take a look at what is actually on disk, it's in English.