Reputation: 1330
I have a difficult problem and tried too many things but in vain. I have 4 translation files for 4 different languages, and whenever the user changes the language, I need to re-translate the UI.
Fixed text can be re-translated as follows:
ui->BusinessNameHelpText->setText(tr("Enter the business name."));
However, the variable text like the action on a button can't be done this way, because it's either “Install” or “Update” for example.
I tried this code block:
QString action = ui->actionButton->text();
ui->retranslateUi(this);
ui->actionButton->setText(trUtf8(action.toUtf8().data()));
And this:
QString action = ui->actionButton->text();
ui->retranslateUi(this);
ui->actionButton->setText(tr(action.toUtf8().data()));
And this:
QString action = ui->actionButton->text();
ui->retranslateUi(this);
ui->actionButton->setText(tr(action.toStdString.c_str()));
This works for only the first time when I change language, but after that, it doesn't work.
Upvotes: 0
Views: 1874
Reputation: 61
It depends on what you mean by variable string. For example, if strings come from an external file, which can all be listed, you can use the following answer. How to translate strings from external files
Use the QT_TRANSLATE_NOOP()
macro to declare to lupdate
those strings are translatable and should appear in .ts file.
Upvotes: 0
Reputation: 62684
It sounds like you have code like
ui->actionButton->setText(isInstall ? tr("Install") : tr("Update"));
in your program. You need to run that again, in response to the language change event.
Upvotes: 1
Reputation: 37697
Ok now I get it what is actually your problem.
Problem is how you treat change text of button. It works only when you change English to other langue. It happens since English text is equivalent to text you have used in code as identifies of translation.
When your application calls this:
QString action = ui->actionButton->text();
if previously English was selected action
will contain text which is same as identifier of translation.
if previously other langue was selected action
will contain something else then identifier and translator is unable to find matching translation. In such case translator will return original value.
To fix just leave only ui->retranslateUi(this);
and thrash other code and make sure your UI has all respective strings marked that they need translation.
Upvotes: 0