PDA

View Full Version : Translation system simply not working!



Momergil
31st July 2014, 16:19
Hello!

I'm knocking my head for some hours now trying to understand why my translation mechanism is not working despite the fact that I followed all translation manuals and examples (no to mention that to translate seems a pretty easy task). What happens is that I created the .ts files, translated (parts of it) in Qt Linguist, created the .qm files and added them to the specified folder. Then I call QTranslator etc. in my program and both finding the translator files as well as loading them returns no error - but no translation actually occurs in the application.

Here is (most of) the code so you all may check if I missed something:



//main.cpp
qint32 main(qint32 argc, char *argv[])
{
QApplication::setGraphicsSystem("raster");

//
QApplication app(argc, argv);
app.setApplicationName("RE8000");
app.setApplicationVersion(APP_VERSION);
app.setOrganizationName("XXXX");
app.setOrganizationDomain("http://www.xxxx.com.br/");

app.thread()->setPriority(QThread::HighestPriority);

if (!setTranslation())
CFG_ED.setProfileLanguage(CFG::Language_English);

//Starts creating the widgets
g_mainSplashScreen = new SplashScreen(QPixmap(":/Images/EntraceImage.bmp"), Qt::WindowStaysOnTopHint);
g_mainSplashScreen->setGeometry((QApplication::desktop()->width() - g_mainSplashScreen->pixmap().width())/2,
(QApplication::desktop()->height() - g_mainSplashScreen->height())/2,
g_mainSplashScreen->pixmap().width(),g_mainSplashScreen->height());
}

//...
static bool setTranslation()
{
const CFG::AvailableLanguages currLanguage = CFG_ED.profileLanguage();

Settings::Profile::ProfileCenterView::setDefaultLo cale(currLanguage);

if (Q_UNLIKELY(!Settings::Profile::ProfileCenterView: :installLanguage(currLanguage)))
{
mLog("An error occured while trying to load the translation file (static bool setTranslation(QApplication&))");

return false;
}

return true;
}

//Other files
void ProfileCenterView::setDefaultLocale(const Config::UserCfgSingletom::AvailableLanguages language) (static)
{
switch (language)
{
case CFG::Language_English: QLocale::setDefault(QLocale(QLocale::English,QLoca le::UnitedStates)); break;
case CFG::Language_Espanol: QLocale::setDefault(QLocale(QLocale::Spanish,QLoca le::LatinAmericaAndTheCaribbean)); break;
case CFG::Language_PortugueseBR: QLocale::setDefault(QLocale(QLocale::Portuguese,QL ocale::Brazil)); break;

default: QLocale::setDefault(QLocale::c()); break;
}
}

bool ProfileCenterView::installLanguage(const Config::UserCfgSingletom::AvailableLanguages language) (static)
{
if (language == CFG::Language_English)
return true;

//
QDir translationDir(DEFAULT_TRANSLATION_PATH);

QStringList fileNameList = translationDir.entryList(QStringList("*.qm"),QDir::Files,QDir::Name);

if (Q_UNLIKELY(fileNameList.isEmpty()))
{
mDebugS("Error while trying to install translator: no translator files were found (ProfileCenterView::installLanguage))");
mLog("No translation files were found");

return false;
}

//![] Install selected translation
QTranslator translator;

if (Q_UNLIKELY(!translator.load(QLocale(),"re8000","_",DEFAULT_TRANSLATION_PATH,".qm")))
{
mDebugS("Error while trying to install translator: error while loading file (ProfileCenterView::installLanguage)");
mLog("Error when trying to load the translation file");

return false;
}

qApp->installTranslator(&translator);

return true;
}


Remember: as I sad, no errors are returned; only the translations don't appear as if I hadn't done any translations to the .ts file (which I did!).


What could be wrong? :(

I'm glad for any help,

Momergil

wysota
31st July 2014, 16:24
Guess what happens to QTranslator instance after the scope where it is declared ends...

Momergil
31st July 2014, 17:05
Guess what happens to QTranslator instance after the scope where it is declared ends...

Hmm! So you mean that the QTranslator object need to be kept alive during the whole translation process, not only for the QTranslator::load() operation? Well, I'll check that out after dinner and return with the results!

wysota
31st July 2014, 17:08
Think why you are passing a pointer to the object to installTranslator...

Momergil
31st July 2014, 19:59
Think why you are passing a pointer to the object to installTranslator...

hmm, ok, you certainly made a point :)

So now the QTranslator inside my installLanguage method is a static one and everything is working 'fine' :) The other problem I'm having was mentioned here (http://www.qtcentre.org/threads/59874-lupdate-not-loading-all-strings).


Thanks,

Momergil

wysota
31st July 2014, 23:28
QTranslator is a QObject so you can create it on heap passing a parent to it and then forget about the object if you don't have functionality for dynamically unloading translations in your application.