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