From: vsr Date: Thu, 31 Jul 2008 07:53:45 +0000 (+0000) Subject: Add QtxTranslator class to workaround limitation of QTranslator class with translatin... X-Git-Tag: TG_TRIPOLI_qt4_porting~36 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=8fc720baba2ad5715b109ed88272c168561eb4ac;p=modules%2Fgui.git Add QtxTranslator class to workaround limitation of QTranslator class with translating messages of global (empty) context in frames of migrating to Qt 4.4.0 --- diff --git a/src/Qtx/Makefile.am b/src/Qtx/Makefile.am index 2386ccb58..d5a34bb89 100755 --- a/src/Qtx/Makefile.am +++ b/src/Qtx/Makefile.am @@ -64,6 +64,7 @@ salomeinclude_HEADERS= \ QtxSplash.h \ QtxToolBar.h \ QtxToolTip.h \ + QtxTranslator.h \ QtxValidator.h \ QtxWorkspace.h \ QtxWorkspaceAction.h \ @@ -117,6 +118,7 @@ dist_libqtx_la_SOURCES= \ QtxSplash.cxx \ QtxToolBar.cxx \ QtxToolTip.cxx \ + QtxTranslator.cxx \ QtxValidator.cxx \ QtxWorkspace.cxx \ QtxWorkspaceAction.cxx \ diff --git a/src/Qtx/QtxResourceMgr.cxx b/src/Qtx/QtxResourceMgr.cxx index 6a83b7b56..80812a2f1 100644 --- a/src/Qtx/QtxResourceMgr.cxx +++ b/src/Qtx/QtxResourceMgr.cxx @@ -20,12 +20,12 @@ // Author: Alexander SOLOVYOV, Sergey TELKOV #include "QtxResourceMgr.h" +#include "QtxTranslator.h" #include #include #include #include -#include #include #ifndef QT_NO_DOM #include @@ -33,8 +33,6 @@ #include #endif -#define EMULATE_GLOBAL_CONTEXT - #include /*! @@ -387,47 +385,9 @@ QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QStrin */ QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, const QString& prefix, const QString& name ) const { - QTranslator* trans = new QTranslator( 0 ); + QTranslator* trans = new QtxTranslator( 0 ); QString fname = fileName( sect, prefix, name ); -#ifdef EMULATE_GLOBAL_CONTEXT - char* buf = 0; - QFile file( fname ); - int len = file.size(); - if ( len ) - { - buf = new char[len]; - if ( !file.open( QIODevice::ReadOnly ) || len != (int)file.read( buf, len ) ) - { - delete buf; - buf = 0; - } - file.close(); - } - if ( buf ) - { - char* pattern = "@default"; - size_t pl = strlen( pattern ); - for ( size_t i = 0; i < len - pl; i++ ) - { - char* cur = buf + i; - if ( !strncmp( cur, pattern, pl ) ) - { - *cur = '\0'; - i += pl - 1; - } - } - - if ( !trans->load( (uchar*)buf, len ) ) - { - delete buf; - buf = 0; - } - } - - if ( !buf ) -#else if ( !trans->load( Qtx::file( fname, false ), Qtx::dir( fname ) ) ) -#endif { delete trans; trans = 0; diff --git a/src/Qtx/QtxTranslator.cxx b/src/Qtx/QtxTranslator.cxx new file mode 100644 index 000000000..f3f955be1 --- /dev/null +++ b/src/Qtx/QtxTranslator.cxx @@ -0,0 +1,78 @@ +// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File: QtxTranslator.cxx +// Author: Alexander SOLOVYOV + +#include "QtxTranslator.h" + +/*! + \class QtxTranslator + \brief Extended version of QTranslator + + In addition to the functionality provided by QTranslator class, QtxTranslator + allows translating resources defined in the global context (i.e. common resources). + The QtxTranslator can be used to workaround QTranslator's limitation which + does not allow to process messages with global context. + + For the current moment global context should be specified in translation + (*.ts/*.qm) files as "@default" string. For example: + \verbatim + + + @default + + MY_MESSAGE + My translated message + + + + \endverbatim +*/ + +#define GLOBAL_CONTEXT "@default" + +/*! + \brief Constructor. +*/ +QtxTranslator::QtxTranslator( QObject* parent ) +: QTranslator( parent ) +{ +} + +/*! + \brief Destructor. +*/ +QtxTranslator::~QtxTranslator() +{ +} + +/*! + \brief Returns the translation for the key. + \param context message context + \param sourceText message source name + \param comment message comment (optional) + \return Translated text if found or \a sourceText otherwise +*/ +QString QtxTranslator::translate( const char* context, const char* sourceText, const char* comment ) const +{ + QString res = QTranslator::translate( context, sourceText, comment ); + if( res.isNull() ) + res = QTranslator::translate( GLOBAL_CONTEXT, sourceText, comment ); + return res; +} diff --git a/src/Qtx/QtxTranslator.h b/src/Qtx/QtxTranslator.h new file mode 100644 index 000000000..a2c9ae058 --- /dev/null +++ b/src/Qtx/QtxTranslator.h @@ -0,0 +1,35 @@ +// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File: QtxTranslator.h +// Author: Alexander SOLOVYOV + +#ifndef QTXTRANSLATOR_H +#define QTXTRANSLATOR_H + +#include + +class QtxTranslator : public QTranslator +{ +public: + QtxTranslator( QObject* parent = 0 ); + ~QtxTranslator(); + virtual QString translate( const char*, const char*, const char* = 0 ) const; +}; + +#endif