Salome HOME
Update copyrights
[modules/gui.git] / src / Qtx / QtxTranslator.cxx
1 // Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:      QtxTranslator.cxx
21 // Author:    Alexander SOLOVYOV
22 //
23 #include "QtxTranslator.h"
24
25 /*!
26   \class QtxTranslator
27   \brief Extended version of QTranslator
28
29   In addition to the functionality provided by QTranslator class, QtxTranslator
30   allows translating resources defined in the global context (i.e. common resources).
31   The QtxTranslator can be used to workaround QTranslator's limitation which 
32   does not allow to process messages with global context.
33
34   For the current moment global context should be specified in translation
35   (*.ts/*.qm) files as "@default" string. For example:
36   \verbatim
37   <!DOCTYPE TS><TS>
38   <context>
39     <name>@default</name>
40     <message>
41         <source>MY_MESSAGE</source>
42         <translation>My translated message</translation>
43     </message>
44   </context>
45   </TS>
46   \endverbatim
47 */
48
49 #define GLOBAL_CONTEXT "@default"
50
51 /*!
52   \brief Constructor.
53 */
54 QtxTranslator::QtxTranslator( QObject* parent )
55 : QTranslator( parent )
56 {
57 }
58
59 /*!
60   \brief Destructor.
61 */
62 QtxTranslator::~QtxTranslator()
63 {
64 }
65
66 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
67
68 /*!
69   \brief Returns the translation for the key.
70   \param context message context
71   \param sourceText message source name
72   \param disambiguation message comment (optional)
73   \return Translated text if found or \a sourceText otherwise
74 */
75 QString QtxTranslator::translate( const char* context, const char* sourceText, const char* disambiguation ) const
76 {
77   QString res = QTranslator::translate( context, sourceText, disambiguation );
78   if ( res.isNull() )
79     res = QTranslator::translate( GLOBAL_CONTEXT, sourceText, disambiguation );
80   return res;
81 }
82
83 #else
84
85 /*!
86   \brief Returns the translation for the key.
87   \param context message context
88   \param sourceText message source name
89   \param disambiguation message comment (optional)
90   \param n optional numeral to choose the appropriate form of translation
91   \return Translated text if found or \a sourceText otherwise
92 */
93 QString QtxTranslator::translate( const char* context, const char* sourceText, const char* disambiguation, int n ) const
94 {
95   QString res = QTranslator::translate( context, sourceText, disambiguation, n );
96   if ( res.isNull() )
97     res = QTranslator::translate( GLOBAL_CONTEXT, sourceText, disambiguation, n );
98   return res;
99 }
100
101 #endif // QT_VERSION < QT_VERSION_CHECK(5, 0, 0)