I have a bilingual program in English and Arabic, in files called app_en and app_ar. I can translate the program at the beginning to main using installTranslator in QApplication. My question is: how can I change the program language by pressing QAction?
Here is my attempt:
I have my own QAction language connected to a slot that selects the user's language, then saves it and switches to another function to set the translation. All this happens outside of QMainWindow.
void MainCore::GetAndSaveLanguage(bool){
bool OKPressed;
QString Language = QInputDialog::getItem(NULL, InputDialogString, InputDialogString + ":", Languages, 0, false, &OKPressed)
.remove(QRegExp("*(", Qt::CaseSensitive, QRegExp::Wildcard)).remove(')');
if(OKPressed){
Settings->beginGroup("Settings");
Settings->setValue("Language", Language);
Settings->endGroup();
UpdateTranslations(Language);
}
}
void MainCore::UpdateTranslations(QString Language){
QTranslator QtTranslator;
QtTranslator.load("qt_" + Language, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
QApplication::instance()->installTranslator(&QtTranslator);
QTranslator AppTranslator;
AppTranslator.load("app_" + Language, ":/translations");
QApplication::instance()->installTranslator(&AppTranslator);
}
I also have a QMainWindow function that sets the entire screen text as follows:
void Window::SetText(){
Menu->setTitle(tr("File"));
...
}
This is called when building windows and in the changeEvent function:
void Window::changeEvent(QEvent *event){
if(event->type() == QEvent::LanguageChange){
SetText();
}else{
QWidget::changeEvent(event);
}
}
source
share