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