]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: intl.cpp | |
3 | // Purpose: Internationalization and localisation for wxWindows | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
7af89395 | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
1678ad78 | 13 | // declaration |
c801d85f KB |
14 | // ============================================================================ |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
84c18814 | 21 | #pragma implementation "intl.h" |
c801d85f KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
84c18814 | 28 | #pragma hdrstop |
c801d85f KB |
29 | #endif |
30 | ||
d427503c VZ |
31 | #if wxUSE_INTL |
32 | ||
1678ad78 GL |
33 | // standard headers |
34 | #include <locale.h> | |
0fb67cd1 | 35 | #include <ctype.h> |
7502ba29 | 36 | |
1678ad78 GL |
37 | // wxWindows |
38 | #include "wx/defs.h" | |
39 | #include "wx/string.h" | |
c801d85f KB |
40 | #include "wx/intl.h" |
41 | #include "wx/file.h" | |
42 | #include "wx/log.h" | |
43 | #include "wx/utils.h" | |
44 | ||
45 | #include <stdlib.h> | |
46 | ||
84c18814 VZ |
47 | // ---------------------------------------------------------------------------- |
48 | // simple types | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
e939abfd | 51 | // this should *not* be wxChar, this type must have exactly 8 bits! |
84c18814 | 52 | typedef unsigned char size_t8; |
e939abfd VZ |
53 | |
54 | #ifdef __WXMSW__ | |
55 | #if defined(__WIN16__) | |
56 | typedef unsigned long size_t32; | |
57 | #elif defined(__WIN32__) | |
58 | typedef unsigned int size_t32; | |
59 | #else | |
60 | // Win64 will have different type sizes | |
61 | #error "Please define a 32 bit type" | |
62 | #endif | |
63 | #else // !Windows | |
64 | // SIZEOF_XXX are defined by configure | |
65 | #if defined(SIZEOF_INT) && (SIZEOF_INT == 4) | |
66 | typedef unsigned int size_t32; | |
67 | #elif defined(SIZEOF_LONG) && (SIZEOF_LONG == 4) | |
68 | typedef unsigned long size_t32; | |
69 | #else | |
70 | // assume sizeof(int) == 4 - what else can we do | |
71 | typedef unsigned int size_t32; | |
72 | ||
73 | // ... but at least check it during run time | |
74 | static class IntSizeChecker | |
75 | { | |
76 | public: | |
77 | IntSizeChecker() | |
78 | { | |
913df6f2 DW |
79 | // Asserting a sizeof directly causes some compilers to |
80 | // issue a "using constant in a conditional expression" warning | |
81 | size_t intsize = sizeof(int); | |
82 | ||
83 | wxASSERT_MSG( intsize == 4, | |
e939abfd VZ |
84 | "size_t32 is incorrectly defined!" ); |
85 | } | |
86 | } intsizechecker; | |
87 | #endif | |
88 | #endif // Win/!Win | |
84c18814 | 89 | |
c801d85f KB |
90 | // ---------------------------------------------------------------------------- |
91 | // constants | |
92 | // ---------------------------------------------------------------------------- | |
93 | ||
94 | // magic number identifying the .mo format file | |
c86f1403 VZ |
95 | const size_t32 MSGCATALOG_MAGIC = 0x950412de; |
96 | const size_t32 MSGCATALOG_MAGIC_SW = 0xde120495; | |
c801d85f KB |
97 | |
98 | // extension of ".mo" files | |
99 | #define MSGCATALOG_EXTENSION ".mo" | |
100 | ||
101 | // ---------------------------------------------------------------------------- | |
1678ad78 | 102 | // global functions |
c801d85f KB |
103 | // ---------------------------------------------------------------------------- |
104 | ||
105 | // suppress further error messages about missing translations | |
106 | // (if you don't have one catalog file, you wouldn't like to see the | |
107 | // error message for each string in it, so normally it's given only | |
108 | // once) | |
1678ad78 | 109 | void wxSuppressTransErrors(); |
c801d85f KB |
110 | |
111 | // restore the logging | |
1678ad78 | 112 | void wxRestoreTransErrors(); |
c801d85f KB |
113 | |
114 | // get the current state | |
1678ad78 | 115 | bool wxIsLoggingTransErrors(); |
c801d85f | 116 | |
84c18814 | 117 | static wxLocale *wxSetLocale(wxLocale *pLocale); |
c801d85f KB |
118 | |
119 | // ---------------------------------------------------------------------------- | |
120 | // wxMsgCatalog corresponds to one disk-file message catalog. | |
121 | // | |
122 | // This is a "low-level" class and is used only by wxLocale (that's why | |
123 | // it's designed to be stored in a linked list) | |
124 | // ---------------------------------------------------------------------------- | |
125 | ||
126 | class wxMsgCatalog | |
127 | { | |
128 | public: | |
129 | // ctor & dtor | |
130 | wxMsgCatalog(); | |
131 | ~wxMsgCatalog(); | |
132 | ||
133 | // load the catalog from disk (szDirPrefix corresponds to language) | |
e36e6f95 | 134 | bool Load(const wxChar *szDirPrefix, const wxChar *szName); |
c801d85f KB |
135 | bool IsLoaded() const { return m_pData != NULL; } |
136 | ||
137 | // get name of the catalog | |
e36e6f95 | 138 | const wxChar *GetName() const { return m_pszName; } |
c801d85f KB |
139 | |
140 | // get the translated string: returns NULL if not found | |
141 | const char *GetString(const char *sz) const; | |
142 | ||
143 | // public variable pointing to the next element in a linked list (or NULL) | |
144 | wxMsgCatalog *m_pNext; | |
7af89395 | 145 | |
c801d85f KB |
146 | private: |
147 | // this implementation is binary compatible with GNU gettext() version 0.10 | |
148 | ||
149 | // an entry in the string table | |
150 | struct wxMsgTableEntry | |
151 | { | |
c86f1403 VZ |
152 | size_t32 nLen; // length of the string |
153 | size_t32 ofsString; // pointer to the string | |
c801d85f KB |
154 | }; |
155 | ||
156 | // header of a .mo file | |
157 | struct wxMsgCatalogHeader | |
158 | { | |
c86f1403 | 159 | size_t32 magic, // offset +00: magic id |
84c18814 VZ |
160 | revision, // +04: revision |
161 | numStrings; // +08: number of strings in the file | |
c86f1403 | 162 | size_t32 ofsOrigTable, // +0C: start of original string table |
84c18814 | 163 | ofsTransTable; // +10: start of translated string table |
c86f1403 | 164 | size_t32 nHashSize, // +14: hash table size |
84c18814 | 165 | ofsHashTable; // +18: offset of hash table start |
7af89395 VZ |
166 | }; |
167 | ||
c801d85f | 168 | // all data is stored here, NULL if no data loaded |
c86f1403 | 169 | size_t8 *m_pData; |
7af89395 | 170 | |
c801d85f | 171 | // data description |
84c18814 | 172 | size_t32 m_numStrings, // number of strings in this domain |
c801d85f | 173 | m_nHashSize; // number of entries in hash table |
84c18814 | 174 | size_t32 *m_pHashTable; // pointer to hash table |
c801d85f KB |
175 | wxMsgTableEntry *m_pOrigTable, // pointer to original strings |
176 | *m_pTransTable; // translated | |
177 | ||
c86f1403 | 178 | const char *StringAtOfs(wxMsgTableEntry *pTable, size_t32 index) const |
c801d85f KB |
179 | { return (const char *)(m_pData + Swap(pTable[index].ofsString)); } |
180 | ||
181 | // utility functions | |
182 | // calculate the hash value of given string | |
c86f1403 | 183 | static inline size_t32 GetHash(const char *sz); |
c801d85f | 184 | // big<->little endian |
c86f1403 | 185 | inline size_t32 Swap(size_t32 ui) const; |
c801d85f KB |
186 | |
187 | // internal state | |
188 | bool HasHashTable() const // true if hash table is present | |
189 | { return m_nHashSize > 2 && m_pHashTable != NULL; } | |
190 | ||
191 | bool m_bSwapped; // wrong endianness? | |
192 | ||
e36e6f95 | 193 | wxChar *m_pszName; // name of the domain |
c801d85f KB |
194 | }; |
195 | ||
fd323a5e VZ |
196 | // ---------------------------------------------------------------------------- |
197 | // global variables | |
198 | // ---------------------------------------------------------------------------- | |
199 | ||
200 | // the list of the directories to search for message catalog files | |
201 | static wxArrayString s_searchPrefixes; | |
202 | ||
c801d85f KB |
203 | // ============================================================================ |
204 | // implementation | |
205 | // ============================================================================ | |
206 | ||
207 | // ---------------------------------------------------------------------------- | |
208 | // wxMsgCatalog class | |
209 | // ---------------------------------------------------------------------------- | |
210 | ||
211 | // calculate hash value using the so called hashpjw function by P.J. Weinberger | |
212 | // [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools] | |
c86f1403 | 213 | size_t32 wxMsgCatalog::GetHash(const char *sz) |
c801d85f | 214 | { |
c86f1403 | 215 | #define HASHWORDBITS 32 // the length of size_t32 |
c801d85f | 216 | |
c86f1403 VZ |
217 | size_t32 hval = 0; |
218 | size_t32 g; | |
c801d85f KB |
219 | while ( *sz != '\0' ) { |
220 | hval <<= 4; | |
c86f1403 VZ |
221 | hval += (size_t32)*sz++; |
222 | g = hval & ((size_t32)0xf << (HASHWORDBITS - 4)); | |
c801d85f KB |
223 | if ( g != 0 ) { |
224 | hval ^= g >> (HASHWORDBITS - 8); | |
225 | hval ^= g; | |
226 | } | |
227 | } | |
228 | ||
229 | return hval; | |
230 | } | |
231 | ||
232 | // swap the 2 halves of 32 bit integer if needed | |
c86f1403 | 233 | size_t32 wxMsgCatalog::Swap(size_t32 ui) const |
c801d85f | 234 | { |
7af89395 | 235 | return m_bSwapped ? (ui << 24) | ((ui & 0xff00) << 8) | |
c801d85f KB |
236 | ((ui >> 8) & 0xff00) | (ui >> 24) |
237 | : ui; | |
238 | } | |
239 | ||
7af89395 VZ |
240 | wxMsgCatalog::wxMsgCatalog() |
241 | { | |
c801d85f KB |
242 | m_pData = NULL; |
243 | m_pszName = NULL; | |
244 | } | |
245 | ||
7af89395 VZ |
246 | wxMsgCatalog::~wxMsgCatalog() |
247 | { | |
248 | wxDELETEA(m_pData); | |
249 | wxDELETEA(m_pszName); | |
c801d85f KB |
250 | } |
251 | ||
fd323a5e | 252 | // small class to suppress the translation erros until exit from current scope |
c801d85f KB |
253 | class NoTransErr |
254 | { | |
fd323a5e | 255 | public: |
c801d85f KB |
256 | NoTransErr() { wxSuppressTransErrors(); } |
257 | ~NoTransErr() { wxRestoreTransErrors(); } | |
258 | }; | |
7af89395 | 259 | |
fd323a5e | 260 | // return all directories to search for given prefix |
e36e6f95 OK |
261 | static wxString GetAllMsgCatalogSubdirs(const wxChar *prefix, |
262 | const wxChar *lang) | |
fd323a5e VZ |
263 | { |
264 | wxString searchPath; | |
265 | ||
266 | // search first in prefix/fr/LC_MESSAGES, then in prefix/fr and finally in | |
267 | // prefix (assuming the language is 'fr') | |
7af89395 | 268 | searchPath << prefix << wxFILE_SEP_PATH << lang << wxFILE_SEP_PATH |
e36e6f95 | 269 | << _T("LC_MESSAGES") << wxPATH_SEP |
7af89395 VZ |
270 | << prefix << wxFILE_SEP_PATH << lang << wxPATH_SEP |
271 | << prefix << wxPATH_SEP; | |
fd323a5e VZ |
272 | |
273 | return searchPath; | |
274 | } | |
275 | ||
276 | // construct the search path for the given language | |
e36e6f95 | 277 | static wxString GetFullSearchPath(const wxChar *lang) |
fd323a5e VZ |
278 | { |
279 | wxString searchPath; | |
280 | ||
281 | // first take the entries explicitly added by the program | |
282 | size_t count = s_searchPrefixes.Count(); | |
283 | for ( size_t n = 0; n < count; n++ ) | |
284 | { | |
285 | searchPath << GetAllMsgCatalogSubdirs(s_searchPrefixes[n], lang) | |
7af89395 | 286 | << wxPATH_SEP; |
fd323a5e VZ |
287 | } |
288 | ||
289 | // then take the current directory | |
290 | // FIXME it should be the directory of the executable | |
e36e6f95 | 291 | searchPath << GetAllMsgCatalogSubdirs(_T("."), lang) << wxPATH_SEP; |
fd323a5e VZ |
292 | |
293 | // and finally add some standard ones | |
294 | searchPath | |
e36e6f95 OK |
295 | << GetAllMsgCatalogSubdirs(_T("/usr/share/locale"), lang) << wxPATH_SEP |
296 | << GetAllMsgCatalogSubdirs(_T("/usr/lib/locale"), lang) << wxPATH_SEP | |
297 | << GetAllMsgCatalogSubdirs(_T("/usr/local/share/locale"), lang); | |
fd323a5e VZ |
298 | |
299 | return searchPath; | |
300 | } | |
301 | ||
c801d85f | 302 | // open disk file and read in it's contents |
e36e6f95 | 303 | bool wxMsgCatalog::Load(const wxChar *szDirPrefix, const wxChar *szName) |
c801d85f | 304 | { |
fd323a5e VZ |
305 | // FIXME VZ: I forgot the exact meaning of LC_PATH - anyone to remind me? |
306 | #if 0 | |
e36e6f95 | 307 | const wxChar *pszLcPath = wxGetenv("LC_PATH"); |
c801d85f | 308 | if ( pszLcPath != NULL ) |
fd323a5e VZ |
309 | strPath += pszLcPath + wxString(szDirPrefix) + MSG_PATH; |
310 | #endif // 0 | |
7af89395 | 311 | |
fd323a5e | 312 | wxString searchPath = GetFullSearchPath(szDirPrefix); |
e36e6f95 | 313 | const wxChar *sublocale = wxStrchr(szDirPrefix, _T('_')); |
fd323a5e VZ |
314 | if ( sublocale ) |
315 | { | |
316 | // also add just base locale name: for things like "fr_BE" (belgium | |
317 | // french) we should use "fr" if no belgium specific message catalogs | |
318 | // exist | |
319 | searchPath << GetFullSearchPath(wxString(szDirPrefix). | |
320 | Left((size_t)(sublocale - szDirPrefix))) | |
7af89395 | 321 | << wxPATH_SEP; |
fd323a5e | 322 | } |
7af89395 | 323 | |
c801d85f KB |
324 | wxString strFile = szName; |
325 | strFile += MSGCATALOG_EXTENSION; | |
326 | ||
327 | // don't give translation errors here because the wxstd catalog might | |
328 | // not yet be loaded (and it's normal) | |
329 | // | |
330 | // (we're using an object because we have several return paths) | |
331 | NoTransErr noTransErr; | |
332 | ||
1a5a8367 | 333 | wxLogVerbose(_("looking for catalog '%s' in path '%s'."), |
fd323a5e | 334 | szName, searchPath.c_str()); |
c801d85f KB |
335 | |
336 | wxString strFullName; | |
fd323a5e | 337 | if ( !wxFindFileInPath(&strFullName, searchPath, strFile) ) { |
1a5a8367 | 338 | wxLogWarning(_("catalog file for domain '%s' not found."), szName); |
c801d85f KB |
339 | return FALSE; |
340 | } | |
341 | ||
342 | // open file | |
1a5a8367 | 343 | wxLogVerbose(_("using catalog '%s' from '%s'."), |
1678ad78 | 344 | szName, strFullName.c_str()); |
7af89395 | 345 | |
c801d85f KB |
346 | wxFile fileMsg(strFullName); |
347 | if ( !fileMsg.IsOpened() ) | |
1678ad78 | 348 | return FALSE; |
c801d85f KB |
349 | |
350 | // get the file size | |
1678ad78 GL |
351 | off_t nSize = fileMsg.Length(); |
352 | if ( nSize == wxInvalidOffset ) | |
353 | return FALSE; | |
c801d85f KB |
354 | |
355 | // read the whole file in memory | |
c86f1403 | 356 | m_pData = new size_t8[nSize]; |
c801d85f | 357 | if ( fileMsg.Read(m_pData, nSize) != nSize ) { |
a3622daa | 358 | wxDELETEA(m_pData); |
1678ad78 | 359 | return FALSE; |
c801d85f | 360 | } |
7af89395 | 361 | |
c801d85f | 362 | // examine header |
1678ad78 | 363 | bool bValid = (size_t)nSize > sizeof(wxMsgCatalogHeader); |
7af89395 | 364 | |
fd3f686c | 365 | wxMsgCatalogHeader *pHeader = (wxMsgCatalogHeader *)m_pData; |
c801d85f | 366 | if ( bValid ) { |
c801d85f KB |
367 | // we'll have to swap all the integers if it's true |
368 | m_bSwapped = pHeader->magic == MSGCATALOG_MAGIC_SW; | |
369 | ||
370 | // check the magic number | |
371 | bValid = m_bSwapped || pHeader->magic == MSGCATALOG_MAGIC; | |
372 | } | |
7af89395 | 373 | |
c801d85f KB |
374 | if ( !bValid ) { |
375 | // it's either too short or has incorrect magic number | |
1a5a8367 | 376 | wxLogWarning(_("'%s' is not a valid message catalog."), strFullName.c_str()); |
7af89395 | 377 | |
a3622daa | 378 | wxDELETEA(m_pData); |
c801d85f KB |
379 | return FALSE; |
380 | } | |
7af89395 | 381 | |
c801d85f KB |
382 | // initialize |
383 | m_numStrings = Swap(pHeader->numStrings); | |
7af89395 | 384 | m_pOrigTable = (wxMsgTableEntry *)(m_pData + |
1678ad78 | 385 | Swap(pHeader->ofsOrigTable)); |
7af89395 | 386 | m_pTransTable = (wxMsgTableEntry *)(m_pData + |
1678ad78 | 387 | Swap(pHeader->ofsTransTable)); |
c801d85f KB |
388 | |
389 | m_nHashSize = Swap(pHeader->nHashSize); | |
c86f1403 | 390 | m_pHashTable = (size_t32 *)(m_pData + Swap(pHeader->ofsHashTable)); |
c801d85f | 391 | |
e36e6f95 OK |
392 | m_pszName = new wxChar[wxStrlen(szName) + 1]; |
393 | wxStrcpy(m_pszName, szName); | |
c801d85f KB |
394 | |
395 | // everything is fine | |
396 | return TRUE; | |
397 | } | |
398 | ||
399 | // search for a string | |
400 | const char *wxMsgCatalog::GetString(const char *szOrig) const | |
401 | { | |
402 | if ( szOrig == NULL ) | |
403 | return NULL; | |
404 | ||
405 | if ( HasHashTable() ) { // use hash table for lookup if possible | |
7af89395 | 406 | size_t32 nHashVal = GetHash(szOrig); |
c86f1403 | 407 | size_t32 nIndex = nHashVal % m_nHashSize; |
c801d85f | 408 | |
c86f1403 | 409 | size_t32 nIncr = 1 + (nHashVal % (m_nHashSize - 2)); |
7af89395 | 410 | |
c801d85f | 411 | while ( TRUE ) { |
c86f1403 | 412 | size_t32 nStr = Swap(m_pHashTable[nIndex]); |
c801d85f KB |
413 | if ( nStr == 0 ) |
414 | return NULL; | |
7af89395 | 415 | |
c801d85f KB |
416 | if ( strcmp(szOrig, StringAtOfs(m_pOrigTable, nStr - 1)) == 0 ) |
417 | return StringAtOfs(m_pTransTable, nStr - 1); | |
418 | ||
419 | if ( nIndex >= m_nHashSize - nIncr) | |
420 | nIndex -= m_nHashSize - nIncr; | |
421 | else | |
422 | nIndex += nIncr; | |
423 | } | |
424 | } | |
425 | else { // no hash table: use default binary search | |
c86f1403 | 426 | size_t32 bottom = 0, |
c801d85f KB |
427 | top = m_numStrings, |
428 | current; | |
429 | while ( bottom < top ) { | |
430 | current = (bottom + top) / 2; | |
431 | int res = strcmp(szOrig, StringAtOfs(m_pOrigTable, current)); | |
432 | if ( res < 0 ) | |
433 | top = current; | |
434 | else if ( res > 0 ) | |
435 | bottom = current + 1; | |
436 | else // found! | |
437 | return StringAtOfs(m_pTransTable, current); | |
438 | } | |
439 | } | |
440 | ||
441 | // not found | |
442 | return NULL; | |
443 | } | |
444 | ||
445 | // ---------------------------------------------------------------------------- | |
446 | // wxLocale | |
447 | // ---------------------------------------------------------------------------- | |
448 | ||
23fcecf7 | 449 | wxLocale::wxLocale() |
c801d85f | 450 | { |
23fcecf7 VZ |
451 | m_pszOldLocale = NULL; |
452 | m_pMsgCat = NULL; | |
453 | } | |
454 | ||
455 | // NB: this function has (desired) side effect of changing current locale | |
e36e6f95 OK |
456 | bool wxLocale::Init(const wxChar *szName, |
457 | const wxChar *szShort, | |
458 | const wxChar *szLocale, | |
23fcecf7 VZ |
459 | bool bLoadDefault) |
460 | { | |
461 | m_strLocale = szName; | |
462 | m_strShort = szShort; | |
463 | ||
c801d85f KB |
464 | // change current locale (default: same as long name) |
465 | if ( szLocale == NULL ) | |
466 | szLocale = szName; | |
e36e6f95 | 467 | m_pszOldLocale = wxSetlocale(LC_ALL, szLocale); |
c801d85f | 468 | if ( m_pszOldLocale == NULL ) |
1a5a8367 | 469 | wxLogError(_("locale '%s' can not be set."), szLocale); |
c801d85f KB |
470 | |
471 | // the short name will be used to look for catalog files as well, | |
472 | // so we need something here | |
473 | if ( m_strShort.IsEmpty() ) { | |
fd323a5e VZ |
474 | // FIXME I don't know how these 2 letter abbreviations are formed, |
475 | // this wild guess is surely wrong | |
0fb67cd1 | 476 | m_strShort = tolower(szLocale[0]) + tolower(szLocale[1]); |
c801d85f | 477 | } |
7af89395 | 478 | |
c801d85f | 479 | // save the old locale to be able to restore it later |
7af89395 VZ |
480 | m_pOldLocale = wxSetLocale(this); |
481 | ||
c801d85f KB |
482 | // load the default catalog with wxWindows standard messages |
483 | m_pMsgCat = NULL; | |
23fcecf7 | 484 | bool bOk = TRUE; |
c801d85f | 485 | if ( bLoadDefault ) |
e36e6f95 | 486 | bOk = AddCatalog(_T("wxstd")); |
23fcecf7 VZ |
487 | |
488 | return bOk; | |
c801d85f KB |
489 | } |
490 | ||
fd323a5e VZ |
491 | void wxLocale::AddCatalogLookupPathPrefix(const wxString& prefix) |
492 | { | |
3c67202d | 493 | if ( s_searchPrefixes.Index(prefix) == wxNOT_FOUND ) |
fd323a5e VZ |
494 | { |
495 | s_searchPrefixes.Add(prefix); | |
496 | } | |
497 | //else: already have it | |
498 | } | |
499 | ||
c801d85f KB |
500 | // clean up |
501 | wxLocale::~wxLocale() | |
502 | { | |
fd323a5e VZ |
503 | // free memory |
504 | wxMsgCatalog *pTmpCat; | |
505 | while ( m_pMsgCat != NULL ) { | |
506 | pTmpCat = m_pMsgCat; | |
507 | m_pMsgCat = m_pMsgCat->m_pNext; | |
508 | delete pTmpCat; | |
509 | } | |
510 | ||
511 | // restore old locale | |
512 | wxSetLocale(m_pOldLocale); | |
e36e6f95 | 513 | wxSetlocale(LC_ALL, m_pszOldLocale); |
c801d85f KB |
514 | } |
515 | ||
516 | // get the translation of given string in current locale | |
e36e6f95 OK |
517 | const wxMB2WXbuf wxLocale::GetString(const wxChar *szOrigString, |
518 | const wxChar *szDomain) const | |
c801d85f | 519 | { |
e36e6f95 | 520 | if ( wxIsEmpty(szOrigString) ) |
dd0e574a | 521 | return szDomain; |
c801d85f KB |
522 | |
523 | const char *pszTrans = NULL; | |
dcf924a3 | 524 | const wxWX2MBbuf szOrgString = wxConvCurrent->cWX2MB(szOrigString); |
c801d85f KB |
525 | |
526 | wxMsgCatalog *pMsgCat; | |
527 | if ( szDomain != NULL ) { | |
528 | pMsgCat = FindCatalog(szDomain); | |
7af89395 | 529 | |
c801d85f KB |
530 | // does the catalog exist? |
531 | if ( pMsgCat != NULL ) | |
e36e6f95 | 532 | pszTrans = pMsgCat->GetString(szOrgString); |
c801d85f KB |
533 | } |
534 | else { | |
535 | // search in all domains | |
536 | for ( pMsgCat = m_pMsgCat; pMsgCat != NULL; pMsgCat = pMsgCat->m_pNext ) { | |
e36e6f95 | 537 | pszTrans = pMsgCat->GetString(szOrgString); |
c801d85f KB |
538 | if ( pszTrans != NULL ) // take the first found |
539 | break; | |
540 | } | |
541 | } | |
542 | ||
543 | if ( pszTrans == NULL ) { | |
544 | if ( wxIsLoggingTransErrors() ) { | |
fd323a5e VZ |
545 | // suppress further error messages if we're not debugging: this avoids |
546 | // flooding the user with messages about each and every missing string if, | |
547 | // for example, a whole catalog file is missing. | |
548 | ||
549 | // do it before calling LogWarning to prevent infinite recursion! | |
550 | #ifdef __WXDEBUG__ | |
551 | NoTransErr noTransErr; | |
552 | #else // !debug | |
c801d85f | 553 | wxSuppressTransErrors(); |
fd323a5e | 554 | #endif // debug/!debug |
7af89395 | 555 | |
c801d85f | 556 | if ( szDomain != NULL ) |
fd323a5e | 557 | { |
1a5a8367 | 558 | wxLogWarning(_("string '%s' not found in domain '%s' for locale '%s'."), |
fd323a5e VZ |
559 | szOrigString, szDomain, m_strLocale.c_str()); |
560 | } | |
c801d85f | 561 | else |
fd323a5e | 562 | { |
1a5a8367 | 563 | wxLogWarning(_("string '%s' not found in locale '%s'."), |
fd323a5e VZ |
564 | szOrigString, m_strLocale.c_str()); |
565 | } | |
c801d85f KB |
566 | } |
567 | ||
e36e6f95 | 568 | return (wxMB2WXbuf)(szOrigString); |
c801d85f KB |
569 | } |
570 | else | |
dcf924a3 | 571 | return (wxMB2WXbuf)(wxConvCurrent->cMB2WX(pszTrans)); |
c801d85f KB |
572 | } |
573 | ||
574 | // find catalog by name in a linked list, return NULL if !found | |
e36e6f95 | 575 | wxMsgCatalog *wxLocale::FindCatalog(const wxChar *szDomain) const |
c801d85f KB |
576 | { |
577 | // linear search in the linked list | |
578 | wxMsgCatalog *pMsgCat; | |
579 | for ( pMsgCat = m_pMsgCat; pMsgCat != NULL; pMsgCat = pMsgCat->m_pNext ) { | |
e36e6f95 | 580 | if ( wxStricmp(pMsgCat->GetName(), szDomain) == 0 ) |
c801d85f KB |
581 | return pMsgCat; |
582 | } | |
7af89395 | 583 | |
c801d85f KB |
584 | return NULL; |
585 | } | |
586 | ||
587 | // check if the given catalog is loaded | |
e36e6f95 | 588 | bool wxLocale::IsLoaded(const wxChar *szDomain) const |
c801d85f KB |
589 | { |
590 | return FindCatalog(szDomain) != NULL; | |
591 | } | |
592 | ||
593 | // add a catalog to our linked list | |
e36e6f95 | 594 | bool wxLocale::AddCatalog(const wxChar *szDomain) |
c801d85f KB |
595 | { |
596 | wxMsgCatalog *pMsgCat = new wxMsgCatalog; | |
7af89395 | 597 | |
c801d85f KB |
598 | if ( pMsgCat->Load(m_strShort, szDomain) ) { |
599 | // add it to the head of the list so that in GetString it will | |
600 | // be searched before the catalogs added earlier | |
601 | pMsgCat->m_pNext = m_pMsgCat; | |
602 | m_pMsgCat = pMsgCat; | |
7af89395 | 603 | |
c801d85f KB |
604 | return TRUE; |
605 | } | |
606 | else { | |
607 | // don't add it because it couldn't be loaded anyway | |
608 | delete pMsgCat; | |
7af89395 | 609 | |
c801d85f KB |
610 | return FALSE; |
611 | } | |
612 | } | |
613 | ||
614 | // ---------------------------------------------------------------------------- | |
615 | // global functions and variables | |
616 | // ---------------------------------------------------------------------------- | |
617 | ||
618 | // translation errors logging | |
619 | // -------------------------- | |
620 | ||
621 | static bool gs_bGiveTransErrors = TRUE; | |
622 | ||
623 | void wxSuppressTransErrors() | |
624 | { | |
625 | gs_bGiveTransErrors = FALSE; | |
626 | } | |
627 | ||
628 | void wxRestoreTransErrors() | |
629 | { | |
630 | gs_bGiveTransErrors = TRUE; | |
631 | } | |
632 | ||
633 | bool wxIsLoggingTransErrors() | |
634 | { | |
635 | return gs_bGiveTransErrors; | |
636 | } | |
637 | ||
638 | // retrieve/change current locale | |
639 | // ------------------------------ | |
640 | ||
641 | // the current locale object | |
84c18814 | 642 | static wxLocale *g_pLocale = NULL; |
c801d85f | 643 | |
1678ad78 GL |
644 | wxLocale *wxGetLocale() |
645 | { | |
7af89395 | 646 | return g_pLocale; |
1678ad78 GL |
647 | } |
648 | ||
c801d85f KB |
649 | wxLocale *wxSetLocale(wxLocale *pLocale) |
650 | { | |
7af89395 VZ |
651 | wxLocale *pOld = g_pLocale; |
652 | g_pLocale = pLocale; | |
653 | return pOld; | |
c801d85f | 654 | } |
d427503c VZ |
655 | |
656 | #endif // wxUSE_INTL | |
657 |