Getting the translated version of each Srting with QTranslator



  • 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.


  • Qt Champions 2017

    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 ?



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


  • Qt Champions 2017

    @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


  • Lifetime Qt Champion

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.