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"
36 #include "wx/string.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // magic number identifying the .mo format file
49 const size_t32 MSGCATALOG_MAGIC
= 0x950412de;
50 const size_t32 MSGCATALOG_MAGIC_SW
= 0xde120495;
52 // extension of ".mo" files
53 #define MSGCATALOG_EXTENSION ".mo"
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // suppress further error messages about missing translations
60 // (if you don't have one catalog file, you wouldn't like to see the
61 // error message for each string in it, so normally it's given only
63 void wxSuppressTransErrors();
65 // restore the logging
66 void wxRestoreTransErrors();
68 // get the current state
69 bool wxIsLoggingTransErrors();
71 // get the current locale object (## may be NULL!)
72 extern wxLocale
*wxSetLocale(wxLocale
*pLocale
);
74 // ----------------------------------------------------------------------------
75 // wxMsgCatalog corresponds to one disk-file message catalog.
77 // This is a "low-level" class and is used only by wxLocale (that's why
78 // it's designed to be stored in a linked list)
79 // ----------------------------------------------------------------------------
88 // load the catalog from disk (szDirPrefix corresponds to language)
89 bool Load(const char *szDirPrefix
, const char *szName
);
90 bool IsLoaded() const { return m_pData
!= NULL
; }
92 // get name of the catalog
93 const char *GetName() const { return m_pszName
; }
95 // get the translated string: returns NULL if not found
96 const char *GetString(const char *sz
) const;
98 // public variable pointing to the next element in a linked list (or NULL)
99 wxMsgCatalog
*m_pNext
;
102 // this implementation is binary compatible with GNU gettext() version 0.10
104 // an entry in the string table
105 struct wxMsgTableEntry
107 size_t32 nLen
; // length of the string
108 size_t32 ofsString
; // pointer to the string
111 // header of a .mo file
112 struct wxMsgCatalogHeader
114 size_t32 magic
, // offset +00: magic id
115 revision
, // +04: revision
116 numStrings
; // +08: number of strings in the file
117 size_t32 ofsOrigTable
, // +0C: start of original string table
118 ofsTransTable
; // +10: start of translated string table
119 size_t32 nHashSize
, // +14: hash table size
120 ofsHashTable
; // +18: offset of hash table start
123 // all data is stored here, NULL if no data loaded
127 size_t32 m_numStrings
, // number of strings in this domain
128 m_nHashSize
; // number of entries in hash table
129 size_t32
*m_pHashTable
; // pointer to hash table
130 wxMsgTableEntry
*m_pOrigTable
, // pointer to original strings
131 *m_pTransTable
; // translated
133 const char *StringAtOfs(wxMsgTableEntry
*pTable
, size_t32 index
) const
134 { return (const char *)(m_pData
+ Swap(pTable
[index
].ofsString
)); }
137 // calculate the hash value of given string
138 static inline size_t32
GetHash(const char *sz
);
139 // big<->little endian
140 inline size_t32
Swap(size_t32 ui
) const;
143 bool HasHashTable() const // true if hash table is present
144 { return m_nHashSize
> 2 && m_pHashTable
!= NULL
; }
146 bool m_bSwapped
; // wrong endianness?
148 char *m_pszName
; // name of the domain
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 // the list of the directories to search for message catalog files
156 static wxArrayString s_searchPrefixes
;
158 // ============================================================================
160 // ============================================================================
162 // ----------------------------------------------------------------------------
163 // wxMsgCatalog class
164 // ----------------------------------------------------------------------------
166 // calculate hash value using the so called hashpjw function by P.J. Weinberger
167 // [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools]
168 size_t32
wxMsgCatalog::GetHash(const char *sz
)
170 #define HASHWORDBITS 32 // the length of size_t32
174 while ( *sz
!= '\0' ) {
176 hval
+= (size_t32
)*sz
++;
177 g
= hval
& ((size_t32
)0xf << (HASHWORDBITS
- 4));
179 hval
^= g
>> (HASHWORDBITS
- 8);
187 // swap the 2 halves of 32 bit integer if needed
188 size_t32
wxMsgCatalog::Swap(size_t32 ui
) const
190 return m_bSwapped
? (ui
<< 24) | ((ui
& 0xff00) << 8) |
191 ((ui
>> 8) & 0xff00) | (ui
>> 24)
195 wxMsgCatalog::wxMsgCatalog()
201 wxMsgCatalog::~wxMsgCatalog()
204 wxDELETEA(m_pszName
);
207 // small class to suppress the translation erros until exit from current scope
211 NoTransErr() { wxSuppressTransErrors(); }
212 ~NoTransErr() { wxRestoreTransErrors(); }
215 // return all directories to search for given prefix
216 static wxString
GetAllMsgCatalogSubdirs(const char *prefix
,
221 // search first in prefix/fr/LC_MESSAGES, then in prefix/fr and finally in
222 // prefix (assuming the language is 'fr')
223 searchPath
<< prefix
<< wxFILE_SEP_PATH
<< lang
<< wxFILE_SEP_PATH
224 << "LC_MESSAGES" << wxPATH_SEP
225 << prefix
<< wxFILE_SEP_PATH
<< lang
<< wxPATH_SEP
226 << prefix
<< wxPATH_SEP
;
231 // construct the search path for the given language
232 static wxString
GetFullSearchPath(const char *lang
)
236 // first take the entries explicitly added by the program
237 size_t count
= s_searchPrefixes
.Count();
238 for ( size_t n
= 0; n
< count
; n
++ )
240 searchPath
<< GetAllMsgCatalogSubdirs(s_searchPrefixes
[n
], lang
)
244 // then take the current directory
245 // FIXME it should be the directory of the executable
246 searchPath
<< GetAllMsgCatalogSubdirs(".", lang
) << wxPATH_SEP
;
248 // and finally add some standard ones
250 << GetAllMsgCatalogSubdirs("/usr/share/locale", lang
) << wxPATH_SEP
251 << GetAllMsgCatalogSubdirs("/usr/lib/locale", lang
) << wxPATH_SEP
252 << GetAllMsgCatalogSubdirs("/usr/local/share/locale", lang
);
257 // open disk file and read in it's contents
258 bool wxMsgCatalog::Load(const char *szDirPrefix
, const char *szName
)
260 // FIXME VZ: I forgot the exact meaning of LC_PATH - anyone to remind me?
262 const char *pszLcPath
= getenv("LC_PATH");
263 if ( pszLcPath
!= NULL
)
264 strPath
+= pszLcPath
+ wxString(szDirPrefix
) + MSG_PATH
;
267 wxString searchPath
= GetFullSearchPath(szDirPrefix
);
268 const char *sublocale
= strchr(szDirPrefix
, '_');
271 // also add just base locale name: for things like "fr_BE" (belgium
272 // french) we should use "fr" if no belgium specific message catalogs
274 searchPath
<< GetFullSearchPath(wxString(szDirPrefix
).
275 Left((size_t)(sublocale
- szDirPrefix
)))
279 wxString strFile
= szName
;
280 strFile
+= MSGCATALOG_EXTENSION
;
282 // don't give translation errors here because the wxstd catalog might
283 // not yet be loaded (and it's normal)
285 // (we're using an object because we have several return paths)
286 NoTransErr noTransErr
;
288 wxLogVerbose(_("looking for catalog '%s' in path '%s'."),
289 szName
, searchPath
.c_str());
291 wxString strFullName
;
292 if ( !wxFindFileInPath(&strFullName
, searchPath
, strFile
) ) {
293 wxLogWarning(_("catalog file for domain '%s' not found."), szName
);
298 wxLogVerbose(_("using catalog '%s' from '%s'."),
299 szName
, strFullName
.c_str());
301 wxFile
fileMsg(strFullName
);
302 if ( !fileMsg
.IsOpened() )
306 off_t nSize
= fileMsg
.Length();
307 if ( nSize
== wxInvalidOffset
)
310 // read the whole file in memory
311 m_pData
= new size_t8
[nSize
];
312 if ( fileMsg
.Read(m_pData
, nSize
) != nSize
) {
318 bool bValid
= (size_t)nSize
> sizeof(wxMsgCatalogHeader
);
320 wxMsgCatalogHeader
*pHeader
= (wxMsgCatalogHeader
*)m_pData
;
322 // we'll have to swap all the integers if it's true
323 m_bSwapped
= pHeader
->magic
== MSGCATALOG_MAGIC_SW
;
325 // check the magic number
326 bValid
= m_bSwapped
|| pHeader
->magic
== MSGCATALOG_MAGIC
;
330 // it's either too short or has incorrect magic number
331 wxLogWarning(_("'%s' is not a valid message catalog."), strFullName
.c_str());
338 m_numStrings
= Swap(pHeader
->numStrings
);
339 m_pOrigTable
= (wxMsgTableEntry
*)(m_pData
+
340 Swap(pHeader
->ofsOrigTable
));
341 m_pTransTable
= (wxMsgTableEntry
*)(m_pData
+
342 Swap(pHeader
->ofsTransTable
));
344 m_nHashSize
= Swap(pHeader
->nHashSize
);
345 m_pHashTable
= (size_t32
*)(m_pData
+ Swap(pHeader
->ofsHashTable
));
347 m_pszName
= new char[strlen(szName
) + 1];
348 strcpy(m_pszName
, szName
);
350 // everything is fine
354 // search for a string
355 const char *wxMsgCatalog::GetString(const char *szOrig
) const
357 if ( szOrig
== NULL
)
360 if ( HasHashTable() ) { // use hash table for lookup if possible
361 size_t32 nHashVal
= GetHash(szOrig
);
362 size_t32 nIndex
= nHashVal
% m_nHashSize
;
364 size_t32 nIncr
= 1 + (nHashVal
% (m_nHashSize
- 2));
367 size_t32 nStr
= Swap(m_pHashTable
[nIndex
]);
371 if ( strcmp(szOrig
, StringAtOfs(m_pOrigTable
, nStr
- 1)) == 0 )
372 return StringAtOfs(m_pTransTable
, nStr
- 1);
374 if ( nIndex
>= m_nHashSize
- nIncr
)
375 nIndex
-= m_nHashSize
- nIncr
;
380 else { // no hash table: use default binary search
384 while ( bottom
< top
) {
385 current
= (bottom
+ top
) / 2;
386 int res
= strcmp(szOrig
, StringAtOfs(m_pOrigTable
, current
));
390 bottom
= current
+ 1;
392 return StringAtOfs(m_pTransTable
, current
);
400 // ----------------------------------------------------------------------------
402 // ----------------------------------------------------------------------------
406 m_pszOldLocale
= NULL
;
410 // NB: this function has (desired) side effect of changing current locale
411 bool wxLocale::Init(const char *szName
,
413 const char *szLocale
,
416 m_strLocale
= szName
;
417 m_strShort
= szShort
;
419 // change current locale (default: same as long name)
420 if ( szLocale
== NULL
)
422 m_pszOldLocale
= setlocale(LC_ALL
, szLocale
);
423 if ( m_pszOldLocale
== NULL
)
424 wxLogError(_("locale '%s' can not be set."), szLocale
);
426 // the short name will be used to look for catalog files as well,
427 // so we need something here
428 if ( m_strShort
.IsEmpty() ) {
429 // FIXME I don't know how these 2 letter abbreviations are formed,
430 // this wild guess is surely wrong
431 m_strShort
= wxToLower(szLocale
[0]) + wxToLower(szLocale
[1]);
434 // save the old locale to be able to restore it later
435 m_pOldLocale
= wxSetLocale(this);
437 // load the default catalog with wxWindows standard messages
441 bOk
= AddCatalog("wxstd");
446 void wxLocale::AddCatalogLookupPathPrefix(const wxString
& prefix
)
448 if ( s_searchPrefixes
.Index(prefix
) == wxNOT_FOUND
)
450 s_searchPrefixes
.Add(prefix
);
452 //else: already have it
456 wxLocale::~wxLocale()
459 wxMsgCatalog
*pTmpCat
;
460 while ( m_pMsgCat
!= NULL
) {
462 m_pMsgCat
= m_pMsgCat
->m_pNext
;
466 // restore old locale
467 wxSetLocale(m_pOldLocale
);
468 setlocale(LC_ALL
, m_pszOldLocale
);
471 // get the translation of given string in current locale
472 const char *wxLocale::GetString(const char *szOrigString
,
473 const char *szDomain
) const
475 wxASSERT( szOrigString
!= NULL
); // would be pretty silly
477 const char *pszTrans
= NULL
;
479 wxMsgCatalog
*pMsgCat
;
480 if ( szDomain
!= NULL
) {
481 pMsgCat
= FindCatalog(szDomain
);
483 // does the catalog exist?
484 if ( pMsgCat
!= NULL
)
485 pszTrans
= pMsgCat
->GetString(szOrigString
);
488 // search in all domains
489 for ( pMsgCat
= m_pMsgCat
; pMsgCat
!= NULL
; pMsgCat
= pMsgCat
->m_pNext
) {
490 pszTrans
= pMsgCat
->GetString(szOrigString
);
491 if ( pszTrans
!= NULL
) // take the first found
496 if ( pszTrans
== NULL
) {
497 if ( wxIsLoggingTransErrors() ) {
498 // suppress further error messages if we're not debugging: this avoids
499 // flooding the user with messages about each and every missing string if,
500 // for example, a whole catalog file is missing.
502 // do it before calling LogWarning to prevent infinite recursion!
504 NoTransErr noTransErr
;
506 wxSuppressTransErrors();
507 #endif // debug/!debug
509 if ( szDomain
!= NULL
)
511 wxLogWarning(_("string '%s' not found in domain '%s' for locale '%s'."),
512 szOrigString
, szDomain
, m_strLocale
.c_str());
516 wxLogWarning(_("string '%s' not found in locale '%s'."),
517 szOrigString
, m_strLocale
.c_str());
527 // find catalog by name in a linked list, return NULL if !found
528 wxMsgCatalog
*wxLocale::FindCatalog(const char *szDomain
) const
530 // linear search in the linked list
531 wxMsgCatalog
*pMsgCat
;
532 for ( pMsgCat
= m_pMsgCat
; pMsgCat
!= NULL
; pMsgCat
= pMsgCat
->m_pNext
) {
533 if ( Stricmp(pMsgCat
->GetName(), szDomain
) == 0 )
540 // check if the given catalog is loaded
541 bool wxLocale::IsLoaded(const char *szDomain
) const
543 return FindCatalog(szDomain
) != NULL
;
546 // add a catalog to our linked list
547 bool wxLocale::AddCatalog(const char *szDomain
)
549 wxMsgCatalog
*pMsgCat
= new wxMsgCatalog
;
551 if ( pMsgCat
->Load(m_strShort
, szDomain
) ) {
552 // add it to the head of the list so that in GetString it will
553 // be searched before the catalogs added earlier
554 pMsgCat
->m_pNext
= m_pMsgCat
;
560 // don't add it because it couldn't be loaded anyway
567 // ----------------------------------------------------------------------------
568 // global functions and variables
569 // ----------------------------------------------------------------------------
571 // translation errors logging
572 // --------------------------
574 static bool gs_bGiveTransErrors
= TRUE
;
576 void wxSuppressTransErrors()
578 gs_bGiveTransErrors
= FALSE
;
581 void wxRestoreTransErrors()
583 gs_bGiveTransErrors
= TRUE
;
586 bool wxIsLoggingTransErrors()
588 return gs_bGiveTransErrors
;
591 // retrieve/change current locale
592 // ------------------------------
594 // the current locale object
595 wxLocale
*g_pLocale
= NULL
;
597 wxLocale
*wxGetLocale()
602 wxLocale
*wxSetLocale(wxLocale
*pLocale
)
604 wxLocale
*pOld
= g_pLocale
;