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"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // this should *not* be wxChar, this type must have exactly 8 bits!
53 typedef unsigned char size_t8
;
56 #if defined(__WIN16__)
57 typedef unsigned long size_t32
;
58 #elif defined(__WIN32__)
59 typedef unsigned int size_t32
;
61 // Win64 will have different type sizes
62 #error "Please define a 32 bit type"
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
;
71 // assume sizeof(int) == 4 - what else can we do
72 typedef unsigned int size_t32
;
74 // ... but at least check it during run time
75 static class IntSizeChecker
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);
84 wxASSERT_MSG( intsize
== 4,
85 "size_t32 is incorrectly defined!" );
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 // magic number identifying the .mo format file
96 const size_t32 MSGCATALOG_MAGIC
= 0x950412de;
97 const size_t32 MSGCATALOG_MAGIC_SW
= 0xde120495;
99 // extension of ".mo" files
100 #define MSGCATALOG_EXTENSION ".mo"
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
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
110 void wxSuppressTransErrors();
112 // restore the logging
113 void wxRestoreTransErrors();
115 // get the current state
116 bool wxIsLoggingTransErrors();
118 static wxLocale
*wxSetLocale(wxLocale
*pLocale
);
120 // ----------------------------------------------------------------------------
121 // wxMsgCatalog corresponds to one disk-file message catalog.
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 // ----------------------------------------------------------------------------
134 // load the catalog from disk (szDirPrefix corresponds to language)
135 bool Load(const wxChar
*szDirPrefix
, const wxChar
*szName
);
136 bool IsLoaded() const { return m_pData
!= NULL
; }
138 // get name of the catalog
139 const wxChar
*GetName() const { return m_pszName
; }
141 // get the translated string: returns NULL if not found
142 const char *GetString(const char *sz
) const;
144 // public variable pointing to the next element in a linked list (or NULL)
145 wxMsgCatalog
*m_pNext
;
148 // this implementation is binary compatible with GNU gettext() version 0.10
150 // an entry in the string table
151 struct wxMsgTableEntry
153 size_t32 nLen
; // length of the string
154 size_t32 ofsString
; // pointer to the string
157 // header of a .mo file
158 struct wxMsgCatalogHeader
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
169 // all data is stored here, NULL if no data loaded
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
179 const char *StringAtOfs(wxMsgTableEntry
*pTable
, size_t32 index
) const
180 { return (const char *)(m_pData
+ Swap(pTable
[index
].ofsString
)); }
183 // calculate the hash value of given string
184 static inline size_t32
GetHash(const char *sz
);
185 // big<->little endian
186 inline size_t32
Swap(size_t32 ui
) const;
189 bool HasHashTable() const // true if hash table is present
190 { return m_nHashSize
> 2 && m_pHashTable
!= NULL
; }
192 bool m_bSwapped
; // wrong endianness?
194 wxChar
*m_pszName
; // name of the domain
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 // the list of the directories to search for message catalog files
202 static wxArrayString s_searchPrefixes
;
204 // ============================================================================
206 // ============================================================================
208 // ----------------------------------------------------------------------------
209 // wxMsgCatalog class
210 // ----------------------------------------------------------------------------
212 // calculate hash value using the so called hashpjw function by P.J. Weinberger
213 // [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools]
214 size_t32
wxMsgCatalog::GetHash(const char *sz
)
216 #define HASHWORDBITS 32 // the length of size_t32
220 while ( *sz
!= '\0' ) {
222 hval
+= (size_t32
)*sz
++;
223 g
= hval
& ((size_t32
)0xf << (HASHWORDBITS
- 4));
225 hval
^= g
>> (HASHWORDBITS
- 8);
233 // swap the 2 halves of 32 bit integer if needed
234 size_t32
wxMsgCatalog::Swap(size_t32 ui
) const
236 return m_bSwapped
? (ui
<< 24) | ((ui
& 0xff00) << 8) |
237 ((ui
>> 8) & 0xff00) | (ui
>> 24)
241 wxMsgCatalog::wxMsgCatalog()
247 wxMsgCatalog::~wxMsgCatalog()
250 wxDELETEA(m_pszName
);
253 // small class to suppress the translation erros until exit from current scope
257 NoTransErr() { wxSuppressTransErrors(); }
258 ~NoTransErr() { wxRestoreTransErrors(); }
261 // return all directories to search for given prefix
262 static wxString
GetAllMsgCatalogSubdirs(const wxChar
*prefix
,
267 // search first in prefix/fr/LC_MESSAGES, then in prefix/fr and finally in
268 // prefix (assuming the language is 'fr')
269 searchPath
<< prefix
<< wxFILE_SEP_PATH
<< lang
<< wxFILE_SEP_PATH
270 << _T("LC_MESSAGES") << wxPATH_SEP
271 << prefix
<< wxFILE_SEP_PATH
<< lang
<< wxPATH_SEP
272 << prefix
<< wxPATH_SEP
;
277 // construct the search path for the given language
278 static wxString
GetFullSearchPath(const wxChar
*lang
)
282 // first take the entries explicitly added by the program
283 size_t count
= s_searchPrefixes
.Count();
284 for ( size_t n
= 0; n
< count
; n
++ )
286 searchPath
<< GetAllMsgCatalogSubdirs(s_searchPrefixes
[n
], lang
)
290 // then take the current directory
291 // FIXME it should be the directory of the executable
292 searchPath
<< GetAllMsgCatalogSubdirs(_T("."), lang
) << wxPATH_SEP
;
294 // and finally add some standard ones
296 << GetAllMsgCatalogSubdirs(_T("/usr/share/locale"), lang
) << wxPATH_SEP
297 << GetAllMsgCatalogSubdirs(_T("/usr/lib/locale"), lang
) << wxPATH_SEP
298 << GetAllMsgCatalogSubdirs(_T("/usr/local/share/locale"), lang
);
303 // open disk file and read in it's contents
304 bool wxMsgCatalog::Load(const wxChar
*szDirPrefix
, const wxChar
*szName0
)
306 /* We need to handle locales like de_AT.iso-8859-1
307 For this we first chop off the .CHARSET specifier and ignore it.
308 FIXME: UNICODE SUPPORT: must use CHARSET specifier!
310 wxString szName
= szName0
;
311 if(szName
.Find('.') != -1) // contains a dot
312 szName
= szName
.Left(szName
.Find('.'));
314 // FIXME VZ: I forgot the exact meaning of LC_PATH - anyone to remind me?
315 // KB: search path where to find the mo files, probably : delimited
317 const wxChar
*pszLcPath
= wxGetenv("LC_PATH");
318 if ( pszLcPath
!= NULL
)
319 strPath
+= pszLcPath
+ wxString(szDirPrefix
) + MSG_PATH
;
322 wxString searchPath
= GetFullSearchPath(szDirPrefix
);
323 const wxChar
*sublocale
= wxStrchr(szDirPrefix
, _T('_'));
326 // also add just base locale name: for things like "fr_BE" (belgium
327 // french) we should use "fr" if no belgium specific message catalogs
329 searchPath
<< GetFullSearchPath(wxString(szDirPrefix
).
330 Left((size_t)(sublocale
- szDirPrefix
)))
334 wxString strFile
= szName
;
335 strFile
+= MSGCATALOG_EXTENSION
;
337 // don't give translation errors here because the wxstd catalog might
338 // not yet be loaded (and it's normal)
340 // (we're using an object because we have several return paths)
342 NoTransErr noTransErr
;
343 // Then why do you translate at all? Just use _T() and not _(). RR.
345 wxLogVerbose(_("looking for catalog '%s' in path '%s'."),
346 szName
.c_str
, searchPath
.c_str());
348 wxString strFullName
;
349 if ( !wxFindFileInPath(&strFullName
, searchPath
, strFile
) ) {
350 wxLogWarning(_("catalog file for domain '%s' not found."), szName
.c_str());
355 wxLogVerbose(_("using catalog '%s' from '%s'."),
356 szName
.c_str(), strFullName
.c_str());
358 wxFile
fileMsg(strFullName
);
359 if ( !fileMsg
.IsOpened() )
363 off_t nSize
= fileMsg
.Length();
364 if ( nSize
== wxInvalidOffset
)
367 // read the whole file in memory
368 m_pData
= new size_t8
[nSize
];
369 if ( fileMsg
.Read(m_pData
, nSize
) != nSize
) {
375 bool bValid
= (size_t)nSize
> sizeof(wxMsgCatalogHeader
);
377 wxMsgCatalogHeader
*pHeader
= (wxMsgCatalogHeader
*)m_pData
;
379 // we'll have to swap all the integers if it's true
380 m_bSwapped
= pHeader
->magic
== MSGCATALOG_MAGIC_SW
;
382 // check the magic number
383 bValid
= m_bSwapped
|| pHeader
->magic
== MSGCATALOG_MAGIC
;
387 // it's either too short or has incorrect magic number
388 wxLogWarning(_("'%s' is not a valid message catalog."), strFullName
.c_str());
395 m_numStrings
= Swap(pHeader
->numStrings
);
396 m_pOrigTable
= (wxMsgTableEntry
*)(m_pData
+
397 Swap(pHeader
->ofsOrigTable
));
398 m_pTransTable
= (wxMsgTableEntry
*)(m_pData
+
399 Swap(pHeader
->ofsTransTable
));
401 m_nHashSize
= Swap(pHeader
->nHashSize
);
402 m_pHashTable
= (size_t32
*)(m_pData
+ Swap(pHeader
->ofsHashTable
));
404 m_pszName
= new wxChar
[wxStrlen(szName
) + 1];
405 wxStrcpy(m_pszName
, szName
);
407 // everything is fine
411 // search for a string
412 const char *wxMsgCatalog::GetString(const char *szOrig
) const
414 if ( szOrig
== NULL
)
417 if ( HasHashTable() ) { // use hash table for lookup if possible
418 size_t32 nHashVal
= GetHash(szOrig
);
419 size_t32 nIndex
= nHashVal
% m_nHashSize
;
421 size_t32 nIncr
= 1 + (nHashVal
% (m_nHashSize
- 2));
424 size_t32 nStr
= Swap(m_pHashTable
[nIndex
]);
428 if ( strcmp(szOrig
, StringAtOfs(m_pOrigTable
, nStr
- 1)) == 0 )
429 return StringAtOfs(m_pTransTable
, nStr
- 1);
431 if ( nIndex
>= m_nHashSize
- nIncr
)
432 nIndex
-= m_nHashSize
- nIncr
;
437 else { // no hash table: use default binary search
441 while ( bottom
< top
) {
442 current
= (bottom
+ top
) / 2;
443 int res
= strcmp(szOrig
, StringAtOfs(m_pOrigTable
, current
));
447 bottom
= current
+ 1;
449 return StringAtOfs(m_pTransTable
, current
);
457 // ----------------------------------------------------------------------------
459 // ----------------------------------------------------------------------------
463 m_pszOldLocale
= NULL
;
467 // NB: this function has (desired) side effect of changing current locale
468 bool wxLocale::Init(const wxChar
*szName
,
469 const wxChar
*szShort
,
470 const wxChar
*szLocale
,
473 m_strLocale
= szName
;
474 m_strShort
= szShort
;
476 // change current locale (default: same as long name)
477 if ( szLocale
== NULL
)
479 m_pszOldLocale
= wxSetlocale(LC_ALL
, szLocale
);
480 if ( m_pszOldLocale
== NULL
)
481 wxLogError(_("locale '%s' can not be set."), szLocale
);
483 // the short name will be used to look for catalog files as well,
484 // so we need something here
485 if ( m_strShort
.IsEmpty() ) {
486 // FIXME I don't know how these 2 letter abbreviations are formed,
487 // this wild guess is surely wrong
488 m_strShort
= tolower(szLocale
[0]) + tolower(szLocale
[1]);
491 // save the old locale to be able to restore it later
492 m_pOldLocale
= wxSetLocale(this);
494 // load the default catalog with wxWindows standard messages
498 bOk
= AddCatalog(_T("wxstd"));
503 void wxLocale::AddCatalogLookupPathPrefix(const wxString
& prefix
)
505 if ( s_searchPrefixes
.Index(prefix
) == wxNOT_FOUND
)
507 s_searchPrefixes
.Add(prefix
);
509 //else: already have it
513 wxLocale::~wxLocale()
516 wxMsgCatalog
*pTmpCat
;
517 while ( m_pMsgCat
!= NULL
) {
519 m_pMsgCat
= m_pMsgCat
->m_pNext
;
523 // restore old locale
524 wxSetLocale(m_pOldLocale
);
525 wxSetlocale(LC_ALL
, m_pszOldLocale
);
528 // get the translation of given string in current locale
529 const wxMB2WXbuf
wxLocale::GetString(const wxChar
*szOrigString
,
530 const wxChar
*szDomain
) const
532 if ( wxIsEmpty(szOrigString
) )
535 const char *pszTrans
= NULL
;
536 const wxWX2MBbuf szOrgString
= wxConvCurrent
->cWX2MB(szOrigString
);
538 wxMsgCatalog
*pMsgCat
;
539 if ( szDomain
!= NULL
) {
540 pMsgCat
= FindCatalog(szDomain
);
542 // does the catalog exist?
543 if ( pMsgCat
!= NULL
)
544 pszTrans
= pMsgCat
->GetString(szOrgString
);
547 // search in all domains
548 for ( pMsgCat
= m_pMsgCat
; pMsgCat
!= NULL
; pMsgCat
= pMsgCat
->m_pNext
) {
549 pszTrans
= pMsgCat
->GetString(szOrgString
);
550 if ( pszTrans
!= NULL
) // take the first found
555 if ( pszTrans
== NULL
) {
556 if ( wxIsLoggingTransErrors() ) {
557 // suppress further error messages if we're not debugging: this avoids
558 // flooding the user with messages about each and every missing string if,
559 // for example, a whole catalog file is missing.
561 // do it before calling LogWarning to prevent infinite recursion!
563 NoTransErr noTransErr
;
565 wxSuppressTransErrors();
566 #endif // debug/!debug
568 if ( szDomain
!= NULL
)
570 wxLogWarning(_("string '%s' not found in domain '%s' for locale '%s'."),
571 szOrigString
, szDomain
, m_strLocale
.c_str());
575 wxLogWarning(_("string '%s' not found in locale '%s'."),
576 szOrigString
, m_strLocale
.c_str());
580 return (wxMB2WXbuf
)(szOrigString
);
583 return (wxMB2WXbuf
)(wxConvCurrent
->cMB2WX(pszTrans
));
586 // find catalog by name in a linked list, return NULL if !found
587 wxMsgCatalog
*wxLocale::FindCatalog(const wxChar
*szDomain
) const
589 // linear search in the linked list
590 wxMsgCatalog
*pMsgCat
;
591 for ( pMsgCat
= m_pMsgCat
; pMsgCat
!= NULL
; pMsgCat
= pMsgCat
->m_pNext
) {
592 if ( wxStricmp(pMsgCat
->GetName(), szDomain
) == 0 )
599 // check if the given catalog is loaded
600 bool wxLocale::IsLoaded(const wxChar
*szDomain
) const
602 return FindCatalog(szDomain
) != NULL
;
605 // add a catalog to our linked list
606 bool wxLocale::AddCatalog(const wxChar
*szDomain
)
608 wxMsgCatalog
*pMsgCat
= new wxMsgCatalog
;
610 if ( pMsgCat
->Load(m_strShort
, szDomain
) ) {
611 // add it to the head of the list so that in GetString it will
612 // be searched before the catalogs added earlier
613 pMsgCat
->m_pNext
= m_pMsgCat
;
619 // don't add it because it couldn't be loaded anyway
626 // ----------------------------------------------------------------------------
627 // global functions and variables
628 // ----------------------------------------------------------------------------
630 // translation errors logging
631 // --------------------------
633 static bool gs_bGiveTransErrors
= TRUE
;
635 void wxSuppressTransErrors()
637 gs_bGiveTransErrors
= FALSE
;
640 void wxRestoreTransErrors()
642 gs_bGiveTransErrors
= TRUE
;
645 bool wxIsLoggingTransErrors()
647 return gs_bGiveTransErrors
;
650 // retrieve/change current locale
651 // ------------------------------
653 // the current locale object
654 static wxLocale
*g_pLocale
= NULL
;
656 wxLocale
*wxGetLocale()
661 wxLocale
*wxSetLocale(wxLocale
*pLocale
)
663 wxLocale
*pOld
= g_pLocale
;