Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I programmatically translate a QTranslator-translated string back to english?
Forum Updated to NodeBB v4.3 + New Features

How can I programmatically translate a QTranslator-translated string back to english?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 364 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thierryhenry14
    wrote on last edited by
    #1

    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);
    
    J.HilkJ JonBJ 2 Replies Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • T thierryhenry14

        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);
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @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?


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • T thierryhenry14

          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);
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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. For QComboBox the value is a QVariant and can be set in a variety of ways including addItem(const QString &text, const QVariant &userData = QVariant()) and QComboBox::setItemData(int index, const QVariant &value, int role = Qt::UserRole) and retrieved via QVariant QComboBox::itemData(int index, int role = Qt::UserRole) const, or you can do same via the QAbstractItemModel *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.

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved