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