1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Internationalization and localisation for wxWindows
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "intl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
39 #include "wx/string.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // this should *not* be wxChar, this type must have exactly 8 bits!
52 typedef unsigned char size_t8
;
55 #if defined(__WIN16__)
56 typedef unsigned long size_t32
;
57 #elif defined(__WIN32__)
58 typedef unsigned int size_t32
;
60 // Win64 will have different type sizes
61 #error "Please define a 32 bit type"
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
;
70 // assume sizeof(int) == 4 - what else can we do
71 typedef unsigned int size_t32
;
73 // ... but at least check it during run time
74 static class IntSizeChecker
79 wxASSERT_MSG( sizeof(int) == 4,
80 "size_t32 is incorrectly defined!" );
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // magic number identifying the .mo format file
91 const size_t32 MSGCATALOG_MAGIC
= 0x950412de;
92 const size_t32 MSGCATALOG_MAGIC_SW
= 0xde120495;
94 // extension of ".mo" files
95 #define MSGCATALOG_EXTENSION ".mo"
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 // suppress further error messages about missing translations
102 // (if you don't have one catalog file, you wouldn't like to see the
103 // error message for each string in it, so normally it's given only
105 void wxSuppressTransErrors();
107 // restore the logging
108 void wxRestoreTransErrors();
110 // get the current state
111 bool wxIsLoggingTransErrors();
113 static wxLocale
*wxSetLocale(wxLocale
*pLocale
);
115 // ----------------------------------------------------------------------------
116 // wxMsgCatalog corresponds to one disk-file message catalog.
118 // This is a "low-level" class and is used only by wxLocale (that's why
119 // it's designed to be stored in a linked list)
120 // ----------------------------------------------------------------------------
129 // load the catalog from disk (szDirPrefix corresponds to language)
130 bool Load(const wxChar
*szDirPrefix
, const wxChar
*szName
);
131 bool IsLoaded() const { return m_pData
!= NULL
; }
133 // get name of the catalog
134 const wxChar
*GetName() const { return m_pszName
; }
136 // get the translated string: returns NULL if not found
137 const char *GetString(const char *sz
) const;
139 // public variable pointing to the next element in a linked list (or NULL)
140 wxMsgCatalog
*m_pNext
;
143 // this implementation is binary compatible with GNU gettext() version 0.10
145 // an entry in the string table
146 struct wxMsgTableEntry
148 size_t32 nLen
; // length of the string
149 size_t32 ofsString
; // pointer to the string
152 // header of a .mo file
153 struct wxMsgCatalogHeader
155 size_t32 magic
, // offset +00: magic id
156 revision
, // +04: revision
157 numStrings
; // +08: number of strings in the file
158 size_t32 ofsOrigTable
, // +0C: start of original string table
159 ofsTransTable
; // +10: start of translated string table
160 size_t32 nHashSize
, // +14: hash table size
161 ofsHashTable
; // +18: offset of hash table start
164 // all data is stored here, NULL if no data loaded
168 size_t32 m_numStrings
, // number of strings in this domain
169 m_nHashSize
; // number of entries in hash table
170 size_t32
*m_pHashTable
; // pointer to hash table
171 wxMsgTableEntry
*m_pOrigTable
, // pointer to original strings
172 *m_pTransTable
; // translated
174 const char *StringAtOfs(wxMsgTableEntry
*pTable
, size_t32 index
) const
175 { return (const char *)(m_pData
+ Swap(pTable
[index
].ofsString
)); }
178 // calculate the hash value of given string
179 static inline size_t32
GetHash(const char *sz
);
180 // big<->little endian
181 inline size_t32
Swap(size_t32 ui
) const;
184 bool HasHashTable() const // true if hash table is present
185 { return m_nHashSize
> 2 && m_pHashTable
!= NULL
; }
187 bool m_bSwapped
; // wrong endianness?
189 wxChar
*m_pszName
; // name of the domain
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 // the list of the directories to search for message catalog files
197 static wxArrayString s_searchPrefixes
;
199 // ============================================================================
201 // ============================================================================
203 // ----------------------------------------------------------------------------
204 // wxMsgCatalog class
205 // ----------------------------------------------------------------------------
207 // calculate hash value using the so called hashpjw function by P.J. Weinberger
208 // [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools]
209 size_t32
wxMsgCatalog::GetHash(const char *sz
)
211 #define HASHWORDBITS 32 // the length of size_t32
215 while ( *sz
!= '\0' ) {
217 hval
+= (size_t32
)*sz
++;
218 g
= hval
& ((size_t32
)0xf << (HASHWORDBITS
- 4));
220 hval
^= g
>> (HASHWORDBITS
- 8);
228 // swap the 2 halves of 32 bit integer if needed
229 size_t32
wxMsgCatalog::Swap(size_t32 ui
) const
231 return m_bSwapped
? (ui
<< 24) | ((ui
& 0xff00) << 8) |
232 ((ui
>> 8) & 0xff00) | (ui
>> 24)
236 wxMsgCatalog::wxMsgCatalog()
242 wxMsgCatalog::~wxMsgCatalog()
245 wxDELETEA(m_pszName
);
248 // small class to suppress the translation erros until exit from current scope
252 NoTransErr() { wxSuppressTransErrors(); }
253 ~NoTransErr() { wxRestoreTransErrors(); }
256 // return all directories to search for given prefix
257 static wxString
GetAllMsgCatalogSubdirs(const wxChar
*prefix
,
262 // search first in prefix/fr/LC_MESSAGES, then in prefix/fr and finally in
263 // prefix (assuming the language is 'fr')
264 searchPath
<< prefix
<< wxFILE_SEP_PATH
<< lang
<< wxFILE_SEP_PATH
265 << _T("LC_MESSAGES") << wxPATH_SEP
266 << prefix
<< wxFILE_SEP_PATH
<< lang
<< wxPATH_SEP
267 << prefix
<< wxPATH_SEP
;
272 // construct the search path for the given language
273 static wxString
GetFullSearchPath(const wxChar
*lang
)
277 // first take the entries explicitly added by the program
278 size_t count
= s_searchPrefixes
.Count();
279 for ( size_t n
= 0; n
< count
; n
++ )
281 searchPath
<< GetAllMsgCatalogSubdirs(s_searchPrefixes
[n
], lang
)
285 // then take the current directory
286 // FIXME it should be the directory of the executable
287 searchPath
<< GetAllMsgCatalogSubdirs(_T("."), lang
) << wxPATH_SEP
;
289 // and finally add some standard ones
291 << GetAllMsgCatalogSubdirs(_T("/usr/share/locale"), lang
) << wxPATH_SEP
292 << GetAllMsgCatalogSubdirs(_T("/usr/lib/locale"), lang
) << wxPATH_SEP
293 << GetAllMsgCatalogSubdirs(_T("/usr/local/share/locale"), lang
);
298 // open disk file and read in it's contents
299 bool wxMsgCatalog::Load(const wxChar
*szDirPrefix
, const wxChar
*szName
)
301 // FIXME VZ: I forgot the exact meaning of LC_PATH - anyone to remind me?
303 const wxChar
*pszLcPath
= wxGetenv("LC_PATH");
304 if ( pszLcPath
!= NULL
)
305 strPath
+= pszLcPath
+ wxString(szDirPrefix
) + MSG_PATH
;
308 wxString searchPath
= GetFullSearchPath(szDirPrefix
);
309 const wxChar
*sublocale
= wxStrchr(szDirPrefix
, _T('_'));
312 // also add just base locale name: for things like "fr_BE" (belgium
313 // french) we should use "fr" if no belgium specific message catalogs
315 searchPath
<< GetFullSearchPath(wxString(szDirPrefix
).
316 Left((size_t)(sublocale
- szDirPrefix
)))
320 wxString strFile
= szName
;
321 strFile
+= MSGCATALOG_EXTENSION
;
323 // don't give translation errors here because the wxstd catalog might
324 // not yet be loaded (and it's normal)
326 // (we're using an object because we have several return paths)
327 NoTransErr noTransErr
;
329 wxLogVerbose(_("looking for catalog '%s' in path '%s'."),
330 szName
, searchPath
.c_str());
332 wxString strFullName
;
333 if ( !wxFindFileInPath(&strFullName
, searchPath
, strFile
) ) {
334 wxLogWarning(_("catalog file for domain '%s' not found."), szName
);
339 wxLogVerbose(_("using catalog '%s' from '%s'."),
340 szName
, strFullName
.c_str());
342 wxFile
fileMsg(strFullName
);
343 if ( !fileMsg
.IsOpened() )
347 off_t nSize
= fileMsg
.Length();
348 if ( nSize
== wxInvalidOffset
)
351 // read the whole file in memory
352 m_pData
= new size_t8
[nSize
];
353 if ( fileMsg
.Read(m_pData
, nSize
) != nSize
) {
359 bool bValid
= (size_t)nSize
> sizeof(wxMsgCatalogHeader
);
361 wxMsgCatalogHeader
*pHeader
= (wxMsgCatalogHeader
*)m_pData
;
363 // we'll have to swap all the integers if it's true
364 m_bSwapped
= pHeader
->magic
== MSGCATALOG_MAGIC_SW
;
366 // check the magic number
367 bValid
= m_bSwapped
|| pHeader
->magic
== MSGCATALOG_MAGIC
;
371 // it's either too short or has incorrect magic number
372 wxLogWarning(_("'%s' is not a valid message catalog."), strFullName
.c_str());
379 m_numStrings
= Swap(pHeader
->numStrings
);
380 m_pOrigTable
= (wxMsgTableEntry
*)(m_pData
+
381 Swap(pHeader
->ofsOrigTable
));
382 m_pTransTable
= (wxMsgTableEntry
*)(m_pData
+
383 Swap(pHeader
->ofsTransTable
));
385 m_nHashSize
= Swap(pHeader
->nHashSize
);
386 m_pHashTable
= (size_t32
*)(m_pData
+ Swap(pHeader
->ofsHashTable
));
388 m_pszName
= new wxChar
[wxStrlen(szName
) + 1];
389 wxStrcpy(m_pszName
, szName
);
391 // everything is fine
395 // search for a string
396 const char *wxMsgCatalog::GetString(const char *szOrig
) const
398 if ( szOrig
== NULL
)
401 if ( HasHashTable() ) { // use hash table for lookup if possible
402 size_t32 nHashVal
= GetHash(szOrig
);
403 size_t32 nIndex
= nHashVal
% m_nHashSize
;
405 size_t32 nIncr
= 1 + (nHashVal
% (m_nHashSize
- 2));
408 size_t32 nStr
= Swap(m_pHashTable
[nIndex
]);
412 if ( strcmp(szOrig
, StringAtOfs(m_pOrigTable
, nStr
- 1)) == 0 )
413 return StringAtOfs(m_pTransTable
, nStr
- 1);
415 if ( nIndex
>= m_nHashSize
- nIncr
)
416 nIndex
-= m_nHashSize
- nIncr
;
421 else { // no hash table: use default binary search
425 while ( bottom
< top
) {
426 current
= (bottom
+ top
) / 2;
427 int res
= strcmp(szOrig
, StringAtOfs(m_pOrigTable
, current
));
431 bottom
= current
+ 1;
433 return StringAtOfs(m_pTransTable
, current
);
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
447 m_pszOldLocale
= NULL
;
451 // NB: this function has (desired) side effect of changing current locale
452 bool wxLocale::Init(const wxChar
*szName
,
453 const wxChar
*szShort
,
454 const wxChar
*szLocale
,
457 m_strLocale
= szName
;
458 m_strShort
= szShort
;
460 // change current locale (default: same as long name)
461 if ( szLocale
== NULL
)
463 m_pszOldLocale
= wxSetlocale(LC_ALL
, szLocale
);
464 if ( m_pszOldLocale
== NULL
)
465 wxLogError(_("locale '%s' can not be set."), szLocale
);
467 // the short name will be used to look for catalog files as well,
468 // so we need something here
469 if ( m_strShort
.IsEmpty() ) {
470 // FIXME I don't know how these 2 letter abbreviations are formed,
471 // this wild guess is surely wrong
472 m_strShort
= tolower(szLocale
[0]) + tolower(szLocale
[1]);
475 // save the old locale to be able to restore it later
476 m_pOldLocale
= wxSetLocale(this);
478 // load the default catalog with wxWindows standard messages
482 bOk
= AddCatalog(_T("wxstd"));
487 void wxLocale::AddCatalogLookupPathPrefix(const wxString
& prefix
)
489 if ( s_searchPrefixes
.Index(prefix
) == wxNOT_FOUND
)
491 s_searchPrefixes
.Add(prefix
);
493 //else: already have it
497 wxLocale::~wxLocale()
500 wxMsgCatalog
*pTmpCat
;
501 while ( m_pMsgCat
!= NULL
) {
503 m_pMsgCat
= m_pMsgCat
->m_pNext
;
507 // restore old locale
508 wxSetLocale(m_pOldLocale
);
509 wxSetlocale(LC_ALL
, m_pszOldLocale
);
512 // get the translation of given string in current locale
513 const wxMB2WXbuf
wxLocale::GetString(const wxChar
*szOrigString
,
514 const wxChar
*szDomain
) const
516 if ( wxIsEmpty(szOrigString
) )
519 const char *pszTrans
= NULL
;
520 const wxWX2MBbuf szOrgString
= wxConvCurrent
->cWX2MB(szOrigString
);
522 wxMsgCatalog
*pMsgCat
;
523 if ( szDomain
!= NULL
) {
524 pMsgCat
= FindCatalog(szDomain
);
526 // does the catalog exist?
527 if ( pMsgCat
!= NULL
)
528 pszTrans
= pMsgCat
->GetString(szOrgString
);
531 // search in all domains
532 for ( pMsgCat
= m_pMsgCat
; pMsgCat
!= NULL
; pMsgCat
= pMsgCat
->m_pNext
) {
533 pszTrans
= pMsgCat
->GetString(szOrgString
);
534 if ( pszTrans
!= NULL
) // take the first found
539 if ( pszTrans
== NULL
) {
540 if ( wxIsLoggingTransErrors() ) {
541 // suppress further error messages if we're not debugging: this avoids
542 // flooding the user with messages about each and every missing string if,
543 // for example, a whole catalog file is missing.
545 // do it before calling LogWarning to prevent infinite recursion!
547 NoTransErr noTransErr
;
549 wxSuppressTransErrors();
550 #endif // debug/!debug
552 if ( szDomain
!= NULL
)
554 wxLogWarning(_("string '%s' not found in domain '%s' for locale '%s'."),
555 szOrigString
, szDomain
, m_strLocale
.c_str());
559 wxLogWarning(_("string '%s' not found in locale '%s'."),
560 szOrigString
, m_strLocale
.c_str());
564 return (wxMB2WXbuf
)(szOrigString
);
567 return (wxMB2WXbuf
)(wxConvCurrent
->cMB2WX(pszTrans
));
570 // find catalog by name in a linked list, return NULL if !found
571 wxMsgCatalog
*wxLocale::FindCatalog(const wxChar
*szDomain
) const
573 // linear search in the linked list
574 wxMsgCatalog
*pMsgCat
;
575 for ( pMsgCat
= m_pMsgCat
; pMsgCat
!= NULL
; pMsgCat
= pMsgCat
->m_pNext
) {
576 if ( wxStricmp(pMsgCat
->GetName(), szDomain
) == 0 )
583 // check if the given catalog is loaded
584 bool wxLocale::IsLoaded(const wxChar
*szDomain
) const
586 return FindCatalog(szDomain
) != NULL
;
589 // add a catalog to our linked list
590 bool wxLocale::AddCatalog(const wxChar
*szDomain
)
592 wxMsgCatalog
*pMsgCat
= new wxMsgCatalog
;
594 if ( pMsgCat
->Load(m_strShort
, szDomain
) ) {
595 // add it to the head of the list so that in GetString it will
596 // be searched before the catalogs added earlier
597 pMsgCat
->m_pNext
= m_pMsgCat
;
603 // don't add it because it couldn't be loaded anyway
610 // ----------------------------------------------------------------------------
611 // global functions and variables
612 // ----------------------------------------------------------------------------
614 // translation errors logging
615 // --------------------------
617 static bool gs_bGiveTransErrors
= TRUE
;
619 void wxSuppressTransErrors()
621 gs_bGiveTransErrors
= FALSE
;
624 void wxRestoreTransErrors()
626 gs_bGiveTransErrors
= TRUE
;
629 bool wxIsLoggingTransErrors()
631 return gs_bGiveTransErrors
;
634 // retrieve/change current locale
635 // ------------------------------
637 // the current locale object
638 static wxLocale
*g_pLocale
= NULL
;
640 wxLocale
*wxGetLocale()
645 wxLocale
*wxSetLocale(wxLocale
*pLocale
)
647 wxLocale
*pOld
= g_pLocale
;