I am using Qt5 under Windows 7.
Few days ago somebody requested the app should have the so called "internationalization" feature, i.e. to allow app-messages in more than one language...
So I started with QTranslator class (provided by Qt) and my approach was to use Monostate pattern design in order to implement the requested feature.
For the moment I only have 2 languages (default=Portuguese and English)...
The code is listed below :
#include <QVariant>
#include <QCoreApplication>
#include "translator.h"
#include "settingsmanager.h"
int Translator::currentLanguage = Translator::Language_Unknown;
QTranslator Translator::translator;
// Prefiro "Monostate" porque "Singleton" é muito feio :(
Translator::Translator(QObject * parent) : QObject(parent)
{
if(currentLanguage == Language_Unknown)
{
changeLanguage();
}
}
Translator::~Translator()
{
}
void Translator::changeLanguage()
{
SettingsManager settings;
int language = settings.readSetting("Language").toInt();
//
if(language != currentLanguage)
{
QString translationFile;
QCoreApplication::removeTranslator(&translator);
switch(language)
{
case Language_English:
translationFile.append("language_en.qm");
translator.load(translationFile);
QCoreApplication::installTranslator(&translator);
break;
default: // Português
// nada, it's the default
break;
}
currentLanguage = language;
}
}
Finally, my question is:
Do you think it is a good approach?
Would it be flexible enough to allow me to add other/new languages in the future?
Is there another way (better way) to implement it?
Aucun commentaire:
Enregistrer un commentaire