1 #ifndef _KVI_LOCALE_H_
2 #define _KVI_LOCALE_H_
3 //=============================================================================
4 //
5 //   File : KviLocale.h
6 //   Creation date : Sat Jan 16 1999 18:15:01 by Szymon Stefanek
7 //
8 //   This file is part of the KVIrc IRC client distribution
9 //   Copyright (C) 1999-2010 Szymon Stefanek (pragma at kvirc dot net)
10 //   Copyright (C) 2011 Elvio Basello (hellvis69 at gmail dot com)
11 //
12 //   This program is FREE software. You can redistribute it and/or
13 //   modify it under the terms of the GNU General Public License
14 //   as published by the Free Software Foundation; either version 2
15 //   of the License, or (at your option) any later version.
16 //
17 //   This program is distributed in the HOPE that it will be USEFUL,
18 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 //   See the GNU General Public License for more details.
21 //
22 //   You should have received a copy of the GNU General Public License
23 //   along with this program. If not, write to the Free Software Foundation,
24 //   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 //
26 //=============================================================================
27 
28 /**
29 * \file KviLocale.h
30 * \author Szymon Stefanek
31 * \brief KVIrc localization stuff
32 */
33 
34 #include "kvi_settings.h"
35 #include "KviHeapObject.h"
36 #include "KviMessageCatalogue.h"
37 
38 class KviCString;
39 class QApplication;
40 class QString;
41 class QTextCodec;
42 
43 /**
44 * \def KVI_NUM_ENCODINGS The number of charset encodings
45 * \def KVI_NUM_ENCODING_GROUPS The number of charset groups
46 */
47 #ifndef QT_NO_BIG_CODECS
48 #define KVI_NUM_ENCODINGS 112
49 #define KVI_NUM_ENCODING_GROUPS 8
50 #else
51 #define KVI_NUM_ENCODINGS 85
52 #define KVI_NUM_ENCODING_GROUPS 5
53 #endif
54 
55 /**
56 * \class KviLocale
57 * \brief Holds the localization functions
58 */
59 class KVILIB_API KviLocale : public KviHeapObject
60 {
61 public:
62 	/**
63 	* \typedef EncodingDescription
64 	* \struct _EncodingDescription
65 	* \brief Holds the encoding data
66 	*/
67 	struct EncodingDescription
68 	{
69 		const char * pcName;        /**< name of the encoding */
70 		char bSmart;                /**< is it a smart codec? */
71 		char bSendUtf8;             /**< does it send utf8 or the local charset? */
72 		uint uGroup;                /**< group */
73 		const char * pcDescription; /**< description of the encoding */
74 	};
75 
76 protected:
77 	/**
78 	* \brief Constructs the KviLocale object
79 	* \param pApp The main application
80 	* \param szLocaleDir The directory containing the localizations
81 	* \param szForceLocaleDir The directory forced by the user
82 	* \return KviLocale
83 	*/
84 	KviLocale(QApplication * pApp, const QString & szLocaleDir, const QString & szForceLocaleDir);
85 
86 	/**
87 	* \brief Destroys the object
88 	* \return KviLocale
89 	*/
90 	~KviLocale();
91 
92 public:
93 	static QString g_szLang;
94 
95 protected:
96 	QApplication * m_pApp;
97 
98 private:
99 	static KviLocale * m_pSelf;
100 	static unsigned int m_uCount;
101 
102 public:
103 	/**
104 	* \brief Initializes the class instance
105 	* \param pApp The main application
106 	* \param szLocaleDir The directory containing the localizations
107 	* \param szForceLocaleDir The directory forced by the user
108 	* \return void
109 	*/
110 	static void init(QApplication * pApp, const QString & szLocaleDir, const QString & szForceLocaleDir);
111 
112 	/**
113 	* \brief Destroys the class instance
114 	* \param pApp The main application
115 	* \return void
116 	*/
117 	static void done();
118 
119 	/**
120 	* \brief Returns the instance of the class
121 	* \return KviLocale *
122 	*/
instance()123 	static inline KviLocale * instance() { return m_pSelf; }
124 
125 	/**
126 	* \brief Returns the number of instances of the class
127 	* \return unsigned int
128 	*/
count()129 	unsigned int count() { return m_uCount; }
130 
131 	/**
132 	* \brief Returns the description of the encoding used
133 	* \param iIdx The index of the description
134 	* \warning You MUST start iterating from 0 and terminate when you get an entry with
135 	* a NULL pcName
136 	* \return EncodingDescription *
137 	*/
138 	EncodingDescription * encodingDescription(int iIdx);
139 
140 	/**
141 	* \brief Returns the description of the encoding used
142 	* \param iIdx The index of the group
143 	* \warning You MUST start iterating from 0 and terminate when you get an entry with
144 	* a NULL value
145 	* \return const char *
146 	*/
147 	const char * encodingGroup(int iIdx);
148 
149 	/**
150 	* \brief Returns the language code of the localization
151 	* \return const QString &
152 	*/
localeName()153 	const QString & localeName() const { return g_szLang; }
154 
155 	/**
156 	* \brief Returns the codec associated to the given translation
157 	* \param pcName The name of the translation
158 	* \return QTextCodec *
159 	*/
160 	QTextCodec * codecForName(const char * pcName);
161 
162 	/**
163 	* \brief Finds the catalogue
164 	*
165 	* This function attempts to determine the current locale and then load the
166 	* corresponding translation file from the KVIrc locale directory.
167 	* Returns true if the locale was correctly set; i.e. the locale is C or POSIX
168 	* (no translation needed) or the locale is correctly defined and the translation
169 	* map was successfully loaded
170 	* \param szBuffer The buffer where to store the translation full path
171 	* \param szName The name of the translation file
172 	* \param szLocaleDir The directory where the localizations are stored
173 	* \return bool
174 	*/
175 	bool findCatalogue(QString & szBuffer, const QString & szName, const QString & szLocaleDir);
176 
177 	/**
178 	* \brief Loads the catalogue
179 	* \param szName The name of the catalogue
180 	* \param szLocaleDir The directory where to look for the catalogue
181 	* \return KviMessageCatalogue *
182 	*/
183 	KviMessageCatalogue * loadCatalogue(const QString & szName, const QString & szLocaleDir);
184 
185 	/**
186 	* \brief Unloads a catalogue
187 	* \param szName The catalogue to unload
188 	* \return bool
189 	*/
190 	bool unloadCatalogue(const QString & szName);
191 
192 	/**
193 	* \brief Returns the loaded catalogue
194 	* \param szName The name of the catalogue to get
195 	* \return KviMessageCatalogue *
196 	*/
197 	KviMessageCatalogue * getLoadedCatalogue(const QString & szName);
198 
199 	/**
200 	* \brief Translates the given text from the given context
201 	* \param pcText The text to translate
202 	* \param pcContext The context where to look for the text
203 	* \return const char *
204 	*/
205 	const char * translate(const char * pcText, const char * pcContext);
206 
207 	/**
208 	* \brief Translates the given text from the given context
209 	* \param pcText The text to translate
210 	* \param pcContext The context where to look for the text
211 	* \return const QString &
212 	*/
213 	const QString & translateToQString(const char * pcText, const char * pcContext);
214 };
215 
216 #ifndef _KVI_LOCALE_CPP_
217 extern KVILIB_API KviMessageCatalogue * g_pMainCatalogue;
218 #endif // !_KVI_LOCALE_CPP_
219 
220 #define __tr(text) g_pMainCatalogue->translate(text)
221 #define __tr_no_lookup(text) text
222 #define __tr_no_xgettext(text) g_pMainCatalogue->translate(text)
223 #define __tr2qs(text) g_pMainCatalogue->translateToQString(text)
224 #define __tr2qs_no_lookup(text) text
225 #define __tr2qs_no_xgettext(text) g_pMainCatalogue->translateToQString(text)
226 
227 #define __tr_ctx(text, context) KviLocale::instance()->translate(text, context)
228 #define __tr_no_lookup_ctx(text, context) text
229 #define __tr_no_xgettext_ctx(text, context) KviLocale::instance()->translate(text, context)
230 #define __tr2qs_ctx(text, context) KviLocale::instance()->translateToQString(text, context)
231 #define __tr2qs_ctx_no_xgettext(text, context) KviLocale::instance()->translateToQString(text, context)
232 
233 #endif //_KVI_LOCALE_H_
234