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