]>
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" | |
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 | 53 | typedef 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 | |
82 | size_t intsize = sizeof(int); | |
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 |
96 | const size_t32 MSGCATALOG_MAGIC = 0x950412de; |
97 | const size_t32 MSGCATALOG_MAGIC_SW = 0xde120495; | |
c801d85f KB |
98 | |
99 | // extension of ".mo" files | |
100 | #define MSGCATALOG_EXTENSION ".mo" | |
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 | 110 | void wxSuppressTransErrors(); |
c801d85f KB |
111 | |
112 | // restore the logging | |
1678ad78 | 113 | void wxRestoreTransErrors(); |
c801d85f KB |
114 | |
115 | // get the current state | |
1678ad78 | 116 | bool wxIsLoggingTransErrors(); |
c801d85f | 117 | |
84c18814 | 118 | static 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 | ||
127 | class wxMsgCatalog | |
128 | { | |
129 | public: | |
130 | // ctor & dtor | |
131 | wxMsgCatalog(); | |
132 | ~wxMsgCatalog(); | |
133 | ||
134 | // load the catalog from disk (szDirPrefix corresponds to language) | |
e36e6f95 | 135 | bool Load(const wxChar *szDirPrefix, const wxChar *szName); |
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 |
147 | private: |
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 KB |
180 | { return (const char *)(m_pData + Swap(pTable[index].ofsString)); } |
181 | ||
182 | // utility functions | |
183 | // calculate the hash value of given string | |
c86f1403 | 184 | static inline size_t32 GetHash(const char *sz); |
c801d85f | 185 | // big<->little endian |
c86f1403 | 186 | inline size_t32 Swap(size_t32 ui) const; |
c801d85f KB |
187 | |
188 | // internal state | |
189 | bool HasHashTable() const // true if hash table is present | |
190 | { return m_nHashSize > 2 && m_pHashTable != NULL; } | |
191 | ||
192 | bool m_bSwapped; // wrong endianness? | |
193 | ||
e36e6f95 | 194 | wxChar *m_pszName; // name of the domain |
c801d85f KB |
195 | }; |
196 | ||
fd323a5e VZ |
197 | // ---------------------------------------------------------------------------- |
198 | // global variables | |
199 | // ---------------------------------------------------------------------------- | |
200 | ||
201 | // the list of the directories to search for message catalog files | |
202 | static wxArrayString s_searchPrefixes; | |
203 | ||
c801d85f KB |
204 | // ============================================================================ |
205 | // implementation | |
206 | // ============================================================================ | |
207 | ||
208 | // ---------------------------------------------------------------------------- | |
209 | // wxMsgCatalog class | |
210 | // ---------------------------------------------------------------------------- | |
211 | ||
212 | // calculate hash value using the so called hashpjw function by P.J. Weinberger | |
213 | // [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools] | |
c86f1403 | 214 | size_t32 wxMsgCatalog::GetHash(const char *sz) |
c801d85f | 215 | { |
c86f1403 | 216 | #define HASHWORDBITS 32 // the length of size_t32 |
c801d85f | 217 | |
c86f1403 VZ |
218 | size_t32 hval = 0; |
219 | size_t32 g; | |
c801d85f KB |
220 | while ( *sz != '\0' ) { |
221 | hval <<= 4; | |
c86f1403 VZ |
222 | hval += (size_t32)*sz++; |
223 | g = hval & ((size_t32)0xf << (HASHWORDBITS - 4)); | |
c801d85f KB |
224 | if ( g != 0 ) { |
225 | hval ^= g >> (HASHWORDBITS - 8); | |
226 | hval ^= g; | |
227 | } | |
228 | } | |
229 | ||
230 | return hval; | |
231 | } | |
232 | ||
233 | // swap the 2 halves of 32 bit integer if needed | |
c86f1403 | 234 | size_t32 wxMsgCatalog::Swap(size_t32 ui) const |
c801d85f | 235 | { |
7af89395 | 236 | return m_bSwapped ? (ui << 24) | ((ui & 0xff00) << 8) | |
c801d85f KB |
237 | ((ui >> 8) & 0xff00) | (ui >> 24) |
238 | : ui; | |
239 | } | |
240 | ||
7af89395 VZ |
241 | wxMsgCatalog::wxMsgCatalog() |
242 | { | |
c801d85f KB |
243 | m_pData = NULL; |
244 | m_pszName = NULL; | |
245 | } | |
246 | ||
7af89395 VZ |
247 | wxMsgCatalog::~wxMsgCatalog() |
248 | { | |
249 | wxDELETEA(m_pData); | |
250 | wxDELETEA(m_pszName); | |
c801d85f KB |
251 | } |
252 | ||
fd323a5e | 253 | // small class to suppress the translation erros until exit from current scope |
c801d85f KB |
254 | class NoTransErr |
255 | { | |
fd323a5e | 256 | public: |
c801d85f KB |
257 | NoTransErr() { wxSuppressTransErrors(); } |
258 | ~NoTransErr() { wxRestoreTransErrors(); } | |
259 | }; | |
7af89395 | 260 | |
fd323a5e | 261 | // return all directories to search for given prefix |
e36e6f95 OK |
262 | static wxString GetAllMsgCatalogSubdirs(const wxChar *prefix, |
263 | const wxChar *lang) | |
fd323a5e VZ |
264 | { |
265 | wxString searchPath; | |
266 | ||
267 | // search first in prefix/fr/LC_MESSAGES, then in prefix/fr and finally in | |
268 | // prefix (assuming the language is 'fr') | |
7af89395 | 269 | searchPath << prefix << wxFILE_SEP_PATH << lang << wxFILE_SEP_PATH |
223d09f6 | 270 | << wxT("LC_MESSAGES") << wxPATH_SEP |
7af89395 VZ |
271 | << prefix << wxFILE_SEP_PATH << lang << wxPATH_SEP |
272 | << prefix << wxPATH_SEP; | |
fd323a5e VZ |
273 | |
274 | return searchPath; | |
275 | } | |
276 | ||
277 | // construct the search path for the given language | |
e36e6f95 | 278 | static wxString GetFullSearchPath(const wxChar *lang) |
fd323a5e VZ |
279 | { |
280 | wxString searchPath; | |
281 | ||
282 | // first take the entries explicitly added by the program | |
283 | size_t count = s_searchPrefixes.Count(); | |
284 | for ( size_t n = 0; n < count; n++ ) | |
285 | { | |
286 | searchPath << GetAllMsgCatalogSubdirs(s_searchPrefixes[n], lang) | |
7af89395 | 287 | << wxPATH_SEP; |
fd323a5e VZ |
288 | } |
289 | ||
290 | // then take the current directory | |
291 | // FIXME it should be the directory of the executable | |
223d09f6 | 292 | searchPath << GetAllMsgCatalogSubdirs(wxT("."), lang) << wxPATH_SEP; |
fd323a5e VZ |
293 | |
294 | // and finally add some standard ones | |
295 | searchPath | |
223d09f6 KB |
296 | << GetAllMsgCatalogSubdirs(wxT("/usr/share/locale"), lang) << wxPATH_SEP |
297 | << GetAllMsgCatalogSubdirs(wxT("/usr/lib/locale"), lang) << wxPATH_SEP | |
298 | << GetAllMsgCatalogSubdirs(wxT("/usr/local/share/locale"), lang); | |
fd323a5e VZ |
299 | |
300 | return searchPath; | |
301 | } | |
302 | ||
c801d85f | 303 | // open disk file and read in it's contents |
03443829 | 304 | bool wxMsgCatalog::Load(const wxChar *szDirPrefix, const wxChar *szName0) |
c801d85f | 305 | { |
03443829 KB |
306 | /* We need to handle locales like de_AT.iso-8859-1 |
307 | For this we first chop off the .CHARSET specifier and ignore it. | |
308 | FIXME: UNICODE SUPPORT: must use CHARSET specifier! | |
309 | */ | |
310 | wxString szName = szName0; | |
58c837a4 RR |
311 | if(szName.Find(wxT('.')) != -1) // contains a dot |
312 | szName = szName.Left(szName.Find(wxT('.'))); | |
2ce0a6e2 | 313 | |
fd323a5e | 314 | // FIXME VZ: I forgot the exact meaning of LC_PATH - anyone to remind me? |
03443829 | 315 | // KB: search path where to find the mo files, probably : delimited |
fd323a5e | 316 | #if 0 |
e36e6f95 | 317 | const wxChar *pszLcPath = wxGetenv("LC_PATH"); |
c801d85f | 318 | if ( pszLcPath != NULL ) |
fd323a5e VZ |
319 | strPath += pszLcPath + wxString(szDirPrefix) + MSG_PATH; |
320 | #endif // 0 | |
7af89395 | 321 | |
fd323a5e | 322 | wxString searchPath = GetFullSearchPath(szDirPrefix); |
223d09f6 | 323 | const wxChar *sublocale = wxStrchr(szDirPrefix, wxT('_')); |
fd323a5e VZ |
324 | if ( sublocale ) |
325 | { | |
326 | // also add just base locale name: for things like "fr_BE" (belgium | |
327 | // french) we should use "fr" if no belgium specific message catalogs | |
328 | // exist | |
329 | searchPath << GetFullSearchPath(wxString(szDirPrefix). | |
330 | Left((size_t)(sublocale - szDirPrefix))) | |
7af89395 | 331 | << wxPATH_SEP; |
fd323a5e | 332 | } |
7af89395 | 333 | |
c801d85f KB |
334 | wxString strFile = szName; |
335 | strFile += MSGCATALOG_EXTENSION; | |
336 | ||
337 | // don't give translation errors here because the wxstd catalog might | |
338 | // not yet be loaded (and it's normal) | |
339 | // | |
340 | // (we're using an object because we have several return paths) | |
ed58dbea | 341 | |
c801d85f | 342 | NoTransErr noTransErr; |
223d09f6 | 343 | wxLogVerbose(wxT("looking for catalog '%s' in path '%s'."), |
742af071 | 344 | szName.c_str(), searchPath.c_str()); |
c801d85f KB |
345 | |
346 | wxString strFullName; | |
fd323a5e | 347 | if ( !wxFindFileInPath(&strFullName, searchPath, strFile) ) { |
ed58dbea | 348 | wxLogWarning(_("catalog file for domain '%s' not found."), szName.c_str()); |
c801d85f KB |
349 | return FALSE; |
350 | } | |
351 | ||
352 | // open file | |
1a5a8367 | 353 | wxLogVerbose(_("using catalog '%s' from '%s'."), |
ed58dbea | 354 | szName.c_str(), strFullName.c_str()); |
7af89395 | 355 | |
c801d85f KB |
356 | wxFile fileMsg(strFullName); |
357 | if ( !fileMsg.IsOpened() ) | |
1678ad78 | 358 | return FALSE; |
c801d85f KB |
359 | |
360 | // get the file size | |
1678ad78 GL |
361 | off_t nSize = fileMsg.Length(); |
362 | if ( nSize == wxInvalidOffset ) | |
363 | return FALSE; | |
c801d85f KB |
364 | |
365 | // read the whole file in memory | |
c86f1403 | 366 | m_pData = new size_t8[nSize]; |
c801d85f | 367 | if ( fileMsg.Read(m_pData, nSize) != nSize ) { |
a3622daa | 368 | wxDELETEA(m_pData); |
1678ad78 | 369 | return FALSE; |
c801d85f | 370 | } |
7af89395 | 371 | |
c801d85f | 372 | // examine header |
1678ad78 | 373 | bool bValid = (size_t)nSize > sizeof(wxMsgCatalogHeader); |
7af89395 | 374 | |
fd3f686c | 375 | wxMsgCatalogHeader *pHeader = (wxMsgCatalogHeader *)m_pData; |
c801d85f | 376 | if ( bValid ) { |
c801d85f KB |
377 | // we'll have to swap all the integers if it's true |
378 | m_bSwapped = pHeader->magic == MSGCATALOG_MAGIC_SW; | |
379 | ||
380 | // check the magic number | |
381 | bValid = m_bSwapped || pHeader->magic == MSGCATALOG_MAGIC; | |
382 | } | |
7af89395 | 383 | |
c801d85f KB |
384 | if ( !bValid ) { |
385 | // it's either too short or has incorrect magic number | |
1a5a8367 | 386 | wxLogWarning(_("'%s' is not a valid message catalog."), strFullName.c_str()); |
7af89395 | 387 | |
a3622daa | 388 | wxDELETEA(m_pData); |
c801d85f KB |
389 | return FALSE; |
390 | } | |
7af89395 | 391 | |
c801d85f KB |
392 | // initialize |
393 | m_numStrings = Swap(pHeader->numStrings); | |
7af89395 | 394 | m_pOrigTable = (wxMsgTableEntry *)(m_pData + |
1678ad78 | 395 | Swap(pHeader->ofsOrigTable)); |
7af89395 | 396 | m_pTransTable = (wxMsgTableEntry *)(m_pData + |
1678ad78 | 397 | Swap(pHeader->ofsTransTable)); |
c801d85f KB |
398 | |
399 | m_nHashSize = Swap(pHeader->nHashSize); | |
c86f1403 | 400 | m_pHashTable = (size_t32 *)(m_pData + Swap(pHeader->ofsHashTable)); |
c801d85f | 401 | |
e36e6f95 OK |
402 | m_pszName = new wxChar[wxStrlen(szName) + 1]; |
403 | wxStrcpy(m_pszName, szName); | |
c801d85f KB |
404 | |
405 | // everything is fine | |
406 | return TRUE; | |
407 | } | |
408 | ||
409 | // search for a string | |
410 | const char *wxMsgCatalog::GetString(const char *szOrig) const | |
411 | { | |
412 | if ( szOrig == NULL ) | |
413 | return NULL; | |
414 | ||
415 | if ( HasHashTable() ) { // use hash table for lookup if possible | |
7af89395 | 416 | size_t32 nHashVal = GetHash(szOrig); |
c86f1403 | 417 | size_t32 nIndex = nHashVal % m_nHashSize; |
c801d85f | 418 | |
c86f1403 | 419 | size_t32 nIncr = 1 + (nHashVal % (m_nHashSize - 2)); |
7af89395 | 420 | |
2ce0a6e2 DW |
421 | #if defined(__VISAGECPP__) |
422 | // VA just can't stand while(1) or while(TRUE) | |
423 | bool bOs2var = TRUE; | |
424 | while(bOs2var) { | |
425 | #else | |
426 | while (1) { | |
427 | #endif | |
c86f1403 | 428 | size_t32 nStr = Swap(m_pHashTable[nIndex]); |
c801d85f KB |
429 | if ( nStr == 0 ) |
430 | return NULL; | |
7af89395 | 431 | |
7aa733b3 | 432 | if ( strcmp(szOrig, StringAtOfs(m_pOrigTable, nStr - 1)) == 0 ) |
c801d85f KB |
433 | return StringAtOfs(m_pTransTable, nStr - 1); |
434 | ||
435 | if ( nIndex >= m_nHashSize - nIncr) | |
436 | nIndex -= m_nHashSize - nIncr; | |
437 | else | |
438 | nIndex += nIncr; | |
439 | } | |
440 | } | |
441 | else { // no hash table: use default binary search | |
c86f1403 | 442 | size_t32 bottom = 0, |
c801d85f KB |
443 | top = m_numStrings, |
444 | current; | |
445 | while ( bottom < top ) { | |
446 | current = (bottom + top) / 2; | |
7aa733b3 | 447 | int res = strcmp(szOrig, StringAtOfs(m_pOrigTable, current)); |
c801d85f KB |
448 | if ( res < 0 ) |
449 | top = current; | |
450 | else if ( res > 0 ) | |
451 | bottom = current + 1; | |
452 | else // found! | |
453 | return StringAtOfs(m_pTransTable, current); | |
454 | } | |
455 | } | |
456 | ||
457 | // not found | |
458 | return NULL; | |
459 | } | |
460 | ||
461 | // ---------------------------------------------------------------------------- | |
462 | // wxLocale | |
463 | // ---------------------------------------------------------------------------- | |
464 | ||
23fcecf7 | 465 | wxLocale::wxLocale() |
c801d85f | 466 | { |
23fcecf7 VZ |
467 | m_pszOldLocale = NULL; |
468 | m_pMsgCat = NULL; | |
469 | } | |
470 | ||
471 | // NB: this function has (desired) side effect of changing current locale | |
e36e6f95 OK |
472 | bool wxLocale::Init(const wxChar *szName, |
473 | const wxChar *szShort, | |
474 | const wxChar *szLocale, | |
23fcecf7 VZ |
475 | bool bLoadDefault) |
476 | { | |
477 | m_strLocale = szName; | |
478 | m_strShort = szShort; | |
479 | ||
c801d85f KB |
480 | // change current locale (default: same as long name) |
481 | if ( szLocale == NULL ) | |
9dea50fc VZ |
482 | { |
483 | // the argument to setlocale() | |
484 | szLocale = szShort; | |
485 | } | |
e36e6f95 | 486 | m_pszOldLocale = wxSetlocale(LC_ALL, szLocale); |
c801d85f | 487 | if ( m_pszOldLocale == NULL ) |
1a5a8367 | 488 | wxLogError(_("locale '%s' can not be set."), szLocale); |
c801d85f KB |
489 | |
490 | // the short name will be used to look for catalog files as well, | |
491 | // so we need something here | |
492 | if ( m_strShort.IsEmpty() ) { | |
fd323a5e VZ |
493 | // FIXME I don't know how these 2 letter abbreviations are formed, |
494 | // this wild guess is surely wrong | |
0fb67cd1 | 495 | m_strShort = tolower(szLocale[0]) + tolower(szLocale[1]); |
c801d85f | 496 | } |
7af89395 | 497 | |
c801d85f | 498 | // save the old locale to be able to restore it later |
7af89395 VZ |
499 | m_pOldLocale = wxSetLocale(this); |
500 | ||
c801d85f KB |
501 | // load the default catalog with wxWindows standard messages |
502 | m_pMsgCat = NULL; | |
23fcecf7 | 503 | bool bOk = TRUE; |
c801d85f | 504 | if ( bLoadDefault ) |
223d09f6 | 505 | bOk = AddCatalog(wxT("wxstd")); |
23fcecf7 VZ |
506 | |
507 | return bOk; | |
c801d85f KB |
508 | } |
509 | ||
fd323a5e VZ |
510 | void wxLocale::AddCatalogLookupPathPrefix(const wxString& prefix) |
511 | { | |
3c67202d | 512 | if ( s_searchPrefixes.Index(prefix) == wxNOT_FOUND ) |
fd323a5e VZ |
513 | { |
514 | s_searchPrefixes.Add(prefix); | |
515 | } | |
516 | //else: already have it | |
517 | } | |
518 | ||
c801d85f KB |
519 | // clean up |
520 | wxLocale::~wxLocale() | |
521 | { | |
fd323a5e VZ |
522 | // free memory |
523 | wxMsgCatalog *pTmpCat; | |
524 | while ( m_pMsgCat != NULL ) { | |
525 | pTmpCat = m_pMsgCat; | |
526 | m_pMsgCat = m_pMsgCat->m_pNext; | |
527 | delete pTmpCat; | |
528 | } | |
529 | ||
530 | // restore old locale | |
531 | wxSetLocale(m_pOldLocale); | |
e36e6f95 | 532 | wxSetlocale(LC_ALL, m_pszOldLocale); |
c801d85f KB |
533 | } |
534 | ||
535 | // get the translation of given string in current locale | |
e36e6f95 | 536 | const wxMB2WXbuf wxLocale::GetString(const wxChar *szOrigString, |
e90c1d2a | 537 | const wxChar *szDomain) const |
c801d85f | 538 | { |
e36e6f95 | 539 | if ( wxIsEmpty(szOrigString) ) |
dd0e574a | 540 | return szDomain; |
c801d85f KB |
541 | |
542 | const char *pszTrans = NULL; | |
e90c1d2a | 543 | #if wxUSE_UNICODE |
dcf924a3 | 544 | const wxWX2MBbuf szOrgString = wxConvCurrent->cWX2MB(szOrigString); |
e90c1d2a VZ |
545 | #else // ANSI |
546 | #define szOrgString szOrigString | |
547 | #endif // Unicode/ANSI | |
c801d85f KB |
548 | |
549 | wxMsgCatalog *pMsgCat; | |
550 | if ( szDomain != NULL ) { | |
551 | pMsgCat = FindCatalog(szDomain); | |
7af89395 | 552 | |
c801d85f KB |
553 | // does the catalog exist? |
554 | if ( pMsgCat != NULL ) | |
e36e6f95 | 555 | pszTrans = pMsgCat->GetString(szOrgString); |
c801d85f KB |
556 | } |
557 | else { | |
558 | // search in all domains | |
559 | for ( pMsgCat = m_pMsgCat; pMsgCat != NULL; pMsgCat = pMsgCat->m_pNext ) { | |
e36e6f95 | 560 | pszTrans = pMsgCat->GetString(szOrgString); |
c801d85f KB |
561 | if ( pszTrans != NULL ) // take the first found |
562 | break; | |
563 | } | |
564 | } | |
565 | ||
566 | if ( pszTrans == NULL ) { | |
567 | if ( wxIsLoggingTransErrors() ) { | |
fd323a5e VZ |
568 | // suppress further error messages if we're not debugging: this avoids |
569 | // flooding the user with messages about each and every missing string if, | |
570 | // for example, a whole catalog file is missing. | |
571 | ||
572 | // do it before calling LogWarning to prevent infinite recursion! | |
573 | #ifdef __WXDEBUG__ | |
574 | NoTransErr noTransErr; | |
575 | #else // !debug | |
c801d85f | 576 | wxSuppressTransErrors(); |
fd323a5e | 577 | #endif // debug/!debug |
7af89395 | 578 | |
c801d85f | 579 | if ( szDomain != NULL ) |
fd323a5e | 580 | { |
1a5a8367 | 581 | wxLogWarning(_("string '%s' not found in domain '%s' for locale '%s'."), |
fd323a5e VZ |
582 | szOrigString, szDomain, m_strLocale.c_str()); |
583 | } | |
c801d85f | 584 | else |
fd323a5e | 585 | { |
1a5a8367 | 586 | wxLogWarning(_("string '%s' not found in locale '%s'."), |
fd323a5e VZ |
587 | szOrigString, m_strLocale.c_str()); |
588 | } | |
c801d85f KB |
589 | } |
590 | ||
e36e6f95 | 591 | return (wxMB2WXbuf)(szOrigString); |
c801d85f KB |
592 | } |
593 | else | |
e90c1d2a | 594 | { |
f6e9f05f OK |
595 | return wxConvertMB2WX(pszTrans); // or preferably wxCSConv(charset).cMB2WX(pszTrans) or something, |
596 | // a macro similar to wxConvertMB2WX could be written for that | |
e90c1d2a VZ |
597 | } |
598 | ||
599 | #undef szOrgString | |
c801d85f KB |
600 | } |
601 | ||
602 | // find catalog by name in a linked list, return NULL if !found | |
e36e6f95 | 603 | wxMsgCatalog *wxLocale::FindCatalog(const wxChar *szDomain) const |
c801d85f KB |
604 | { |
605 | // linear search in the linked list | |
606 | wxMsgCatalog *pMsgCat; | |
607 | for ( pMsgCat = m_pMsgCat; pMsgCat != NULL; pMsgCat = pMsgCat->m_pNext ) { | |
e36e6f95 | 608 | if ( wxStricmp(pMsgCat->GetName(), szDomain) == 0 ) |
c801d85f KB |
609 | return pMsgCat; |
610 | } | |
7af89395 | 611 | |
c801d85f KB |
612 | return NULL; |
613 | } | |
614 | ||
615 | // check if the given catalog is loaded | |
e36e6f95 | 616 | bool wxLocale::IsLoaded(const wxChar *szDomain) const |
c801d85f KB |
617 | { |
618 | return FindCatalog(szDomain) != NULL; | |
619 | } | |
620 | ||
621 | // add a catalog to our linked list | |
e36e6f95 | 622 | bool wxLocale::AddCatalog(const wxChar *szDomain) |
c801d85f KB |
623 | { |
624 | wxMsgCatalog *pMsgCat = new wxMsgCatalog; | |
7af89395 | 625 | |
c801d85f KB |
626 | if ( pMsgCat->Load(m_strShort, szDomain) ) { |
627 | // add it to the head of the list so that in GetString it will | |
628 | // be searched before the catalogs added earlier | |
629 | pMsgCat->m_pNext = m_pMsgCat; | |
630 | m_pMsgCat = pMsgCat; | |
7af89395 | 631 | |
c801d85f KB |
632 | return TRUE; |
633 | } | |
634 | else { | |
635 | // don't add it because it couldn't be loaded anyway | |
636 | delete pMsgCat; | |
7af89395 | 637 | |
c801d85f KB |
638 | return FALSE; |
639 | } | |
640 | } | |
641 | ||
642 | // ---------------------------------------------------------------------------- | |
643 | // global functions and variables | |
644 | // ---------------------------------------------------------------------------- | |
645 | ||
646 | // translation errors logging | |
647 | // -------------------------- | |
648 | ||
649 | static bool gs_bGiveTransErrors = TRUE; | |
650 | ||
651 | void wxSuppressTransErrors() | |
652 | { | |
653 | gs_bGiveTransErrors = FALSE; | |
654 | } | |
655 | ||
656 | void wxRestoreTransErrors() | |
657 | { | |
658 | gs_bGiveTransErrors = TRUE; | |
659 | } | |
660 | ||
661 | bool wxIsLoggingTransErrors() | |
662 | { | |
663 | return gs_bGiveTransErrors; | |
664 | } | |
665 | ||
666 | // retrieve/change current locale | |
667 | // ------------------------------ | |
668 | ||
669 | // the current locale object | |
84c18814 | 670 | static wxLocale *g_pLocale = NULL; |
c801d85f | 671 | |
1678ad78 GL |
672 | wxLocale *wxGetLocale() |
673 | { | |
7af89395 | 674 | return g_pLocale; |
1678ad78 GL |
675 | } |
676 | ||
c801d85f KB |
677 | wxLocale *wxSetLocale(wxLocale *pLocale) |
678 | { | |
7af89395 VZ |
679 | wxLocale *pOld = g_pLocale; |
680 | g_pLocale = pLocale; | |
681 | return pOld; | |
c801d85f | 682 | } |
d427503c VZ |
683 | |
684 | #endif // wxUSE_INTL | |
685 |