Mr490
Mr490

Reputation: 79

Qt MultiLanguage Application

Basically,I am trying to make a Multi-Language app in Qt. For that, I created hindi.ts file and hindi.qm file using lupdate and lrelease commands in qt 5.10.0 msvsc(2015) cmd terminal. I have a Language Settings Widget in which I have a combo box and based on the language selected, I load that value into a registry. As Shown in Below fig fig 1 is Structure of My program fig2 is UI of Language Settings

1. Block Diagram of Widgets in my Project

2. Language Gui Page

CODE:

void Form_LanguageSettings::on_pbtn_Submit_clicked()
{
    QSettings settings("HKEY_CURRENT_USER\\MyProject\\Employee", QSettings::NativeFormat);
    settings.setValue("language", ui->cmb_Language->currentText());

}

So, there are no problems while updating. However, when I want to retrieve it from the Registry into ListOfDepartment Widget, I retrieve it in this way :

QSettings settings("HKEY_CURRENT_USER\\MyProject\\Employee", QSettings::NativeFormat);

QApplication *app;
QString SelectedLanguage;
SelectedLanguage=settings.value("language").toString();
if(SelectedLanguage.toLower() != "english")
{
    if(SelectedLanguage=="hindi")
    {
        QTranslator translation;

        translation.load("C:/MyProject/LanguagePack/ObjectFiles/HindiDevanagari.qm");
        app->installTranslator(&translation);
    }
}

In this way I am unable to load the corresponding language. If I load a particular language manually in main.cpp file it loads but if I do it via the settings widgets from my app, it doesn't work. How can I deal with this when the language settings are to be done in a separate widget ? I am new to multilanguage of QT. Please, any help?

Upvotes: 1

Views: 675

Answers (2)

Fabio
Fabio

Reputation: 2602

Try to use:

QTranslator* translation = new QTranslator();
translation->load("C:/MyProject/LanguagePack/ObjectFiles/HindiDevanagari.qm");
QApplication::instance()->installTranslator(translation);
//or also QApplication::installTranslator(translation), it is static

In your code, the QTranslator instance is destroyed at the end of the if block, and the app variable is not assigned

Upvotes: 1

Yuriy Rusinov
Yuriy Rusinov

Reputation: 61

Possible you should load your translation using brief file name and QDir e.g.

QTranslator * translation =new QTranslator();
QString dir ("C:/MyProject/LanguagePack/ObjectFiles");
translation->load ("HindiDevanagari", dir);
app->installTranslator (translation);

Upvotes: 0

Related Questions