How can I programmatically translate a QTranslator-translated string back to english?
-
I'm working on adding translations to a Qt desktop app that communicates with a remote backend. Up until now they only worked in english.
I need to add 2 languages. I got translations working using QTranslator and qApp->installTranslator(translator).
The app has several configuration windows, some created in Qt Designer, some programmatically, for example comboboxes and such. Now my problem is that the backend expects the configuration settings to be sent in english. So for example, if there's a config combobox with the choices ["Red", "Blue"], the spanish-translated app would show ["Rojo", "Azul"], but if the user selects the 2nd choice and presses OK, I need to send "choice=Blue" over the network.
I can't figure out a general solution to this problem that doesn't require the code to be translation-aware.
i.e. I would like to do the following:
QString translatedChoice = combobox->currentText(); QString untranslatedChoice = ???(translatedChoice); networkClient.send(untranslatedChoice); -
Hi,
That's typically a case where you separate the data and its representation. In the case of the combox you can use a user role in the model to set the value you want to send over the wire and the default is the translatable version that will be shown to the user.
-
I'm working on adding translations to a Qt desktop app that communicates with a remote backend. Up until now they only worked in english.
I need to add 2 languages. I got translations working using QTranslator and qApp->installTranslator(translator).
The app has several configuration windows, some created in Qt Designer, some programmatically, for example comboboxes and such. Now my problem is that the backend expects the configuration settings to be sent in english. So for example, if there's a config combobox with the choices ["Red", "Blue"], the spanish-translated app would show ["Rojo", "Azul"], but if the user selects the 2nd choice and presses OK, I need to send "choice=Blue" over the network.
I can't figure out a general solution to this problem that doesn't require the code to be translation-aware.
i.e. I would like to do the following:
QString translatedChoice = combobox->currentText(); QString untranslatedChoice = ???(translatedChoice); networkClient.send(untranslatedChoice);@thierryhenry14 thats generally speaking not possible, you would need a 2nd translation file with Spanish as source and English as target etc. that is inviting tons of work an bugs/translation errors.
Maybe you can hack/work your way around it.
How is the combobox populated?
-
I'm working on adding translations to a Qt desktop app that communicates with a remote backend. Up until now they only worked in english.
I need to add 2 languages. I got translations working using QTranslator and qApp->installTranslator(translator).
The app has several configuration windows, some created in Qt Designer, some programmatically, for example comboboxes and such. Now my problem is that the backend expects the configuration settings to be sent in english. So for example, if there's a config combobox with the choices ["Red", "Blue"], the spanish-translated app would show ["Rojo", "Azul"], but if the user selects the 2nd choice and presses OK, I need to send "choice=Blue" over the network.
I can't figure out a general solution to this problem that doesn't require the code to be translation-aware.
i.e. I would like to do the following:
QString translatedChoice = combobox->currentText(); QString untranslatedChoice = ???(translatedChoice); networkClient.send(untranslatedChoice);@thierryhenry14 said in How can I programmatically translate a QTranslator-translated string back to english?:
Now my problem is that the backend expects the configuration settings to be sent in english. So for example, if there's a config combobox with the choices ["Red", "Blue"], the spanish-translated app would show ["Rojo", "Azul"], but if the user selects the 2nd choice and presses OK, I need to send "choice=Blue" over the network.
With the proviso that I have not used
QTranslator/tr("..."). This specific case is easy. Every library has a way of setting a combobox/dropdown item to have a display string but also a "value", which (if absent) will default to the display string, but can be set separately. ForQComboBoxthe value is aQVariantand can be set in a variety of ways includingaddItem(const QString &text, const QVariant &userData = QVariant())andQComboBox::setItemData(int index, const QVariant &value, int role = Qt::UserRole)and retrieved viaQVariant QComboBox::itemData(int index, int role = Qt::UserRole) const, or you can do same via theQAbstractItemModel *QComboBox::model() const. Since you know you always need the English word from a Spanish choice you should store the original English word as the value while allowing the translation for the text. This is what @SGaist was referring to in his reply above.