aboutsummaryrefslogtreecommitdiffstats
path: root/src/kvilib/locale/KviLocale.h
blob: 9d356a89e37e635a716d7102525b2e5d183330b5 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#ifndef _KVI_LOCALE_H_
#define _KVI_LOCALE_H_
//=============================================================================
//
//   File : KviLocale.h
//   Creation date : Sat Jan 16 1999 18:15:01 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 1999-2010 Szymon Stefanek (pragma at kvirc dot net)
//   Copyright (C) 2011 Elvio Basello (hellvis69 at gmail dot com)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your option) any later version.
//
//   This program 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 General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

/**
* \file KviLocale.h
* \author Szymon Stefanek
* \brief KVIrc localization stuff
*/

#include "kvi_settings.h"
#include "KviHeapObject.h"
#include "KviMessageCatalogue.h"

class KviCString;
class QApplication;
class QString;
class QTextCodec;

/**
* \def KVI_NUM_ENCODINGS The number of charset encodings
* \def KVI_NUM_ENCODING_GROUPS The number of charset groups
*/
#ifndef QT_NO_BIG_CODECS
#define KVI_NUM_ENCODINGS 112
#define KVI_NUM_ENCODING_GROUPS 8
#else
#define KVI_NUM_ENCODINGS 85
#define KVI_NUM_ENCODING_GROUPS 5
#endif

/**
* \class KviLocale
* \brief Holds the localization functions
*/
class KVILIB_API KviLocale : public KviHeapObject
{
public:
	/**
	* \typedef EncodingDescription
	* \struct _EncodingDescription
	* \brief Holds the encoding data
	*/
	struct EncodingDescription
	{
		const char * pcName;        /**< name of the encoding */
		char bSmart;                /**< is it a smart codec? */
		char bSendUtf8;             /**< does it send utf8 or the local charset? */
		uint uGroup;                /**< group */
		const char * pcDescription; /**< description of the encoding */
	};

protected:
	/**
	* \brief Constructs the KviLocale object
	* \param pApp The main application
	* \param szLocaleDir The directory containing the localizations
	* \param szForceLocaleDir The directory forced by the user
	* \return KviLocale
	*/
	KviLocale(QApplication * pApp, const QString & szLocaleDir, const QString & szForceLocaleDir);

	/**
	* \brief Destroys the object
	* \return KviLocale
	*/
	~KviLocale();

public:
	static QString g_szLang;

protected:
	QApplication * m_pApp;

private:
	static KviLocale * m_pSelf;

public:
	/**
	* \brief Initializes the class instance
	* \param pApp The main application
	* \param szLocaleDir The directory containing the localizations
	* \param szForceLocaleDir The directory forced by the user
	* \return void
	*/
	static void init(QApplication * pApp, const QString & szLocaleDir, const QString & szForceLocaleDir);

	/**
	* \brief Destroys the class instance
	* \param pApp The main application
	* \return void
	*/
	static void done();

	/**
	* \brief Returns the instance of the class
	* \return KviLocale *
	*/
	static inline KviLocale * instance() { return m_pSelf; }

	/**
	* \brief Returns the description of the encoding used
	* \param iIdx The index of the description
	* \warning You MUST start iterating from 0 and terminate when you get an entry with
	* a NULL pcName
	* \return EncodingDescription *
	*/
	EncodingDescription * encodingDescription(int iIdx);

	/**
	* \brief Returns the description of the encoding used
	* \param iIdx The index of the group
	* \warning You MUST start iterating from 0 and terminate when you get an entry with
	* a NULL value
	* \return const char *
	*/
	const char * encodingGroup(int iIdx);

	/**
	* \brief Returns the language code of the localization
	* \return const QString &
	*/
	const QString & localeName() const { return g_szLang; }

	/**
	* \brief Returns the codec associated to the given translation
	* \param pcName The name of the translation
	* \return QTextCodec *
	*/
	QTextCodec * codecForName(const char * pcName);

	/**
	* \brief Finds the catalogue
	*
	* This function attempts to determine the current locale and then load the
	* corresponding translation file from the KVIrc locale directory.
	* Returns true if the locale was correctly set; i.e. the locale is C or POSIX
	* (no translation needed) or the locale is correctly defined and the translation
	* map was successfully loaded
	* \param szBuffer The buffer where to store the translation full path
	* \param szName The name of the translation file
	* \param szLocaleDir The directory where the localizations are stored
	* \return bool
	*/
	bool findCatalogue(QString & szBuffer, const QString & szName, const QString & szLocaleDir);

	/**
	* \brief Loads the catalogue
	* \param szName The name of the catalogue
	* \param szLocaleDir The directory where to look for the catalogue
	* \return KviMessageCatalogue *
	*/
	KviMessageCatalogue * loadCatalogue(const QString & szName, const QString & szLocaleDir);

	/**
	* \brief Unloads a catalogue
	* \param szName The catalogue to unload
	* \return bool
	*/
	bool unloadCatalogue(const QString & szName);

	/**
	* \brief Returns the loaded catalogue
	* \param szName The name of the catalogue to get
	* \return KviMessageCatalogue *
	*/
	KviMessageCatalogue * getLoadedCatalogue(const QString & szName);

	/**
	* \brief Translates the given text from the given context
	* \param pcText The text to translate
	* \param pcContext The context where to look for the text
	* \return const char *
	*/
	const char * translate(const char * pcText, const char * pcContext);

	/**
	* \brief Translates the given text from the given context
	* \param pcText The text to translate
	* \param pcContext The context where to look for the text
	* \return const QString &
	*/
	const QString & translateToQString(const char * pcText, const char * pcContext);
};

#ifndef _KVI_LOCALE_CPP_
extern KVILIB_API KviMessageCatalogue * g_pMainCatalogue;
#endif // !_KVI_LOCALE_CPP_

#define __tr(text) g_pMainCatalogue->translate(text)
#define __tr_no_lookup(text) text
#define __tr_no_xgettext(text) g_pMainCatalogue->translate(text)
#define __tr2qs(text) g_pMainCatalogue->translateToQString(text)
#define __tr2qs_no_lookup(text) text
#define __tr2qs_no_xgettext(text) g_pMainCatalogue->translateToQString(text)

#define __tr_ctx(text, context) KviLocale::instance()->translate(text, context)
#define __tr_no_lookup_ctx(text, context) text
#define __tr_no_xgettext_ctx(text, context) KviLocale::instance()->translate(text, context)
#define __tr2qs_ctx(text, context) KviLocale::instance()->translateToQString(text, context)
#define __tr2qs_ctx_no_xgettext(text, context) KviLocale::instance()->translateToQString(text, context)

#endif //_KVI_LOCALE_H_