1

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?

p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
Mr490
  • 79
  • 1
  • 5

2 Answers2

1

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

Fabio
  • 2,522
  • 1
  • 15
  • 25
  • i tried doing the same way but the language is not changed it still satys in general english. Is it Mandatory to use QApplication Object . Is there any other way to load directly the translation File.? – Mr490 Mar 20 '18 at 17:10
  • In your code, the app variable is not assigned. You have to create a QApplication instance in the main. You can access to the QApplication global instance using QApplication::instance(). However QApplication::installTranslator() is static, you can use it directy. I will edit my answer – Fabio Mar 21 '18 at 07:45
0

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);
Yuriy Rusinov
  • 61
  • 2
  • 5
  • Mr Rusinov i tried with your concept using dir but by that the language pack is not loading at all. that is the language change is not applied . Is there any other alternative for loading the .qm file Without using QApplication Object. ? i.e app in above example – Mr490 Mar 21 '18 at 06:53
  • I have to ask, do you try to debug result of load function. Is it true or false, further answers depends on this. – Yuriy Rusinov Mar 21 '18 at 07:23