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
, bool bConvertEncoding
= FALSE
);
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
)); }
182 // convert encoding to platform native one, if neccessary
183 void ConvertEncoding();
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;
192 bool HasHashTable() const // true if hash table is present
193 { return m_nHashSize
> 2 && m_pHashTable
!= NULL
; }
195 bool m_bSwapped
; // wrong endianness?
197 wxChar
*m_pszName
; // name of the domain
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 // the list of the directories to search for message catalog files
205 static wxArrayString s_searchPrefixes
;
207 // ============================================================================
209 // ============================================================================
211 // ----------------------------------------------------------------------------
212 // wxMsgCatalog class
213 // ----------------------------------------------------------------------------
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
)
219 #define HASHWORDBITS 32 // the length of size_t32
223 while ( *sz
!= '\0' ) {
225 hval
+= (size_t32
)*sz
++;
226 g
= hval
& ((size_t32
)0xf << (HASHWORDBITS
- 4));
228 hval
^= g
>> (HASHWORDBITS
- 8);
236 // swap the 2 halves of 32 bit integer if needed
237 size_t32
wxMsgCatalog::Swap(size_t32 ui
) const
239 return m_bSwapped
? (ui
<< 24) | ((ui
& 0xff00) << 8) |
240 ((ui
>> 8) & 0xff00) | (ui
>> 24)
244 wxMsgCatalog::wxMsgCatalog()
250 wxMsgCatalog::~wxMsgCatalog()
253 wxDELETEA(m_pszName
);
256 // small class to suppress the translation erros until exit from current scope
260 NoTransErr() { wxSuppressTransErrors(); }
261 ~NoTransErr() { wxRestoreTransErrors(); }
264 // return all directories to search for given prefix
265 static wxString
GetAllMsgCatalogSubdirs(const wxChar
*prefix
,
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
;
280 // construct the search path for the given language
281 static wxString
GetFullSearchPath(const wxChar
*lang
)
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
++ )
289 searchPath
<< GetAllMsgCatalogSubdirs(s_searchPrefixes
[n
], lang
)
293 // then take the current directory
294 // FIXME it should be the directory of the executable
295 searchPath
<< GetAllMsgCatalogSubdirs(wxT("."), lang
) << wxPATH_SEP
;
297 // and finally add some standard ones
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
);
306 // open disk file and read in it's contents
307 bool wxMsgCatalog::Load(const wxChar
*szDirPrefix
, const wxChar
*szName0
, bool bConvertEncoding
)
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!
313 wxString szName
= szName0
;
314 if(szName
.Find(wxT('.')) != -1) // contains a dot
315 szName
= szName
.Left(szName
.Find(wxT('.')));
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
320 const wxChar
*pszLcPath
= wxGetenv("LC_PATH");
321 if ( pszLcPath
!= NULL
)
322 strPath
+= pszLcPath
+ wxString(szDirPrefix
) + MSG_PATH
;
325 wxString searchPath
= GetFullSearchPath(szDirPrefix
);
326 const wxChar
*sublocale
= wxStrchr(szDirPrefix
, wxT('_'));
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
332 searchPath
<< GetFullSearchPath(wxString(szDirPrefix
).
333 Left((size_t)(sublocale
- szDirPrefix
)))
337 wxString strFile
= szName
;
338 strFile
+= MSGCATALOG_EXTENSION
;
340 // don't give translation errors here because the wxstd catalog might
341 // not yet be loaded (and it's normal)
343 // (we're using an object because we have several return paths)
345 NoTransErr noTransErr
;
346 wxLogVerbose(wxT("looking for catalog '%s' in path '%s'."),
347 szName
.c_str(), searchPath
.c_str());
349 wxString strFullName
;
350 if ( !wxFindFileInPath(&strFullName
, searchPath
, strFile
) ) {
351 wxLogWarning(_("catalog file for domain '%s' not found."), szName
.c_str());
356 wxLogVerbose(_("using catalog '%s' from '%s'."),
357 szName
.c_str(), strFullName
.c_str());
359 wxFile
fileMsg(strFullName
);
360 if ( !fileMsg
.IsOpened() )
364 off_t nSize
= fileMsg
.Length();
365 if ( nSize
== wxInvalidOffset
)
368 // read the whole file in memory
369 m_pData
= new size_t8
[nSize
];
370 if ( fileMsg
.Read(m_pData
, nSize
) != nSize
) {
376 bool bValid
= (size_t)nSize
> sizeof(wxMsgCatalogHeader
);
378 wxMsgCatalogHeader
*pHeader
= (wxMsgCatalogHeader
*)m_pData
;
380 // we'll have to swap all the integers if it's true
381 m_bSwapped
= pHeader
->magic
== MSGCATALOG_MAGIC_SW
;
383 // check the magic number
384 bValid
= m_bSwapped
|| pHeader
->magic
== MSGCATALOG_MAGIC
;
388 // it's either too short or has incorrect magic number
389 wxLogWarning(_("'%s' is not a valid message catalog."), strFullName
.c_str());
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
));
402 m_nHashSize
= Swap(pHeader
->nHashSize
);
403 m_pHashTable
= (size_t32
*)(m_pData
+ Swap(pHeader
->ofsHashTable
));
405 m_pszName
= new wxChar
[wxStrlen(szName
) + 1];
406 wxStrcpy(m_pszName
, szName
);
408 if (bConvertEncoding
) ConvertEncoding();
410 // everything is fine
414 // search for a string
415 const char *wxMsgCatalog::GetString(const char *szOrig
) const
417 if ( szOrig
== NULL
)
420 if ( HasHashTable() ) { // use hash table for lookup if possible
421 size_t32 nHashVal
= GetHash(szOrig
);
422 size_t32 nIndex
= nHashVal
% m_nHashSize
;
424 size_t32 nIncr
= 1 + (nHashVal
% (m_nHashSize
- 2));
426 #if defined(__VISAGECPP__)
427 // VA just can't stand while(1) or while(TRUE)
433 size_t32 nStr
= Swap(m_pHashTable
[nIndex
]);
437 if ( strcmp(szOrig
, StringAtOfs(m_pOrigTable
, nStr
- 1)) == 0 )
438 return StringAtOfs(m_pTransTable
, nStr
- 1);
440 if ( nIndex
>= m_nHashSize
- nIncr
)
441 nIndex
-= m_nHashSize
- nIncr
;
446 else { // no hash table: use default binary search
450 while ( bottom
< top
) {
451 current
= (bottom
+ top
) / 2;
452 int res
= strcmp(szOrig
, StringAtOfs(m_pOrigTable
, current
));
456 bottom
= current
+ 1;
458 return StringAtOfs(m_pTransTable
, current
);
468 #include "wx/fontmap.h"
469 #include "wx/encconv.h"
472 void wxMsgCatalog::ConvertEncoding()
477 // first, find encoding header:
478 const char *hdr
= StringAtOfs(m_pOrigTable
, 0);
479 if (hdr
== NULL
) return; // not supported by this catalog, does not have non-fuzzy header
480 if (hdr
[0] != 0) return; // ditto
482 /* we support catalogs with header (msgid "") that is _not_ marked as "#, fuzzy" (otherwise
483 the string would not be included into compiled catalog) */
484 wxString
header(StringAtOfs(m_pTransTable
, 0));
486 int pos
= header
.Find(wxT("Content-Type: text/plain; charset="));
487 if (pos
== wxNOT_FOUND
)
488 return; // incorrectly filled Content-Type header
489 size_t n
= pos
+ 34; /*strlen("Content-Type: text/plain; charset=")*/
490 while (header
[n
] != wxT('\n'))
491 charset
<< header
[n
++];
493 enc
= wxTheFontMapper
->CharsetToEncoding(charset
, FALSE
);
494 if ( enc
== wxFONTENCODING_SYSTEM
)
495 return; // unknown encoding
497 wxFontEncodingArray a
= wxEncodingConverter::GetPlatformEquivalents(enc
);
499 return; // no conversion needed, locale uses native encoding
501 if (a
.GetCount() == 0)
502 return; // we don't know common equiv. under this platform
504 wxEncodingConverter converter
;
506 converter
.Init(enc
, a
[0]);
507 for (size_t i
= 0; i
< m_numStrings
; i
++)
508 converter
.Convert((char*)StringAtOfs(m_pTransTable
, i
));
513 // ----------------------------------------------------------------------------
515 // ----------------------------------------------------------------------------
519 m_pszOldLocale
= NULL
;
523 // NB: this function has (desired) side effect of changing current locale
524 bool wxLocale::Init(const wxChar
*szName
,
525 const wxChar
*szShort
,
526 const wxChar
*szLocale
,
528 bool bConvertEncoding
)
530 m_strLocale
= szName
;
531 m_strShort
= szShort
;
532 m_bConvertEncoding
= bConvertEncoding
;
534 // change current locale (default: same as long name)
535 if ( szLocale
== NULL
)
537 // the argument to setlocale()
540 m_pszOldLocale
= wxSetlocale(LC_ALL
, szLocale
);
541 if ( m_pszOldLocale
== NULL
)
542 wxLogError(_("locale '%s' can not be set."), szLocale
);
544 // the short name will be used to look for catalog files as well,
545 // so we need something here
546 if ( m_strShort
.IsEmpty() ) {
547 // FIXME I don't know how these 2 letter abbreviations are formed,
548 // this wild guess is surely wrong
549 m_strShort
= tolower(szLocale
[0]) + tolower(szLocale
[1]);
552 // save the old locale to be able to restore it later
553 m_pOldLocale
= wxSetLocale(this);
555 // load the default catalog with wxWindows standard messages
559 bOk
= AddCatalog(wxT("wxstd"));
564 void wxLocale::AddCatalogLookupPathPrefix(const wxString
& prefix
)
566 if ( s_searchPrefixes
.Index(prefix
) == wxNOT_FOUND
)
568 s_searchPrefixes
.Add(prefix
);
570 //else: already have it
574 wxLocale::~wxLocale()
577 wxMsgCatalog
*pTmpCat
;
578 while ( m_pMsgCat
!= NULL
) {
580 m_pMsgCat
= m_pMsgCat
->m_pNext
;
584 // restore old locale
585 wxSetLocale(m_pOldLocale
);
586 wxSetlocale(LC_ALL
, m_pszOldLocale
);
589 // get the translation of given string in current locale
590 const wxMB2WXbuf
wxLocale::GetString(const wxChar
*szOrigString
,
591 const wxChar
*szDomain
) const
593 if ( wxIsEmpty(szOrigString
) )
596 const char *pszTrans
= NULL
;
598 const wxWX2MBbuf szOrgString
= wxConvCurrent
->cWX2MB(szOrigString
);
600 #define szOrgString szOrigString
601 #endif // Unicode/ANSI
603 wxMsgCatalog
*pMsgCat
;
604 if ( szDomain
!= NULL
) {
605 pMsgCat
= FindCatalog(szDomain
);
607 // does the catalog exist?
608 if ( pMsgCat
!= NULL
)
609 pszTrans
= pMsgCat
->GetString(szOrgString
);
612 // search in all domains
613 for ( pMsgCat
= m_pMsgCat
; pMsgCat
!= NULL
; pMsgCat
= pMsgCat
->m_pNext
) {
614 pszTrans
= pMsgCat
->GetString(szOrgString
);
615 if ( pszTrans
!= NULL
) // take the first found
620 if ( pszTrans
== NULL
) {
621 if ( wxIsLoggingTransErrors() ) {
622 // suppress further error messages if we're not debugging: this avoids
623 // flooding the user with messages about each and every missing string if,
624 // for example, a whole catalog file is missing.
626 // do it before calling LogWarning to prevent infinite recursion!
628 NoTransErr noTransErr
;
630 wxSuppressTransErrors();
631 #endif // debug/!debug
633 if ( szDomain
!= NULL
)
635 wxLogWarning(_("string '%s' not found in domain '%s' for locale '%s'."),
636 szOrigString
, szDomain
, m_strLocale
.c_str());
640 wxLogWarning(_("string '%s' not found in locale '%s'."),
641 szOrigString
, m_strLocale
.c_str());
645 return (wxMB2WXbuf
)(szOrigString
);
649 return wxConvertMB2WX(pszTrans
); // or preferably wxCSConv(charset).cMB2WX(pszTrans) or something,
650 // a macro similar to wxConvertMB2WX could be written for that
656 // find catalog by name in a linked list, return NULL if !found
657 wxMsgCatalog
*wxLocale::FindCatalog(const wxChar
*szDomain
) const
659 // linear search in the linked list
660 wxMsgCatalog
*pMsgCat
;
661 for ( pMsgCat
= m_pMsgCat
; pMsgCat
!= NULL
; pMsgCat
= pMsgCat
->m_pNext
) {
662 if ( wxStricmp(pMsgCat
->GetName(), szDomain
) == 0 )
669 // check if the given catalog is loaded
670 bool wxLocale::IsLoaded(const wxChar
*szDomain
) const
672 return FindCatalog(szDomain
) != NULL
;
675 // add a catalog to our linked list
676 bool wxLocale::AddCatalog(const wxChar
*szDomain
)
678 wxMsgCatalog
*pMsgCat
= new wxMsgCatalog
;
680 if ( pMsgCat
->Load(m_strShort
, szDomain
, m_bConvertEncoding
) ) {
681 // add it to the head of the list so that in GetString it will
682 // be searched before the catalogs added earlier
683 pMsgCat
->m_pNext
= m_pMsgCat
;
689 // don't add it because it couldn't be loaded anyway
696 // ----------------------------------------------------------------------------
697 // global functions and variables
698 // ----------------------------------------------------------------------------
700 // translation errors logging
701 // --------------------------
703 static bool gs_bGiveTransErrors
= TRUE
;
705 void wxSuppressTransErrors()
707 gs_bGiveTransErrors
= FALSE
;
710 void wxRestoreTransErrors()
712 gs_bGiveTransErrors
= TRUE
;
715 bool wxIsLoggingTransErrors()
717 return gs_bGiveTransErrors
;
720 // retrieve/change current locale
721 // ------------------------------
723 // the current locale object
724 static wxLocale
*g_pLocale
= NULL
;
726 wxLocale
*wxGetLocale()
731 wxLocale
*wxSetLocale(wxLocale
*pLocale
)
733 wxLocale
*pOld
= g_pLocale
;