Language not updating on UI
-
I created a .ts file and then the .qm file. I loaded it in the following manner in the MainWindow class:
void MainWindow::onLanguageChanged() { hindiTranslator.load(":/lang/hindi.qm"); qApp->installTranslator(&hindiTranslator); }
Then, I used the changeEvent() in the MenuBar class:
void Menubar::changeEvent(QEvent* event) { if(event->type() == QEvent::LanguageChange) { ui->retranslateUi(this); for (auto it = mainMenuButtons.begin(); it != mainMenuButtons.end(); ++it) { MenuButton* button = it.value(); button->test(button->getText()); } for (auto &buttonList: subMenuButtons) { for (auto &button: *buttonList) { button->test(button->getText()); } } } QWidget::changeEvent(event); }
This is the test function in the MenuButton class:
void MenuButton::test(const QString &text) { textLabel = new QLabel(tr(text.toStdString().c_str())); }
Any suggestions about what I am doing wrong or why the language is not updating?
-
It was a stupid mistake!
I just had to override the changeEvent() in the MenuButton class, not the Menubar class. -
@shreya_agrawal said in Language not updating on UI:
button->test(button->getText())
Please check if the text returned is really the text you translated in your ts file. Normally you pass a constant string and not variable stuff - your approach can not work when you want to translate it a second time.
-
By second time, do you mean when I want to translate it back to English?