]> git.saurik.com Git - wxWidgets.git/blob - src/common/intl.cpp
Added missing wxGetFullHostName() and wxNO_DEFAULT for generic msgdialog
[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 #include <ctype.h>
34
35 // wxWindows
36 #include "wx/defs.h"
37 #include "wx/string.h"
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
45 // ----------------------------------------------------------------------------
46 // simple types
47 // ----------------------------------------------------------------------------
48
49 // this should *not* be wxChar, this type must have exactly 8 bits!
50 typedef unsigned char size_t8;
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
83
84 // ----------------------------------------------------------------------------
85 // constants
86 // ----------------------------------------------------------------------------
87
88 // magic number identifying the .mo format file
89 const size_t32 MSGCATALOG_MAGIC = 0x950412de;
90 const size_t32 MSGCATALOG_MAGIC_SW = 0xde120495;
91
92 // extension of ".mo" files
93 #define MSGCATALOG_EXTENSION ".mo"
94
95 // ----------------------------------------------------------------------------
96 // global functions
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)
103 void wxSuppressTransErrors();
104
105 // restore the logging
106 void wxRestoreTransErrors();
107
108 // get the current state
109 bool wxIsLoggingTransErrors();
110
111 static wxLocale *wxSetLocale(wxLocale *pLocale);
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)
128 bool Load(const wxChar *szDirPrefix, const wxChar *szName);
129 bool IsLoaded() const { return m_pData != NULL; }
130
131 // get name of the catalog
132 const wxChar *GetName() const { return m_pszName; }
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;
139
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 {
146 size_t32 nLen; // length of the string
147 size_t32 ofsString; // pointer to the string
148 };
149
150 // header of a .mo file
151 struct wxMsgCatalogHeader
152 {
153 size_t32 magic, // offset +00: magic id
154 revision, // +04: revision
155 numStrings; // +08: number of strings in the file
156 size_t32 ofsOrigTable, // +0C: start of original string table
157 ofsTransTable; // +10: start of translated string table
158 size_t32 nHashSize, // +14: hash table size
159 ofsHashTable; // +18: offset of hash table start
160 };
161
162 // all data is stored here, NULL if no data loaded
163 size_t8 *m_pData;
164
165 // data description
166 size_t32 m_numStrings, // number of strings in this domain
167 m_nHashSize; // number of entries in hash table
168 size_t32 *m_pHashTable; // pointer to hash table
169 wxMsgTableEntry *m_pOrigTable, // pointer to original strings
170 *m_pTransTable; // translated
171
172 const char *StringAtOfs(wxMsgTableEntry *pTable, size_t32 index) const
173 { return (const char *)(m_pData + Swap(pTable[index].ofsString)); }
174
175 // utility functions
176 // calculate the hash value of given string
177 static inline size_t32 GetHash(const char *sz);
178 // big<->little endian
179 inline size_t32 Swap(size_t32 ui) const;
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
187 wxChar *m_pszName; // name of the domain
188 };
189
190 // ----------------------------------------------------------------------------
191 // global variables
192 // ----------------------------------------------------------------------------
193
194 // the list of the directories to search for message catalog files
195 static wxArrayString s_searchPrefixes;
196
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]
207 size_t32 wxMsgCatalog::GetHash(const char *sz)
208 {
209 #define HASHWORDBITS 32 // the length of size_t32
210
211 size_t32 hval = 0;
212 size_t32 g;
213 while ( *sz != '\0' ) {
214 hval <<= 4;
215 hval += (size_t32)*sz++;
216 g = hval & ((size_t32)0xf << (HASHWORDBITS - 4));
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
227 size_t32 wxMsgCatalog::Swap(size_t32 ui) const
228 {
229 return m_bSwapped ? (ui << 24) | ((ui & 0xff00) << 8) |
230 ((ui >> 8) & 0xff00) | (ui >> 24)
231 : ui;
232 }
233
234 wxMsgCatalog::wxMsgCatalog()
235 {
236 m_pData = NULL;
237 m_pszName = NULL;
238 }
239
240 wxMsgCatalog::~wxMsgCatalog()
241 {
242 wxDELETEA(m_pData);
243 wxDELETEA(m_pszName);
244 }
245
246 // small class to suppress the translation erros until exit from current scope
247 class NoTransErr
248 {
249 public:
250 NoTransErr() { wxSuppressTransErrors(); }
251 ~NoTransErr() { wxRestoreTransErrors(); }
252 };
253
254 // return all directories to search for given prefix
255 static wxString GetAllMsgCatalogSubdirs(const wxChar *prefix,
256 const wxChar *lang)
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')
262 searchPath << prefix << wxFILE_SEP_PATH << lang << wxFILE_SEP_PATH
263 << _T("LC_MESSAGES") << wxPATH_SEP
264 << prefix << wxFILE_SEP_PATH << lang << wxPATH_SEP
265 << prefix << wxPATH_SEP;
266
267 return searchPath;
268 }
269
270 // construct the search path for the given language
271 static wxString GetFullSearchPath(const wxChar *lang)
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)
280 << wxPATH_SEP;
281 }
282
283 // then take the current directory
284 // FIXME it should be the directory of the executable
285 searchPath << GetAllMsgCatalogSubdirs(_T("."), lang) << wxPATH_SEP;
286
287 // and finally add some standard ones
288 searchPath
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);
292
293 return searchPath;
294 }
295
296 // open disk file and read in it's contents
297 bool wxMsgCatalog::Load(const wxChar *szDirPrefix, const wxChar *szName)
298 {
299 // FIXME VZ: I forgot the exact meaning of LC_PATH - anyone to remind me?
300 #if 0
301 const wxChar *pszLcPath = wxGetenv("LC_PATH");
302 if ( pszLcPath != NULL )
303 strPath += pszLcPath + wxString(szDirPrefix) + MSG_PATH;
304 #endif // 0
305
306 wxString searchPath = GetFullSearchPath(szDirPrefix);
307 const wxChar *sublocale = wxStrchr(szDirPrefix, _T('_'));
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)))
315 << wxPATH_SEP;
316 }
317
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
327 wxLogVerbose(_("looking for catalog '%s' in path '%s'."),
328 szName, searchPath.c_str());
329
330 wxString strFullName;
331 if ( !wxFindFileInPath(&strFullName, searchPath, strFile) ) {
332 wxLogWarning(_("catalog file for domain '%s' not found."), szName);
333 return FALSE;
334 }
335
336 // open file
337 wxLogVerbose(_("using catalog '%s' from '%s'."),
338 szName, strFullName.c_str());
339
340 wxFile fileMsg(strFullName);
341 if ( !fileMsg.IsOpened() )
342 return FALSE;
343
344 // get the file size
345 off_t nSize = fileMsg.Length();
346 if ( nSize == wxInvalidOffset )
347 return FALSE;
348
349 // read the whole file in memory
350 m_pData = new size_t8[nSize];
351 if ( fileMsg.Read(m_pData, nSize) != nSize ) {
352 wxDELETEA(m_pData);
353 return FALSE;
354 }
355
356 // examine header
357 bool bValid = (size_t)nSize > sizeof(wxMsgCatalogHeader);
358
359 wxMsgCatalogHeader *pHeader = (wxMsgCatalogHeader *)m_pData;
360 if ( bValid ) {
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 }
367
368 if ( !bValid ) {
369 // it's either too short or has incorrect magic number
370 wxLogWarning(_("'%s' is not a valid message catalog."), strFullName.c_str());
371
372 wxDELETEA(m_pData);
373 return FALSE;
374 }
375
376 // initialize
377 m_numStrings = Swap(pHeader->numStrings);
378 m_pOrigTable = (wxMsgTableEntry *)(m_pData +
379 Swap(pHeader->ofsOrigTable));
380 m_pTransTable = (wxMsgTableEntry *)(m_pData +
381 Swap(pHeader->ofsTransTable));
382
383 m_nHashSize = Swap(pHeader->nHashSize);
384 m_pHashTable = (size_t32 *)(m_pData + Swap(pHeader->ofsHashTable));
385
386 m_pszName = new wxChar[wxStrlen(szName) + 1];
387 wxStrcpy(m_pszName, szName);
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
400 size_t32 nHashVal = GetHash(szOrig);
401 size_t32 nIndex = nHashVal % m_nHashSize;
402
403 size_t32 nIncr = 1 + (nHashVal % (m_nHashSize - 2));
404
405 while ( TRUE ) {
406 size_t32 nStr = Swap(m_pHashTable[nIndex]);
407 if ( nStr == 0 )
408 return NULL;
409
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
420 size_t32 bottom = 0,
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
443 wxLocale::wxLocale()
444 {
445 m_pszOldLocale = NULL;
446 m_pMsgCat = NULL;
447 }
448
449 // NB: this function has (desired) side effect of changing current locale
450 bool wxLocale::Init(const wxChar *szName,
451 const wxChar *szShort,
452 const wxChar *szLocale,
453 bool bLoadDefault)
454 {
455 m_strLocale = szName;
456 m_strShort = szShort;
457
458 // change current locale (default: same as long name)
459 if ( szLocale == NULL )
460 szLocale = szName;
461 m_pszOldLocale = wxSetlocale(LC_ALL, szLocale);
462 if ( m_pszOldLocale == NULL )
463 wxLogError(_("locale '%s' can not be set."), szLocale);
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() ) {
468 // FIXME I don't know how these 2 letter abbreviations are formed,
469 // this wild guess is surely wrong
470 m_strShort = tolower(szLocale[0]) + tolower(szLocale[1]);
471 }
472
473 // save the old locale to be able to restore it later
474 m_pOldLocale = wxSetLocale(this);
475
476 // load the default catalog with wxWindows standard messages
477 m_pMsgCat = NULL;
478 bool bOk = TRUE;
479 if ( bLoadDefault )
480 bOk = AddCatalog(_T("wxstd"));
481
482 return bOk;
483 }
484
485 void wxLocale::AddCatalogLookupPathPrefix(const wxString& prefix)
486 {
487 if ( s_searchPrefixes.Index(prefix) == wxNOT_FOUND )
488 {
489 s_searchPrefixes.Add(prefix);
490 }
491 //else: already have it
492 }
493
494 // clean up
495 wxLocale::~wxLocale()
496 {
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);
507 wxSetlocale(LC_ALL, m_pszOldLocale);
508 }
509
510 // get the translation of given string in current locale
511 const wxMB2WXbuf wxLocale::GetString(const wxChar *szOrigString,
512 const wxChar *szDomain) const
513 {
514 if ( wxIsEmpty(szOrigString) )
515 return szDomain;
516
517 const char *pszTrans = NULL;
518 const wxWX2MBbuf szOrgString = wxConv_libc.cWX2MB(szOrigString);
519
520 wxMsgCatalog *pMsgCat;
521 if ( szDomain != NULL ) {
522 pMsgCat = FindCatalog(szDomain);
523
524 // does the catalog exist?
525 if ( pMsgCat != NULL )
526 pszTrans = pMsgCat->GetString(szOrgString);
527 }
528 else {
529 // search in all domains
530 for ( pMsgCat = m_pMsgCat; pMsgCat != NULL; pMsgCat = pMsgCat->m_pNext ) {
531 pszTrans = pMsgCat->GetString(szOrgString);
532 if ( pszTrans != NULL ) // take the first found
533 break;
534 }
535 }
536
537 if ( pszTrans == NULL ) {
538 if ( wxIsLoggingTransErrors() ) {
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
547 wxSuppressTransErrors();
548 #endif // debug/!debug
549
550 if ( szDomain != NULL )
551 {
552 wxLogWarning(_("string '%s' not found in domain '%s' for locale '%s'."),
553 szOrigString, szDomain, m_strLocale.c_str());
554 }
555 else
556 {
557 wxLogWarning(_("string '%s' not found in locale '%s'."),
558 szOrigString, m_strLocale.c_str());
559 }
560 }
561
562 return (wxMB2WXbuf)(szOrigString);
563 }
564 else
565 return (wxMB2WXbuf)(wxConv_libc.cMB2WX(pszTrans));
566 }
567
568 // find catalog by name in a linked list, return NULL if !found
569 wxMsgCatalog *wxLocale::FindCatalog(const wxChar *szDomain) const
570 {
571 // linear search in the linked list
572 wxMsgCatalog *pMsgCat;
573 for ( pMsgCat = m_pMsgCat; pMsgCat != NULL; pMsgCat = pMsgCat->m_pNext ) {
574 if ( wxStricmp(pMsgCat->GetName(), szDomain) == 0 )
575 return pMsgCat;
576 }
577
578 return NULL;
579 }
580
581 // check if the given catalog is loaded
582 bool wxLocale::IsLoaded(const wxChar *szDomain) const
583 {
584 return FindCatalog(szDomain) != NULL;
585 }
586
587 // add a catalog to our linked list
588 bool wxLocale::AddCatalog(const wxChar *szDomain)
589 {
590 wxMsgCatalog *pMsgCat = new wxMsgCatalog;
591
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;
597
598 return TRUE;
599 }
600 else {
601 // don't add it because it couldn't be loaded anyway
602 delete pMsgCat;
603
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
636 static wxLocale *g_pLocale = NULL;
637
638 wxLocale *wxGetLocale()
639 {
640 return g_pLocale;
641 }
642
643 wxLocale *wxSetLocale(wxLocale *pLocale)
644 {
645 wxLocale *pOld = g_pLocale;
646 g_pLocale = pLocale;
647 return pOld;
648 }