Changing languages dynamically
Sometimes, people want to use languages other than the one specified by the system locale. This is a matter of application of the customized settings. This usually means restarting the application in order to load the corresponding translation file. This is partly because changing the language dynamically requires additional work. However, it's feasible and can be done with some lines. What's more important is that it delivers a better user experience!
Let's add a new push
button to MainWindow
. Name it loadButton
and change its text to Load/Unload Translation
. Then, edit the main.cpp
file in the Edit mode. Remove all QTranslator
related lines, as we'll be implementing this dynamic language switch in the MainWindow
class. The main.cpp
file should look like the originally generated one as follows:
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(...