1. Implementing Multi-language Support in Qt
Here’s how to implement multi-language support in Qt:
QTranslator *trans = new QTranslator;
if (curLang == "us_EN") {
trans->load(QCoreApplication::applicationDirPath() + "/language/us_EN.qm");
} else if (curLang == "zh_CN") {
trans->load(QCoreApplication::applicationDirPath() + "/language/zh_CN.qm");
}
qApp->installTranslator(trans);
This code snippet should be placed before ui->setupUi(this); to ensure that the translation files are loaded before the UI is initialized.
2. Dynamic Multi-language Update for Sub-windows in Qt
To dynamically update the text content of sub-windows when switching languages, the retranslateUi() method must be called. This method, generated by Qt Designer, sets the translations for all text elements in the UI.
Detailed Steps to Call the retranslateUi() Method:
Detailed Steps to Call the retranslateUi() Method:
- 1. Ensure the Sub-window Has a
retranslateUi()Method If the sub-window is created using Qt Designer, theretranslateUi()method will automatically be included in theui_*.hfile. For example, if your sub-window class isChildWindow, it typically has a member object likeUi::ChildWindow, whereretranslateUi()is defined. - 2. Implement
changeEvent(QEvent *event)in the Sub-window Class Ensure the sub-window overrides thechangeEventmethod to detect theQEvent::LanguageChangeevent and callretranslateUi().
// ChildWindow.h
ifndef CHILDWINDOW_H
define CHILDWINDOW_H
include
namespace Ui {
class ChildWindow;
}
class ChildWindow : public QWidget
{
Q_OBJECT
public:
explicit ChildWindow(QWidget *parent = nullptr);
~ChildWindow();
protected:
void changeEvent(QEvent *event) override; // Override changeEvent
private:
Ui::ChildWindow *ui;
};
endif // CHILDWINDOW_H
- 3. Call
retranslateUi()in the Sub-window Implementation In thechangeEventmethod, detect the language change event and callretranslateUi().
// ChildWindow.cpp
#include "ChildWindow.h"
#include "ui_ChildWindow.h"
ChildWindow::ChildWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::ChildWindow)
{
ui->setupUi(this);
}
ChildWindow::~ChildWindow()
{
delete ui;
}
void ChildWindow::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange) {
ui->retranslateUi(this); // Update the UI text
}
QWidget::changeEvent(event); // Call the base class changeEvent
}
- 4. Install the Translator and Trigger Events in the Main Window Ensure that when switching languages, the translator is correctly installed, and
changeEventis triggered for both the main and sub-windows.
// MainWindow.cpp
#include "MainWindow.h"
#include "ChildWindow.h"
#include <QTranslator>
#include <QApplication>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
childWindow = new ChildWindow(this); // Create sub-window
}
void MainWindow::switchLanguage(const QString &languageFile)
{
QTranslator translator;
if (translator.load(languageFile)) {
qApp->installTranslator(&translator); // Install the translator
QEvent event(QEvent::LanguageChange);
QApplication::sendEvent(this, &event); // Trigger language change in main window
QApplication::sendEvent(childWindow, &event); // Trigger language change in sub-window
}
}
Summary:
- Sub-windows need to override
changeEventand callretranslateUi()whenQEvent::LanguageChangeoccurs. - Ensure that the language change event is correctly sent to all relevant windows, including sub-windows, when switching languages.
- This approach ensures that both the main and sub-windows dynamically update their text content during language changes.
3. Refreshing Newly Added tr() in Qt Creator and Qt Linguist
The tr() function in Qt is used for string localization. When new or modified content is added within tr(), you need to refresh Qt Linguist to display the latest translations. Here are the steps:
- Update Translation Files (.ts):
- Right-click the
.profile in the project tree and select “Run qmake” or choose “Run qmake” from the “Build” menu. - This updates the project files and detects new
tr()calls.
- Right-click the
- Generate or Update
.tsFiles:- Go to “Tools” → “External” → “Linguist” → “Update Translations” in Qt Creator. This automatically runs the
lupdatecommand, updating the.tsfiles with the latesttr()strings.
- Go to “Tools” → “External” → “Linguist” → “Update Translations” in Qt Creator. This automatically runs the
- Refresh Qt Linguist:
- Open the relevant
.tsfile to check for newtr()strings. If the new strings are not visible, try rerunninglupdateor manually editing the.tsfile.
- Open the relevant
- Edit and Save Translations:
- Use Qt Linguist to edit the translations in the
.tsfile and save your changes.
- Use Qt Linguist to edit the translations in the
- Regenerate
.qmFiles:- In Qt Creator, go to “External” → “Linguist” → “Compile Translations” to regenerate the
.qmfiles for use by your application.
- In Qt Creator, go to “External” → “Linguist” → “Compile Translations” to regenerate the
After completing these steps, the new content within tr() will be refreshed and displayed correctly in Qt Linguist.