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