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 _T(".mo")
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
108 // small class to suppress the translation erros until exit from current scope
112 NoTransErr() { ms_suppressCount
++; }
113 ~NoTransErr() { ms_suppressCount
--; }
115 static bool Suppress() { return ms_suppressCount
> 0; }
118 static size_t ms_suppressCount
;
121 size_t NoTransErr::ms_suppressCount
= 0;
132 #endif // Debug/!Debug
134 static wxLocale
*wxSetLocale(wxLocale
*pLocale
);
136 // ----------------------------------------------------------------------------
137 // wxMsgCatalog corresponds to one disk-file message catalog.
139 // This is a "low-level" class and is used only by wxLocale (that's why
140 // it's designed to be stored in a linked list)
141 // ----------------------------------------------------------------------------
150 // load the catalog from disk (szDirPrefix corresponds to language)
151 bool Load(const wxChar
*szDirPrefix
, const wxChar
*szName
, bool bConvertEncoding
= FALSE
);
152 bool IsLoaded() const { return m_pData
!= NULL
; }
154 // get name of the catalog
155 const wxChar
*GetName() const { return m_pszName
; }
157 // get the translated string: returns NULL if not found
158 const char *GetString(const char *sz
) const;
160 // public variable pointing to the next element in a linked list (or NULL)
161 wxMsgCatalog
*m_pNext
;
164 // this implementation is binary compatible with GNU gettext() version 0.10
166 // an entry in the string table
167 struct wxMsgTableEntry
169 size_t32 nLen
; // length of the string
170 size_t32 ofsString
; // pointer to the string
173 // header of a .mo file
174 struct wxMsgCatalogHeader
176 size_t32 magic
, // offset +00: magic id
177 revision
, // +04: revision
178 numStrings
; // +08: number of strings in the file
179 size_t32 ofsOrigTable
, // +0C: start of original string table
180 ofsTransTable
; // +10: start of translated string table
181 size_t32 nHashSize
, // +14: hash table size
182 ofsHashTable
; // +18: offset of hash table start
185 // all data is stored here, NULL if no data loaded
189 size_t32 m_numStrings
, // number of strings in this domain
190 m_nHashSize
; // number of entries in hash table
191 size_t32
*m_pHashTable
; // pointer to hash table
192 wxMsgTableEntry
*m_pOrigTable
, // pointer to original strings
193 *m_pTransTable
; // translated
195 const char *StringAtOfs(wxMsgTableEntry
*pTable
, size_t32 index
) const
196 { return (const char *)(m_pData
+ Swap(pTable
[index
].ofsString
)); }
198 // convert encoding to platform native one, if neccessary
199 void ConvertEncoding();
202 // calculate the hash value of given string
203 static inline size_t32
GetHash(const char *sz
);
204 // big<->little endian
205 inline size_t32
Swap(size_t32 ui
) const;
208 bool HasHashTable() const // true if hash table is present
209 { return m_nHashSize
> 2 && m_pHashTable
!= NULL
; }
211 bool m_bSwapped
; // wrong endianness?
213 wxChar
*m_pszName
; // name of the domain
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 // the list of the directories to search for message catalog files
221 static wxArrayString s_searchPrefixes
;
223 // ============================================================================
225 // ============================================================================
227 // ----------------------------------------------------------------------------
228 // wxMsgCatalog class
229 // ----------------------------------------------------------------------------
231 // calculate hash value using the so called hashpjw function by P.J. Weinberger
232 // [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools]
233 size_t32
wxMsgCatalog::GetHash(const char *sz
)
235 #define HASHWORDBITS 32 // the length of size_t32
239 while ( *sz
!= '\0' ) {
241 hval
+= (size_t32
)*sz
++;
242 g
= hval
& ((size_t32
)0xf << (HASHWORDBITS
- 4));
244 hval
^= g
>> (HASHWORDBITS
- 8);
252 // swap the 2 halves of 32 bit integer if needed
253 size_t32
wxMsgCatalog::Swap(size_t32 ui
) const
255 return m_bSwapped
? (ui
<< 24) | ((ui
& 0xff00) << 8) |
256 ((ui
>> 8) & 0xff00) | (ui
>> 24)
260 wxMsgCatalog::wxMsgCatalog()
266 wxMsgCatalog::~wxMsgCatalog()
269 wxDELETEA(m_pszName
);
272 // return all directories to search for given prefix
273 static wxString
GetAllMsgCatalogSubdirs(const wxChar
*prefix
,
278 // search first in prefix/fr/LC_MESSAGES, then in prefix/fr and finally in
279 // prefix (assuming the language is 'fr')
280 searchPath
<< prefix
<< wxFILE_SEP_PATH
<< lang
<< wxFILE_SEP_PATH
281 << wxT("LC_MESSAGES") << wxPATH_SEP
282 << prefix
<< wxFILE_SEP_PATH
<< lang
<< wxPATH_SEP
283 << prefix
<< wxPATH_SEP
;
288 // construct the search path for the given language
289 static wxString
GetFullSearchPath(const wxChar
*lang
)
293 // first take the entries explicitly added by the program
294 size_t count
= s_searchPrefixes
.Count();
295 for ( size_t n
= 0; n
< count
; n
++ )
297 searchPath
<< GetAllMsgCatalogSubdirs(s_searchPrefixes
[n
], lang
)
301 // LC_PATH is a standard env var containing the search path for the .mo
303 const wxChar
*pszLcPath
= wxGetenv(wxT("LC_PATH"));
304 if ( pszLcPath
!= NULL
)
305 searchPath
<< GetAllMsgCatalogSubdirs(pszLcPath
, lang
);
307 // then take the current directory
308 // FIXME it should be the directory of the executable
309 searchPath
<< GetAllMsgCatalogSubdirs(wxT("."), lang
);
311 // and finally add some standard ones
313 << GetAllMsgCatalogSubdirs(wxT("/usr/share/locale"), lang
)
314 << GetAllMsgCatalogSubdirs(wxT("/usr/lib/locale"), lang
)
315 << GetAllMsgCatalogSubdirs(wxT("/usr/local/share/locale"), lang
);
320 // open disk file and read in it's contents
321 bool wxMsgCatalog::Load(const wxChar
*szDirPrefix
, const wxChar
*szName0
, bool bConvertEncoding
)
323 /* We need to handle locales like de_AT.iso-8859-1
324 For this we first chop off the .CHARSET specifier and ignore it.
325 FIXME: UNICODE SUPPORT: must use CHARSET specifier!
327 wxString szName
= szName0
;
328 if(szName
.Find(wxT('.')) != -1) // contains a dot
329 szName
= szName
.Left(szName
.Find(wxT('.')));
331 wxString searchPath
= GetFullSearchPath(szDirPrefix
);
332 const wxChar
*sublocale
= wxStrchr(szDirPrefix
, wxT('_'));
335 // also add just base locale name: for things like "fr_BE" (belgium
336 // french) we should use "fr" if no belgium specific message catalogs
338 searchPath
<< GetFullSearchPath(wxString(szDirPrefix
).
339 Left((size_t)(sublocale
- szDirPrefix
)))
343 wxString strFile
= szName
;
344 strFile
+= MSGCATALOG_EXTENSION
;
346 // don't give translation errors here because the wxstd catalog might
347 // not yet be loaded (and it's normal)
349 // (we're using an object because we have several return paths)
351 NoTransErr noTransErr
;
352 wxLogVerbose(_("looking for catalog '%s' in path '%s'."),
353 szName
.c_str(), searchPath
.c_str());
355 wxString strFullName
;
356 if ( !wxFindFileInPath(&strFullName
, searchPath
, strFile
) ) {
357 wxLogWarning(_("catalog file for domain '%s' not found."), szName
.c_str());
362 wxLogVerbose(_("using catalog '%s' from '%s'."),
363 szName
.c_str(), strFullName
.c_str());
365 wxFile
fileMsg(strFullName
);
366 if ( !fileMsg
.IsOpened() )
370 off_t nSize
= fileMsg
.Length();
371 if ( nSize
== wxInvalidOffset
)
374 // read the whole file in memory
375 m_pData
= new size_t8
[nSize
];
376 if ( fileMsg
.Read(m_pData
, nSize
) != nSize
) {
382 bool bValid
= (size_t)nSize
> sizeof(wxMsgCatalogHeader
);
384 wxMsgCatalogHeader
*pHeader
= (wxMsgCatalogHeader
*)m_pData
;
386 // we'll have to swap all the integers if it's true
387 m_bSwapped
= pHeader
->magic
== MSGCATALOG_MAGIC_SW
;
389 // check the magic number
390 bValid
= m_bSwapped
|| pHeader
->magic
== MSGCATALOG_MAGIC
;
394 // it's either too short or has incorrect magic number
395 wxLogWarning(_("'%s' is not a valid message catalog."), strFullName
.c_str());
402 m_numStrings
= Swap(pHeader
->numStrings
);
403 m_pOrigTable
= (wxMsgTableEntry
*)(m_pData
+
404 Swap(pHeader
->ofsOrigTable
));
405 m_pTransTable
= (wxMsgTableEntry
*)(m_pData
+
406 Swap(pHeader
->ofsTransTable
));
408 m_nHashSize
= Swap(pHeader
->nHashSize
);
409 m_pHashTable
= (size_t32
*)(m_pData
+ Swap(pHeader
->ofsHashTable
));
411 m_pszName
= new wxChar
[wxStrlen(szName
) + 1];
412 wxStrcpy(m_pszName
, szName
);
414 if (bConvertEncoding
)
417 // everything is fine
421 // search for a string
422 const char *wxMsgCatalog::GetString(const char *szOrig
) const
424 if ( szOrig
== NULL
)
427 if ( HasHashTable() ) { // use hash table for lookup if possible
428 size_t32 nHashVal
= GetHash(szOrig
);
429 size_t32 nIndex
= nHashVal
% m_nHashSize
;
431 size_t32 nIncr
= 1 + (nHashVal
% (m_nHashSize
- 2));
433 #if defined(__VISAGECPP__)
434 // VA just can't stand while(1) or while(TRUE)
440 size_t32 nStr
= Swap(m_pHashTable
[nIndex
]);
444 if ( strcmp(szOrig
, StringAtOfs(m_pOrigTable
, nStr
- 1)) == 0 )
445 return StringAtOfs(m_pTransTable
, nStr
- 1);
447 if ( nIndex
>= m_nHashSize
- nIncr
)
448 nIndex
-= m_nHashSize
- nIncr
;
453 else { // no hash table: use default binary search
457 while ( bottom
< top
) {
458 current
= (bottom
+ top
) / 2;
459 int res
= strcmp(szOrig
, StringAtOfs(m_pOrigTable
, current
));
463 bottom
= current
+ 1;
465 return StringAtOfs(m_pTransTable
, current
);
475 #include "wx/fontmap.h"
476 #include "wx/encconv.h"
479 void wxMsgCatalog::ConvertEncoding()
484 // first, find encoding header:
485 const char *hdr
= StringAtOfs(m_pOrigTable
, 0);
486 if (hdr
== NULL
) return; // not supported by this catalog, does not have non-fuzzy header
487 if (hdr
[0] != 0) return; // ditto
489 /* we support catalogs with header (msgid "") that is _not_ marked as "#, fuzzy" (otherwise
490 the string would not be included into compiled catalog) */
491 wxString
header(StringAtOfs(m_pTransTable
, 0));
493 int pos
= header
.Find(wxT("Content-Type: text/plain; charset="));
494 if (pos
== wxNOT_FOUND
)
495 return; // incorrectly filled Content-Type header
496 size_t n
= pos
+ 34; /*strlen("Content-Type: text/plain; charset=")*/
497 while (header
[n
] != wxT('\n'))
498 charset
<< header
[n
++];
500 enc
= wxTheFontMapper
->CharsetToEncoding(charset
, FALSE
);
501 if ( enc
== wxFONTENCODING_SYSTEM
)
502 return; // unknown encoding
504 wxFontEncodingArray a
= wxEncodingConverter::GetPlatformEquivalents(enc
);
506 return; // no conversion needed, locale uses native encoding
508 if (a
.GetCount() == 0)
509 return; // we don't know common equiv. under this platform
511 wxEncodingConverter converter
;
513 converter
.Init(enc
, a
[0]);
514 for (size_t i
= 0; i
< m_numStrings
; i
++)
515 converter
.Convert((char*)StringAtOfs(m_pTransTable
, i
));
520 // ----------------------------------------------------------------------------
522 // ----------------------------------------------------------------------------
526 m_pszOldLocale
= NULL
;
530 // NB: this function has (desired) side effect of changing current locale
531 bool wxLocale::Init(const wxChar
*szName
,
532 const wxChar
*szShort
,
533 const wxChar
*szLocale
,
535 bool bConvertEncoding
)
537 m_strLocale
= szName
;
538 m_strShort
= szShort
;
539 m_bConvertEncoding
= bConvertEncoding
;
541 // change current locale (default: same as long name)
542 if ( szLocale
== NULL
)
544 // the argument to setlocale()
547 m_pszOldLocale
= wxSetlocale(LC_ALL
, szLocale
);
548 if ( m_pszOldLocale
== NULL
)
549 wxLogError(_("locale '%s' can not be set."), szLocale
);
551 // the short name will be used to look for catalog files as well,
552 // so we need something here
553 if ( m_strShort
.IsEmpty() ) {
554 // FIXME I don't know how these 2 letter abbreviations are formed,
555 // this wild guess is surely wrong
556 m_strShort
= tolower(szLocale
[0]) + tolower(szLocale
[1]);
559 // save the old locale to be able to restore it later
560 m_pOldLocale
= wxSetLocale(this);
562 // load the default catalog with wxWindows standard messages
566 bOk
= AddCatalog(wxT("wxstd"));
571 void wxLocale::AddCatalogLookupPathPrefix(const wxString
& prefix
)
573 if ( s_searchPrefixes
.Index(prefix
) == wxNOT_FOUND
)
575 s_searchPrefixes
.Add(prefix
);
577 //else: already have it
581 wxLocale::~wxLocale()
584 wxMsgCatalog
*pTmpCat
;
585 while ( m_pMsgCat
!= NULL
) {
587 m_pMsgCat
= m_pMsgCat
->m_pNext
;
591 // restore old locale
592 wxSetLocale(m_pOldLocale
);
593 wxSetlocale(LC_ALL
, m_pszOldLocale
);
596 // get the translation of given string in current locale
597 const wxMB2WXbuf
wxLocale::GetString(const wxChar
*szOrigString
,
598 const wxChar
*szDomain
) const
600 if ( wxIsEmpty(szOrigString
) )
603 const char *pszTrans
= NULL
;
605 const wxWX2MBbuf szOrgString
= wxConvCurrent
->cWX2MB(szOrigString
);
607 #define szOrgString szOrigString
608 #endif // Unicode/ANSI
610 wxMsgCatalog
*pMsgCat
;
611 if ( szDomain
!= NULL
) {
612 pMsgCat
= FindCatalog(szDomain
);
614 // does the catalog exist?
615 if ( pMsgCat
!= NULL
)
616 pszTrans
= pMsgCat
->GetString(szOrgString
);
619 // search in all domains
620 for ( pMsgCat
= m_pMsgCat
; pMsgCat
!= NULL
; pMsgCat
= pMsgCat
->m_pNext
) {
621 pszTrans
= pMsgCat
->GetString(szOrgString
);
622 if ( pszTrans
!= NULL
) // take the first found
627 if ( pszTrans
== NULL
) {
629 if ( !NoTransErr::Suppress() ) {
630 NoTransErr noTransErr
;
632 if ( szDomain
!= NULL
)
634 wxLogDebug(_T("string '%s' not found in domain '%s' for locale '%s'."),
635 szOrigString
, szDomain
, m_strLocale
.c_str());
639 wxLogDebug(_T("string '%s' not found in locale '%s'."),
640 szOrigString
, m_strLocale
.c_str());
643 #endif // __WXDEBUG__
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 // retrieve/change current locale
701 // ------------------------------
703 // the current locale object
704 static wxLocale
*g_pLocale
= NULL
;
706 wxLocale
*wxGetLocale()
711 wxLocale
*wxSetLocale(wxLocale
*pLocale
)
713 wxLocale
*pOld
= g_pLocale
;