// Author: Alexander SOLOVYOV, Sergey TELKOV
#include "QtxResourceMgr.h"
+#include "QtxTranslator.h"
#include <QDir>
#include <QFile>
#include <QRegExp>
#include <QTextStream>
-#include <QTranslator>
#include <QApplication>
#ifndef QT_NO_DOM
#include <QDomDocument>
#include <QDomNode>
#endif
-#define EMULATE_GLOBAL_CONTEXT
-
#include <stdlib.h>
/*!
*/
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;
--- /dev/null
+// 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
+ <!DOCTYPE TS><TS>
+ <context>
+ <name>@default</name>
+ <message>
+ <source>MY_MESSAGE</source>
+ <translation>My translated message</translation>
+ </message>
+ </context>
+ </TS>
+ \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;
+}
--- /dev/null
+// 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 <QTranslator>
+
+class QtxTranslator : public QTranslator
+{
+public:
+ QtxTranslator( QObject* parent = 0 );
+ ~QtxTranslator();
+ virtual QString translate( const char*, const char*, const char* = 0 ) const;
+};
+
+#endif