#ifndef AAHARLANGUAGES_H
#define AAHARLANGUAGES_H
#include<QLocale>
#include<QObject>
class AaharLanguages : public QObject
{
Q_OBJECT
public:
AaharLanguages() {}
enum AaharLangs{
Hindi = QLocale::Hindi,
Gujrati = QLocale::Gujarati,
English = QLocale::English,
Marathi = QLocale::Marathi,
Sanskrit = QLocale::Sanskrit
};
Q_ENUM(AaharLangs)
virtual ~AaharLanguages() {}
};
#endif // AAHARLANGUAGES_H
C++ File
#ifndef AAHARTRANSLATOR_H
#define AAHARTRANSLATOR_H
#include<QObject>
#include<QApplication>
#include<QTranslator>
#include<AaharLanguages.h>
class AaharTranslator : public QObject
{
Q_OBJECT
Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY emptyStringChanged)
public:
AaharTranslator(QApplication *app) { aaharApp = app; }
QString getEmptyString(){ return ""; }
// virtual ~AaharTranslator() {}
signals:
void emptyStringChanged();
public slots:
void updateLanguage(int language){
switch (language) {
case AaharLanguages::Marathi:
mTranslate.load("IN_Marathi",":/translator");
aaharApp->installTranslator(&mTranslate);
break;
case AaharLanguages::Sanskrit:
mTranslate.load("IN_Sanskrit",":/translator");
aaharApp->installTranslator(&mTranslate);
break;
case AaharLanguages::Hindi:
mTranslate.load("IN_Hindi",":/translator");
aaharApp->installTranslator(&mTranslate);
break;
case AaharLanguages::Gujrati:
mTranslate.load("IN_Gujrati",":/translator");
aaharApp->installTranslator(&mTranslate);
break;
default:
aaharApp->removeTranslator(&mTranslate);
break;
}
emit emptyStringChanged();
}
private:
QApplication *aaharApp;
QTranslator mTranslate;
};
#endif // AAHARTRANSLATOR_H
Add QML Registry in Main.CPP File :
qmlRegisterType<AaharLanguages>("AaharLanguages",1,0,"AaharLanguages");
AaharTranslator translate(&app);
engine.rootContext ()->setContextProperty ("AaharTranslator",(QObject*)&translate);
Now Import Languages :
import AaharLanguages 1.0
Then Add This type of EmptyString to every qsTr() Function :
qsTr("<h1> Hurrey..! <br>New Update Available.</h1>")+AaharTranslator.emptyString
this EmptyString Has Event in C++ File so whenever you change language All String will be updated.! I was facing the same problem even I have to Include 50+ Languages in my code As I belong to Incredible India. Here We have more than 2500 Major Local Languages.!
Retranslate of QMLEngine Will change your layout and Still, I found nothing to Re-State All the Components so for now UseThis Code block.!
Happy Coding Bandhu.!