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"
37 #include "wx/string.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // this should *not* be wxChar, this type must have exactly 8 bits!
50 typedef unsigned char size_t8
;
53 #if defined(__WIN16__)
54 typedef unsigned long size_t32
;
55 #elif defined(__WIN32__)
56 typedef unsigned int size_t32
;
58 // Win64 will have different type sizes
59 #error "Please define a 32 bit type"
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
;
68 // assume sizeof(int) == 4 - what else can we do
69 typedef unsigned int size_t32
;
71 // ... but at least check it during run time
72 static class IntSizeChecker
77 wxASSERT_MSG( sizeof(int) == 4,
78 "size_t32 is incorrectly defined!" );
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // magic number identifying the .mo format file
89 const size_t32 MSGCATALOG_MAGIC
= 0x950412de;
90 const size_t32 MSGCATALOG_MAGIC_SW
= 0xde120495;
92 // extension of ".mo" files
93 #define MSGCATALOG_EXTENSION ".mo"
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
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
103 void wxSuppressTransErrors();
105 // restore the logging
106 void wxRestoreTransErrors();
108 // get the current state
109 bool wxIsLoggingTransErrors();
111 static wxLocale
*wxSetLocale(wxLocale
*pLocale
);
113 // ----------------------------------------------------------------------------
114 // wxMsgCatalog corresponds to one disk-file message catalog.
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 // ----------------------------------------------------------------------------
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
; }
131 // get name of the catalog
132 const wxChar
*GetName() const { return m_pszName
; }
134 // get the translated string: returns NULL if not found
135 const char *GetString(const char *sz
) const;
137 // public variable pointing to the next element in a linked list (or NULL)
138 wxMsgCatalog
*m_pNext
;
141 // this implementation is binary compatible with GNU gettext() version 0.10
143 // an entry in the string table
144 struct wxMsgTableEntry
146 size_t32 nLen
; // length of the string
147 size_t32 ofsString
; // pointer to the string
150 // header of a .mo file
151 struct wxMsgCatalogHeader
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
162 // all data is stored here, NULL if no data loaded
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
172 const char *StringAtOfs(wxMsgTableEntry
*pTable
, size_t32 index
) const
173 { return (const char *)(m_pData
+ Swap(pTable
[index
].ofsString
)); }
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;
182 bool HasHashTable() const // true if hash table is present
183 { return m_nHashSize
> 2 && m_pHashTable
!= NULL
; }
185 bool m_bSwapped
; // wrong endianness?
187 wxChar
*m_pszName
; // name of the domain
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 // the list of the directories to search for message catalog files
195 static wxArrayString s_searchPrefixes
;
197 // ============================================================================
199 // ============================================================================
201 // ----------------------------------------------------------------------------
202 // wxMsgCatalog class
203 // ----------------------------------------------------------------------------
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
)
209 #define HASHWORDBITS 32 // the length of size_t32
213 while ( *sz
!= '\0' ) {
215 hval
+= (size_t32
)*sz
++;
216 g
= hval
& ((size_t32
)0xf << (HASHWORDBITS
- 4));
218 hval
^= g
>> (HASHWORDBITS
- 8);
226 // swap the 2 halves of 32 bit integer if needed
227 size_t32
wxMsgCatalog::Swap(size_t32 ui
) const
229 return m_bSwapped
? (ui
<< 24) | ((ui
& 0xff00) << 8) |
230 ((ui
>> 8) & 0xff00) | (ui
>> 24)
234 wxMsgCatalog::wxMsgCatalog()
240 wxMsgCatalog::~wxMsgCatalog()
243 wxDELETEA(m_pszName
);
246 // small class to suppress the translation erros until exit from current scope
250 NoTransErr() { wxSuppressTransErrors(); }
251 ~NoTransErr() { wxRestoreTransErrors(); }
254 // return all directories to search for given prefix
255 static wxString
GetAllMsgCatalogSubdirs(const wxChar
*prefix
,
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
;
270 // construct the search path for the given language
271 static wxString
GetFullSearchPath(const wxChar
*lang
)
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
++ )
279 searchPath
<< GetAllMsgCatalogSubdirs(s_searchPrefixes
[n
], lang
)
283 // then take the current directory
284 // FIXME it should be the directory of the executable
285 searchPath
<< GetAllMsgCatalogSubdirs(_T("."), lang
) << wxPATH_SEP
;
287 // and finally add some standard ones
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
);
296 // open disk file and read in it's contents
297 bool wxMsgCatalog::Load(const wxChar
*szDirPrefix
, const wxChar
*szName
)
299 // FIXME VZ: I forgot the exact meaning of LC_PATH - anyone to remind me?
301 const wxChar
*pszLcPath
= wxGetenv("LC_PATH");
302 if ( pszLcPath
!= NULL
)
303 strPath
+= pszLcPath
+ wxString(szDirPrefix
) + MSG_PATH
;
306 wxString searchPath
= GetFullSearchPath(szDirPrefix
);
307 const wxChar
*sublocale
= wxStrchr(szDirPrefix
, _T('_'));
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
313 searchPath
<< GetFullSearchPath(wxString(szDirPrefix
).
314 Left((size_t)(sublocale
- szDirPrefix
)))
318 wxString strFile
= szName
;
319 strFile
+= MSGCATALOG_EXTENSION
;
321 // don't give translation errors here because the wxstd catalog might
322 // not yet be loaded (and it's normal)
324 // (we're using an object because we have several return paths)
325 NoTransErr noTransErr
;
327 wxLogVerbose(_("looking for catalog '%s' in path '%s'."),
328 szName
, searchPath
.c_str());
330 wxString strFullName
;
331 if ( !wxFindFileInPath(&strFullName
, searchPath
, strFile
) ) {
332 wxLogWarning(_("catalog file for domain '%s' not found."), szName
);
337 wxLogVerbose(_("using catalog '%s' from '%s'."),
338 szName
, strFullName
.c_str());
340 wxFile
fileMsg(strFullName
);
341 if ( !fileMsg
.IsOpened() )
345 off_t nSize
= fileMsg
.Length();
346 if ( nSize
== wxInvalidOffset
)
349 // read the whole file in memory
350 m_pData
= new size_t8
[nSize
];
351 if ( fileMsg
.Read(m_pData
, nSize
) != nSize
) {
357 bool bValid
= (size_t)nSize
> sizeof(wxMsgCatalogHeader
);
359 wxMsgCatalogHeader
*pHeader
= (wxMsgCatalogHeader
*)m_pData
;
361 // we'll have to swap all the integers if it's true
362 m_bSwapped
= pHeader
->magic
== MSGCATALOG_MAGIC_SW
;
364 // check the magic number
365 bValid
= m_bSwapped
|| pHeader
->magic
== MSGCATALOG_MAGIC
;
369 // it's either too short or has incorrect magic number
370 wxLogWarning(_("'%s' is not a valid message catalog."), strFullName
.c_str());
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
));
383 m_nHashSize
= Swap(pHeader
->nHashSize
);
384 m_pHashTable
= (size_t32
*)(m_pData
+ Swap(pHeader
->ofsHashTable
));
386 m_pszName
= new wxChar
[wxStrlen(szName
) + 1];
387 wxStrcpy(m_pszName
, szName
);
389 // everything is fine
393 // search for a string
394 const char *wxMsgCatalog::GetString(const char *szOrig
) const
396 if ( szOrig
== NULL
)
399 if ( HasHashTable() ) { // use hash table for lookup if possible
400 size_t32 nHashVal
= GetHash(szOrig
);
401 size_t32 nIndex
= nHashVal
% m_nHashSize
;
403 size_t32 nIncr
= 1 + (nHashVal
% (m_nHashSize
- 2));
406 size_t32 nStr
= Swap(m_pHashTable
[nIndex
]);
410 if ( strcmp(szOrig
, StringAtOfs(m_pOrigTable
, nStr
- 1)) == 0 )
411 return StringAtOfs(m_pTransTable
, nStr
- 1);
413 if ( nIndex
>= m_nHashSize
- nIncr
)
414 nIndex
-= m_nHashSize
- nIncr
;
419 else { // no hash table: use default binary search
423 while ( bottom
< top
) {
424 current
= (bottom
+ top
) / 2;
425 int res
= strcmp(szOrig
, StringAtOfs(m_pOrigTable
, current
));
429 bottom
= current
+ 1;
431 return StringAtOfs(m_pTransTable
, current
);
439 // ----------------------------------------------------------------------------
441 // ----------------------------------------------------------------------------
445 m_pszOldLocale
= NULL
;
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
,
455 m_strLocale
= szName
;
456 m_strShort
= szShort
;
458 // change current locale (default: same as long name)
459 if ( szLocale
== NULL
)
461 m_pszOldLocale
= wxSetlocale(LC_ALL
, szLocale
);
462 if ( m_pszOldLocale
== NULL
)
463 wxLogError(_("locale '%s' can not be set."), szLocale
);
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]);
473 // save the old locale to be able to restore it later
474 m_pOldLocale
= wxSetLocale(this);
476 // load the default catalog with wxWindows standard messages
480 bOk
= AddCatalog(_T("wxstd"));
485 void wxLocale::AddCatalogLookupPathPrefix(const wxString
& prefix
)
487 if ( s_searchPrefixes
.Index(prefix
) == wxNOT_FOUND
)
489 s_searchPrefixes
.Add(prefix
);
491 //else: already have it
495 wxLocale::~wxLocale()
498 wxMsgCatalog
*pTmpCat
;
499 while ( m_pMsgCat
!= NULL
) {
501 m_pMsgCat
= m_pMsgCat
->m_pNext
;
505 // restore old locale
506 wxSetLocale(m_pOldLocale
);
507 wxSetlocale(LC_ALL
, m_pszOldLocale
);
510 // get the translation of given string in current locale
511 const wxMB2WXbuf
wxLocale::GetString(const wxChar
*szOrigString
,
512 const wxChar
*szDomain
) const
514 if ( wxIsEmpty(szOrigString
) )
517 const char *pszTrans
= NULL
;
518 const wxWX2MBbuf szOrgString
= wxConv_libc
.cWX2MB(szOrigString
);
520 wxMsgCatalog
*pMsgCat
;
521 if ( szDomain
!= NULL
) {
522 pMsgCat
= FindCatalog(szDomain
);
524 // does the catalog exist?
525 if ( pMsgCat
!= NULL
)
526 pszTrans
= pMsgCat
->GetString(szOrgString
);
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
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.
543 // do it before calling LogWarning to prevent infinite recursion!
545 NoTransErr noTransErr
;
547 wxSuppressTransErrors();
548 #endif // debug/!debug
550 if ( szDomain
!= NULL
)
552 wxLogWarning(_("string '%s' not found in domain '%s' for locale '%s'."),
553 szOrigString
, szDomain
, m_strLocale
.c_str());
557 wxLogWarning(_("string '%s' not found in locale '%s'."),
558 szOrigString
, m_strLocale
.c_str());
562 return (wxMB2WXbuf
)(szOrigString
);
565 return (wxMB2WXbuf
)(wxConv_libc
.cMB2WX(pszTrans
));
568 // find catalog by name in a linked list, return NULL if !found
569 wxMsgCatalog
*wxLocale::FindCatalog(const wxChar
*szDomain
) const
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 )
581 // check if the given catalog is loaded
582 bool wxLocale::IsLoaded(const wxChar
*szDomain
) const
584 return FindCatalog(szDomain
) != NULL
;
587 // add a catalog to our linked list
588 bool wxLocale::AddCatalog(const wxChar
*szDomain
)
590 wxMsgCatalog
*pMsgCat
= new wxMsgCatalog
;
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
;
601 // don't add it because it couldn't be loaded anyway
608 // ----------------------------------------------------------------------------
609 // global functions and variables
610 // ----------------------------------------------------------------------------
612 // translation errors logging
613 // --------------------------
615 static bool gs_bGiveTransErrors
= TRUE
;
617 void wxSuppressTransErrors()
619 gs_bGiveTransErrors
= FALSE
;
622 void wxRestoreTransErrors()
624 gs_bGiveTransErrors
= TRUE
;
627 bool wxIsLoggingTransErrors()
629 return gs_bGiveTransErrors
;
632 // retrieve/change current locale
633 // ------------------------------
635 // the current locale object
636 static wxLocale
*g_pLocale
= NULL
;
638 wxLocale
*wxGetLocale()
643 wxLocale
*wxSetLocale(wxLocale
*pLocale
)
645 wxLocale
*pOld
= g_pLocale
;