Commit 15fc3c5e authored by Stéphane Albert's avatar Stéphane Albert

REFAC: Moved i18n part of mvdApplication into mvdI18nApplication base-class...

REFAC: Moved i18n part of mvdApplication into mvdI18nApplication base-class for viewer and manager applications.
parent c34fed36
......@@ -54,13 +54,10 @@ namespace mvd
/*******************************************************************************/
Application
::Application( int& argc, char** argv ) :
QApplication( argc, argv ),
m_IsRunningFromBuildDir( false )
I18nApplication( argc, argv )
{
InitializeCore();
InitializeLocale();
QObject::connect(
this, SIGNAL( aboutToQuit() ),
this, SLOT( OnAboutToQuit() )
......@@ -78,12 +75,6 @@ void
Application
::InitializeCore()
{
//
//
#if 1
QTextCodec::setCodecForTr( QTextCodec::codecForName( "utf8" ) );
#endif
//
// Setup application tags.
QCoreApplication::setApplicationName(
......@@ -103,150 +94,6 @@ Application
);
}
/*******************************************************************************/
void
Application
::InitializeDataDir()
{
}
/*******************************************************************************/
void
Application
::InitializeLocale()
{
//
// 1. default UI language is english (no translation).
QLocale sys_lc( QLocale::system() );
if( sys_lc.language()==QLocale::C ||
( sys_lc.language()==QLocale::English &&
sys_lc.country()==QLocale::UnitedStates ) )
{
return;
}
//
// 2. Choose i18n path between build dir and install dir.
QDir i18n_dir;
QDir bin_dir( QDir::cleanPath( QCoreApplication::applicationDirPath() ) );
QDir build_i18n_dir( bin_dir );
// If build dir is identified...
if( build_i18n_dir.cd( "../i18n" ) &&
build_i18n_dir.exists( "../" Monteverdi2_CONFIGURE_FILE ) )
{
m_IsRunningFromBuildDir = true;
// ...use build dir as prioritary load path for translation.
i18n_dir = build_i18n_dir;
// TODO: Use log system to trace message.
qDebug()
<< tr( "Running from build directory '%1'." ).arg( bin_dir.path() );
}
// Otherwise...
else
{
m_IsRunningFromBuildDir = false;
QDir install_i18n_dir( QDir::cleanPath( Monteverdi2_INSTALL_DATA_I18N_DIR ) );
// ...if install data dir is identified
if( install_i18n_dir.exists() )
{
// ...use install data dir as load path for translation.
i18n_dir = install_i18n_dir;
// TODO: Use log system to trace message.
qDebug()
<< tr( "Running from install directory '%1'." ).arg( Monteverdi2_INSTALL_BIN_DIR );
}
// Otherwise
else
{
QString message(
tr( "Failed to access translation-files directory '%1'." )
.arg( install_i18n_dir.path() )
);
// TODO: Use log system to trace error while loading locale translation file.
qDebug() << message;
// TODO: morph into better HMI design.
QMessageBox::critical( NULL, tr( "Critical error!" ), message );
return;
}
}
//
// 3.1 Stack Qt translator.
LoadAndInstallTranslator(
"qt_" + sys_lc.name(),
QLibraryInfo::location( QLibraryInfo::TranslationsPath )
);
//
// 3.2 Stack Monteverdi2 translator as prioritary over Qt translator.
LoadAndInstallTranslator( sys_lc.name(), i18n_dir.path() );
// TODO: Record locale translation filename(s) used in UI component (e.g.
// AboutDialog, Settings dialog, Information dialog etc.)
}
/*******************************************************************************/
bool
Application
::LoadAndInstallTranslator(const QString& filename,
const QString& directory,
const QString& searchDelimiters,
const QString& suffix )
{
QString filename_ext(
filename +
( suffix.isNull()
? ".qm"
: suffix )
);
// (a) Do need to new QTranslator() here!
QTranslator* lc_translator = new QTranslator( this );
if( !lc_translator->load( filename, directory, searchDelimiters, suffix ) )
{
QString message(
tr( "Failed to load '%1' translation file from '%2'." )
.arg( filename_ext )
.arg( directory )
);
// TODO: Use log system to trace error while loading locale translation file.
qWarning() << message;
// TODO: morph into better HMI design.
QMessageBox::warning( NULL, tr( "Warning!" ), message );
return false;
}
// (a) ...because QTranslator needs to be alive during the whole
// lifespan of the application.
QCoreApplication::installTranslator( lc_translator );
QString message(
tr( "Successfully loaded '%1' translation file from '%2'." )
.arg( filename_ext )
.arg( directory )
);
// TODO: Log locale translation filename used.
qDebug() << message;
return true;
}
/*******************************************************************************/
/* SLOTS */
/*******************************************************************************/
......
......@@ -24,13 +24,13 @@
//
// Qt includes (sorted by alphabetic order)
#include <QApplication>
//
// OTB includes (sorted by alphabetic order)
//
// Monteverdi includes (sorted by alphabetic order)
#include "mvdI18nApplication.h"
//
// Class pre-declaration.
......@@ -43,7 +43,7 @@ namespace mvd
*
*/
class Application
: public QApplication
: public I18nApplication
{
Q_OBJECT;
......@@ -72,33 +72,14 @@ private:
*/
void InitializeCore();
/**
*/
void InitializeDataDir();
/**
*/
void InitializeLocale();
/**
*/
bool LoadAndInstallTranslator( const QString& filename,
const QString& directory =QString(),
const QString& searchDelimiters =QString(),
const QString& suffix =QString() );
//
// Private attributes.
private:
/**
*/
bool m_IsRunningFromBuildDir;
//
// SLOTS
private slots:
/**
*
*/
void OnAboutToQuit();
};
......
/*=========================================================================
Program: Monteverdi2
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved .
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "mvdI18nApplication.h"
//
// System includes (sorted by alphabetic order)
#include <iostream>
//
// Qt includes (sorted by alphabetic order)
#include <QApplication>
#include <QDir>
#include <QLibraryInfo>
#include <QLocale>
#include <QMessageBox>
#include <QTextCodec>
#include <QTranslator>
//
// OTB includes (sorted by alphabetic order)
//
// Monteverdi includes (sorted by alphabetic order)
#include "ConfigureMonteverdi2.h"
//
// Class implementation.
namespace mvd
{
/*
TRANSLATOR mvd::I18nApplication
Necessary for lupdate to be aware of C++ namespaces.
Context comment for translator.
*/
/*******************************************************************************/
I18nApplication
::I18nApplication( int& argc, char** argv ) :
QApplication( argc, argv ),
m_IsRunningFromBuildDir( false )
{
InitializeLocale();
}
/*******************************************************************************/
I18nApplication
::~I18nApplication()
{
}
/*******************************************************************************/
void
I18nApplication
::InitializeLocale()
{
QTextCodec::setCodecForTr( QTextCodec::codecForName( "utf8" ) );
//
// 1. default UI language is english (no translation).
QLocale sys_lc( QLocale::system() );
if( sys_lc.language()==QLocale::C ||
( sys_lc.language()==QLocale::English &&
sys_lc.country()==QLocale::UnitedStates ) )
{
return;
}
//
// 2. Choose i18n path between build dir and install dir.
QDir i18n_dir;
QDir bin_dir( QDir::cleanPath( QCoreApplication::applicationDirPath() ) );
QDir build_i18n_dir( bin_dir );
// If build dir is identified...
if( build_i18n_dir.cd( "../i18n" ) &&
build_i18n_dir.exists( "../" Monteverdi2_CONFIGURE_FILE ) )
{
m_IsRunningFromBuildDir = true;
// ...use build dir as prioritary load path for translation.
i18n_dir = build_i18n_dir;
// TODO: Use log system to trace message.
qDebug()
<< tr( "Running from build directory '%1'." ).arg( bin_dir.path() );
}
// Otherwise...
else
{
m_IsRunningFromBuildDir = false;
QDir install_i18n_dir( QDir::cleanPath( Monteverdi2_INSTALL_DATA_I18N_DIR ) );
// ...if install data dir is identified
if( install_i18n_dir.exists() )
{
// ...use install data dir as load path for translation.
i18n_dir = install_i18n_dir;
// TODO: Use log system to trace message.
qDebug()
<< tr( "Running from install directory '%1'." ).arg( Monteverdi2_INSTALL_BIN_DIR );
}
// Otherwise
else
{
QString message(
tr( "Failed to access translation-files directory '%1'." )
.arg( install_i18n_dir.path() )
);
// TODO: Use log system to trace error while loading locale translation file.
qDebug() << message;
// TODO: morph into better HMI design.
QMessageBox::critical( NULL, tr( "Critical error!" ), message );
return;
}
}
//
// 3.1 Stack Qt translator.
LoadAndInstallTranslator(
"qt_" + sys_lc.name(),
QLibraryInfo::location( QLibraryInfo::TranslationsPath )
);
//
// 3.2 Stack Monteverdi2 translator as prioritary over Qt translator.
LoadAndInstallTranslator( sys_lc.name(), i18n_dir.path() );
// TODO: Record locale translation filename(s) used in UI component (e.g.
// AboutDialog, Settings dialog, Information dialog etc.)
}
/*******************************************************************************/
bool
I18nApplication
::LoadAndInstallTranslator(const QString& filename,
const QString& directory,
const QString& searchDelimiters,
const QString& suffix )
{
QString filename_ext(
filename +
( suffix.isNull()
? ".qm"
: suffix )
);
// (a) Do need to new QTranslator() here!
QTranslator* lc_translator = new QTranslator( this );
if( !lc_translator->load( filename, directory, searchDelimiters, suffix ) )
{
QString message(
tr( "Failed to load '%1' translation file from '%2'." )
.arg( filename_ext )
.arg( directory )
);
// TODO: Use log system to trace error while loading locale translation file.
qWarning() << message;
// TODO: morph into better HMI design.
QMessageBox::warning( NULL, tr( "Warning!" ), message );
return false;
}
// (a) ...because QTranslator needs to be alive during the whole
// lifespan of the application.
QCoreApplication::installTranslator( lc_translator );
QString message(
tr( "Successfully loaded '%1' translation file from '%2'." )
.arg( filename_ext )
.arg( directory )
);
// TODO: Log locale translation filename used.
qDebug() << message;
return true;
}
/*******************************************************************************/
/* SLOTS */
/*******************************************************************************/
} // end namespace 'mvd'
/*=========================================================================
Program: Monteverdi2
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __mvdI18nApplication_h
#define __mvdI18nApplication_h
//
// System includes (sorted by alphabetic order)
//
// Qt includes (sorted by alphabetic order)
#include <QApplication>
//
// OTB includes (sorted by alphabetic order)
//
// Monteverdi includes (sorted by alphabetic order)
//
// Class pre-declaration.
//
// Class declaration.
namespace mvd
{
/** \class I18nApplication
*
*/
class I18nApplication
: public QApplication
{
Q_OBJECT;
//
// Public methods.
public:
/** Constructor */
I18nApplication( int& argc, char** argv );
/** Destructor */
virtual ~I18nApplication();
//
// SIGNALS.
signals:
//
// Protected methods.
protected:
//
// Private methods.
private:
/**
*/
void InitializeLocale();
/**
*/
bool LoadAndInstallTranslator( const QString& filename,
const QString& directory =QString(),
const QString& searchDelimiters =QString(),
const QString& suffix =QString() );
//
// Private attributes.
private:
/**
*/
bool m_IsRunningFromBuildDir;
//
// SLOTS
private slots:
};
} // end namespace 'mvd'
#endif // __I18nApplication_h
......@@ -43,23 +43,23 @@ namespace mvd
Context comment for translator.
*/
/*****************************************************************************/
/*******************************************************************************/
MyClass
::MyClass( QObject* parent ) :
QObject( parent )
{
}
/*****************************************************************************/
/*******************************************************************************/
MyClass
::~MyClass()
{
}
/*****************************************************************************/
/* SLOTS */
/*****************************************************************************/
/*******************************************************************************/
/* SLOTS */
/*******************************************************************************/
/*****************************************************************************/
/*******************************************************************************/
} // end namespace 'mvd'
......@@ -35,3 +35,4 @@
//
// Miscellaneous
#define Monteverdi2_FLOATING_TYPE @Monteverdi2_FLOATING_TYPE@
#define Monteverdi2_IMAGE_DIMENSION_TYPE 2
......@@ -5,23 +5,23 @@
<name>mvd::Application</name>
<message>
<source>Running from build directory &apos;%1&apos;.</source>
<translation>Exécution depuis le répertoire de compilation &apos;%1&apos;.</translation>
<translation type="obsolete">Exécution depuis le répertoire de compilation &apos;%1&apos;.</translation>
</message>
<message>
<source>Running from install directory &apos;%1&apos;.</source>
<translation>Exécution depuis le répertoire d&apos;installation &apos;%1&apos;.</translation>
<translation type="obsolete">Exécution depuis le répertoire d&apos;installation &apos;%1&apos;.</translation>
</message>
<message>
<source>Failed to access translation-files directory &apos;%1&apos;.</source>
<translation>Échec d&apos;accès au répertoire &apos;%1&apos; contenant les fichiers de traduction.</translation>
<translation type="obsolete">Échec d&apos;accès au répertoire &apos;%1&apos; contenant les fichiers de traduction.</translation>
</message>
<message>
<source>Critical error!</source>
<translation>Erreur critique !</translation>
<translation type="obsolete">Erreur critique !</translation>
</message>
<message>
<source>Warning!</source>
<translation>Avertissement !</translation>
<translation type="obsolete">Avertissement !</translation>
</message>
<message>
<source>&gt;DEBUG&gt; mvd::Application::OnAboutToQuit().</source>
......@@ -29,11 +29,11 @@
</message>
<message>
<source>Failed to load &apos;%1&apos; translation file from &apos;%2&apos;.</source>
<translation>Échec de chargement du fichier de traduction &apos;%1&apos; depuis &apos;%2&apos;.</translation>
<translation type="obsolete">Échec de chargement du fichier de traduction &apos;%1&apos; depuis &apos;%2&apos;.</translation>
</message>
<message>
<source>Successfully loaded &apos;%1&apos; translation file from &apos;%2&apos;.</source>
<translation>Chargement réussi du fichier de traduction &apos;%1&apos; depuis &apos;%2&apos;.</translation>
<translation type="obsolete">Chargement réussi du fichier de traduction &apos;%1&apos; depuis &apos;%2&apos;.</translation>
</message>
<message>
<source></source>
......@@ -45,6 +45,42 @@
<translation>Centre National d&apos;Études Spatiales (CNES)</translation>
</message>
</context>
<context>
<name>mvd::I18nApplication</name>
<message>
<source></source>
<comment>Necessary for lupdate to be aware of C++ namespaces. Context comment for translator.</comment>
<translation></translation>
</message>
<message>
<source>Running from build directory &apos;%1&apos;.</source>
<translation>Exécution depuis le répertoire de compilation &apos;%1&apos;.</translation>
</message>
<message>
<source>Running from install directory &apos;%1&apos;.</source>
<translation>Exécution depuis le répertoire d&apos;installation &apos;%1&apos;.</translation>
</message>
<message>
<source>Failed to access translation-files directory &apos;%1&apos;.</source>
<translation>Échec d&apos;accès au répertoire &apos;%1&apos; contenant les fichiers de traduction.</translation>
</message>
<message>
<source>Critical error!</source>
<translation>Erreur critique !</translation>