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. Getting the translated version of each Srting with QTranslator
Forum Updated to NodeBB v4.3 + New Features

Getting the translated version of each Srting with QTranslator

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 896 Views 2 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.
  • E Offline
    E Offline
    El3ctroGh0st
    wrote on last edited by
    #1

    Hello guys,

    I am facing a problem with QTTranslator right now. I have two languages added, and I am using QSettings to make sure that the language will be kept even if the application is closed. This is my main.cpp:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QTranslator trans;
        QSettings setting("textFinder","tFSettings");
        QString language;
    
        setting.beginGroup("textFinder");
        language = setting.value("language", "English").toString();
    
        if(language == "German")
            trans.load(":/german.qm");
        else if(language == "Italian")
            trans.load(":/italian.qm");
    
        if(language != "English")
            a.installTranslator(&trans);
    
        textFinder w;
        w.show();
    
        return a.exec();
    }
    

    So far so good. It works perfectly fine. However, there is a problem: I have a QDialog window (called Settings) that can be called from the MainWindow. And since I want the settings to be "kept" on program restart (meaning that for example, if I change the value of a comboBox, it will keep that index on restart. Here a picture of the settingsWindow:
    Basically, lets say I change the language to German, it is supposed to automatically have "German" as the first index. The way I've done it is basically to search for the most recently selected language, find the index of where this text appears, and then set the curent index to that value. Here the implementation:

    void Settings::changeValues()
    {
        int index = ui->colourBox->findText(getColourName(colour));
        ui->colourBox->setCurrentIndex(index);
    
        index = ui->languageBox->findText(language);
        ui->languageBox->setCurrentIndex(index);
    }
    

    For English, this works. However, when I'm changing the language, naturally it won't find any matches since the strings are different in other languages. My question now is if there's a way to retrieve the translated version of those strings, in order to prevent this from happening. Of course, if someone else has a more effective approach that would work better (because I'm sure there is a better way to do it), I'd like to know of it, too! I hope someone can helpt me out.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Its possible to load another translation with english and ask it for the translation without make it the current language.
      However, im thinking if its not possible to store the index of the selected item and and not the actual text ?

      E 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        Its possible to load another translation with english and ask it for the translation without make it the current language.
        However, im thinking if its not possible to store the index of the selected item and and not the actual text ?

        E Offline
        E Offline
        El3ctroGh0st
        wrote on last edited by
        #3

        @mrjj Hm... Now that I think of it, that should be doable... Thank you, I'll try it out!

        mrjjM 1 Reply Last reply
        0
        • E El3ctroGh0st

          @mrjj Hm... Now that I think of it, that should be doable... Thank you, I'll try it out!

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @El3ctroGh0st
          Ok :)
          IF it works out ok, make sure to make a note in Docs that list should not be sorted as that would
          most likely rearrange the order and and saved index would be bongus

          1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            You can also use a custom model for your combo box which contains the text and an ID.

            That way even if the text changes you can store the ID. Then when you load your application you'll use the idea to select the correct colour.

            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
            1

            • Login

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