1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/intl.cpp
3 // Purpose: Internationalization and localisation for wxWidgets
4 // Author: Vadim Zeitlin
5 // Modified by: Michael N. Filippov <michael@idisys.iae.nsk.su>
6 // (2003/09/30 - PluralForms support)
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 // The following define is needed by Innotek's libc to
29 // make the definition of struct localeconv available.
30 #define __INTERNAL_DEFS
36 #include "wx/dynarray.h"
37 #include "wx/string.h"
42 #include "wx/hashmap.h"
43 #include "wx/module.h"
53 #ifdef HAVE_LANGINFO_H
58 #include "wx/msw/private.h"
62 #include "wx/filename.h"
63 #include "wx/tokenzr.h"
64 #include "wx/fontmap.h"
65 #include "wx/scopedptr.h"
66 #include "wx/apptrait.h"
67 #include "wx/stdpaths.h"
68 #include "wx/hashset.h"
70 #if defined(__WXOSX__)
71 #include "wx/osx/core/cfref.h"
72 #include <CoreFoundation/CFLocale.h>
73 #include <CoreFoundation/CFDateFormatter.h>
74 #include "wx/osx/core/cfstring.h"
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 #define TRACE_I18N wxS("i18n")
83 // ============================================================================
85 // ============================================================================
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 static wxLocale
*wxSetLocale(wxLocale
*pLocale
);
96 // get just the language part ("en" in "en_GB")
97 inline wxString
ExtractLang(const wxString
& langFull
)
99 return langFull
.BeforeFirst('_');
102 // helper functions of GetSystemLanguage()
105 // get everything else (including the leading '_')
106 inline wxString
ExtractNotLang(const wxString
& langFull
)
108 size_t pos
= langFull
.find('_');
109 if ( pos
!= wxString::npos
)
110 return langFull
.substr(pos
);
117 } // anonymous namespace
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
125 // helper used by wxLanguageInfo::GetLocaleName() and elsewhere to determine
126 // whether the locale is Unicode-only (it is if this function returns empty
128 static wxString
wxGetANSICodePageForLocale(LCID lcid
)
133 if ( ::GetLocaleInfo(lcid
, LOCALE_IDEFAULTANSICODEPAGE
,
134 buffer
, WXSIZEOF(buffer
)) > 0 )
136 if ( buffer
[0] != wxT('0') || buffer
[1] != wxT('\0') )
138 //else: this locale doesn't use ANSI code page
144 wxUint32
wxLanguageInfo::GetLCID() const
146 return MAKELCID(MAKELANGID(WinLang
, WinSublang
), SORT_DEFAULT
);
149 wxString
wxLanguageInfo::GetLocaleName() const
153 const LCID lcid
= GetLCID();
156 buffer
[0] = wxT('\0');
157 if ( !::GetLocaleInfo(lcid
, LOCALE_SENGLANGUAGE
, buffer
, WXSIZEOF(buffer
)) )
159 wxLogLastError(wxT("GetLocaleInfo(LOCALE_SENGLANGUAGE)"));
164 if ( ::GetLocaleInfo(lcid
, LOCALE_SENGCOUNTRY
,
165 buffer
, WXSIZEOF(buffer
)) > 0 )
167 locale
<< wxT('_') << buffer
;
170 const wxString cp
= wxGetANSICodePageForLocale(lcid
);
173 locale
<< wxT('.') << cp
;
179 #endif // __WINDOWS__
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 #include "wx/arrimpl.cpp"
186 WX_DECLARE_EXPORTED_OBJARRAY(wxLanguageInfo
, wxLanguageInfoArray
);
187 WX_DEFINE_OBJARRAY(wxLanguageInfoArray
)
189 wxLanguageInfoArray
*wxLocale::ms_languagesDB
= NULL
;
191 /*static*/ void wxLocale::CreateLanguagesDB()
193 if (ms_languagesDB
== NULL
)
195 ms_languagesDB
= new wxLanguageInfoArray
;
200 /*static*/ void wxLocale::DestroyLanguagesDB()
202 wxDELETE(ms_languagesDB
);
206 void wxLocale::DoCommonInit()
208 // Store the current locale in order to be able to restore it in the dtor.
209 m_pszOldLocale
= wxSetlocale(LC_ALL
, NULL
);
210 if ( m_pszOldLocale
)
211 m_pszOldLocale
= wxStrdup(m_pszOldLocale
);
214 m_pOldLocale
= wxSetLocale(this);
216 // Set translations object, but only if the user didn't do so yet.
217 // This is to preserve compatibility with wx-2.8 where wxLocale was
218 // the only API for translations. wxLocale works as a stack, with
219 // latest-created one being the active one:
220 // wxLocale loc_fr(wxLANGUAGE_FRENCH);
221 // // _() returns French
223 // wxLocale loc_cs(wxLANGUAGE_CZECH);
224 // // _() returns Czech
226 // // _() returns French again
227 wxTranslations
*oldTrans
= wxTranslations::Get();
229 (m_pOldLocale
&& oldTrans
== &m_pOldLocale
->m_translations
) )
231 wxTranslations::SetNonOwned(&m_translations
);
234 m_language
= wxLANGUAGE_UNKNOWN
;
235 m_initialized
= false;
238 // NB: this function has (desired) side effect of changing current locale
239 bool wxLocale::Init(const wxString
& name
,
240 const wxString
& shortName
,
241 const wxString
& locale
,
243 #if WXWIN_COMPATIBILITY_2_8
244 ,bool WXUNUSED_UNLESS_DEBUG(bConvertEncoding
)
248 #if WXWIN_COMPATIBILITY_2_8
249 wxASSERT_MSG( bConvertEncoding
,
250 wxS("wxLocale::Init with bConvertEncoding=false is no longer supported, add charset to your catalogs") );
253 bool ret
= DoInit(name
, shortName
, locale
);
255 // NB: don't use 'lang' here, 'language' may be wxLANGUAGE_DEFAULT
256 wxTranslations
*t
= wxTranslations::Get();
259 t
->SetLanguage(shortName
);
268 bool wxLocale::DoInit(const wxString
& name
,
269 const wxString
& shortName
,
270 const wxString
& locale
)
272 wxASSERT_MSG( !m_initialized
,
273 wxS("you can't call wxLocale::Init more than once") );
275 m_initialized
= true;
277 m_strShort
= shortName
;
278 m_language
= wxLANGUAGE_UNKNOWN
;
280 // change current locale (default: same as long name)
281 wxString
szLocale(locale
);
282 if ( szLocale
.empty() )
284 // the argument to setlocale()
285 szLocale
= shortName
;
287 wxCHECK_MSG( !szLocale
.empty(), false,
288 wxS("no locale to set in wxLocale::Init()") );
291 if ( !wxSetlocale(LC_ALL
, szLocale
) )
293 wxLogError(_("locale '%s' cannot be set."), szLocale
);
296 // the short name will be used to look for catalog files as well,
297 // so we need something here
298 if ( m_strShort
.empty() ) {
299 // FIXME I don't know how these 2 letter abbreviations are formed,
300 // this wild guess is surely wrong
301 if ( !szLocale
.empty() )
303 m_strShort
+= (wxChar
)wxTolower(szLocale
[0]);
304 if ( szLocale
.length() > 1 )
305 m_strShort
+= (wxChar
)wxTolower(szLocale
[1]);
313 #if defined(__UNIX__) && wxUSE_UNICODE && !defined(__WXMAC__)
314 static const char *wxSetlocaleTryUTF8(int c
, const wxString
& lc
)
316 const char *l
= NULL
;
318 // NB: We prefer to set UTF-8 locale if it's possible and only fall back to
319 // non-UTF-8 locale if it fails
325 buf2
= buf
+ wxS(".UTF-8");
326 l
= wxSetlocale(c
, buf2
);
329 buf2
= buf
+ wxS(".utf-8");
330 l
= wxSetlocale(c
, buf2
);
334 buf2
= buf
+ wxS(".UTF8");
335 l
= wxSetlocale(c
, buf2
);
339 buf2
= buf
+ wxS(".utf8");
340 l
= wxSetlocale(c
, buf2
);
344 // if we can't set UTF-8 locale, try non-UTF-8 one:
346 l
= wxSetlocale(c
, lc
);
351 #define wxSetlocaleTryUTF8(c, lc) wxSetlocale(c, lc)
354 bool wxLocale::Init(int language
, int flags
)
356 #if WXWIN_COMPATIBILITY_2_8
357 wxASSERT_MSG( !(flags
& wxLOCALE_CONV_ENCODING
),
358 wxS("wxLOCALE_CONV_ENCODING is no longer supported, add charset to your catalogs") );
364 if (lang
== wxLANGUAGE_DEFAULT
)
366 // auto detect the language
367 lang
= GetSystemLanguage();
370 // We failed to detect system language, so we will use English:
371 if (lang
== wxLANGUAGE_UNKNOWN
)
376 const wxLanguageInfo
*info
= GetLanguageInfo(lang
);
381 wxLogError(wxS("Unknown language %i."), lang
);
385 wxString name
= info
->Description
;
386 wxString canonical
= info
->CanonicalName
;
391 const char *retloc
= wxSetlocale(LC_ALL
, wxEmptyString
);
392 #elif defined(__UNIX__) && !defined(__WXMAC__)
393 if (language
!= wxLANGUAGE_DEFAULT
)
394 locale
= info
->CanonicalName
;
396 const char *retloc
= wxSetlocaleTryUTF8(LC_ALL
, locale
);
398 const wxString langOnly
= ExtractLang(locale
);
401 // Some C libraries don't like xx_YY form and require xx only
402 retloc
= wxSetlocaleTryUTF8(LC_ALL
, langOnly
);
406 // some systems (e.g. FreeBSD and HP-UX) don't have xx_YY aliases but
407 // require the full xx_YY.encoding form, so try using UTF-8 because this is
408 // the only thing we can do generically
410 // TODO: add encodings applicable to each language to the lang DB and try
411 // them all in turn here
414 const wxChar
**names
=
415 wxFontMapperBase::GetAllEncodingNames(wxFONTENCODING_UTF8
);
418 retloc
= wxSetlocale(LC_ALL
, locale
+ wxS('.') + *names
++);
423 #endif // wxUSE_FONTMAP
427 // Some C libraries (namely glibc) still use old ISO 639,
428 // so will translate the abbrev for them
430 if ( langOnly
== wxS("he") )
431 localeAlt
= wxS("iw") + ExtractNotLang(locale
);
432 else if ( langOnly
== wxS("id") )
433 localeAlt
= wxS("in") + ExtractNotLang(locale
);
434 else if ( langOnly
== wxS("yi") )
435 localeAlt
= wxS("ji") + ExtractNotLang(locale
);
436 else if ( langOnly
== wxS("nb") )
437 localeAlt
= wxS("no_NO");
438 else if ( langOnly
== wxS("nn") )
439 localeAlt
= wxS("no_NY");
441 if ( !localeAlt
.empty() )
443 retloc
= wxSetlocaleTryUTF8(LC_ALL
, localeAlt
);
445 retloc
= wxSetlocaleTryUTF8(LC_ALL
, ExtractLang(localeAlt
));
453 // at least in AIX 5.2 libc is buggy and the string returned from
454 // setlocale(LC_ALL) can't be passed back to it because it returns 6
455 // strings (one for each locale category), i.e. for C locale we get back
458 // this contradicts IBM own docs but this is not of much help, so just work
459 // around it in the crudest possible manner
460 char* p
= const_cast<char*>(wxStrchr(retloc
, ' '));
465 #elif defined(__WIN32__)
466 const char *retloc
= "C";
467 if ( language
!= wxLANGUAGE_DEFAULT
)
469 if ( info
->WinLang
== 0 )
471 wxLogWarning(wxS("Locale '%s' not supported by OS."), name
.c_str());
472 // retloc already set to "C"
474 else // language supported by Windows
476 // Windows CE doesn't have SetThreadLocale() and there doesn't seem
477 // to be any equivalent
479 const wxUint32 lcid
= info
->GetLCID();
481 // change locale used by Windows functions
482 ::SetThreadLocale(lcid
);
485 // and also call setlocale() to change locale used by the CRT
486 locale
= info
->GetLocaleName();
487 if ( locale
.empty() )
491 else // have a valid locale
493 retloc
= wxSetlocale(LC_ALL
, locale
);
497 else // language == wxLANGUAGE_DEFAULT
499 retloc
= wxSetlocale(LC_ALL
, wxEmptyString
);
502 #if wxUSE_UNICODE && (defined(__VISUALC__) || defined(__MINGW32__))
503 // VC++ setlocale() (also used by Mingw) can't set locale to languages that
504 // can only be written using Unicode, therefore wxSetlocale() call fails
505 // for such languages but we don't want to report it as an error -- so that
506 // at least message catalogs can be used.
509 if ( wxGetANSICodePageForLocale(LOCALE_USER_DEFAULT
).empty() )
511 // we set the locale to a Unicode-only language, don't treat the
512 // inability of CRT to use it as an error
516 #endif // CRT not handling Unicode-only languages
520 #elif defined(__WXMAC__)
521 if (lang
== wxLANGUAGE_DEFAULT
)
522 locale
= wxEmptyString
;
524 locale
= info
->CanonicalName
;
526 const char *retloc
= wxSetlocale(LC_ALL
, locale
);
530 // Some C libraries don't like xx_YY form and require xx only
531 retloc
= wxSetlocale(LC_ALL
, ExtractLang(locale
));
536 #define WX_NO_LOCALE_SUPPORT
539 #ifndef WX_NO_LOCALE_SUPPORT
542 wxLogWarning(_("Cannot set locale to language \"%s\"."), name
.c_str());
544 // continue nevertheless and try to load at least the translations for
548 if ( !DoInit(name
, canonical
, retloc
) )
553 if (IsOk()) // setlocale() succeeded
556 // NB: don't use 'lang' here, 'language'
557 wxTranslations
*t
= wxTranslations::Get();
560 t
->SetLanguage(static_cast<wxLanguage
>(language
));
562 if ( flags
& wxLOCALE_LOAD_DEFAULT
)
567 #endif // !WX_NO_LOCALE_SUPPORT
574 // Small helper function: get the value of the given environment variable and
575 // return true only if the variable was found and has non-empty value.
576 inline bool wxGetNonEmptyEnvVar(const wxString
& name
, wxString
* value
)
578 return wxGetEnv(name
, value
) && !value
->empty();
582 } // anonymous namespace
584 /*static*/ int wxLocale::GetSystemLanguage()
588 // init i to avoid compiler warning
590 count
= ms_languagesDB
->GetCount();
592 #if defined(__UNIX__)
593 // first get the string identifying the language from the environment
596 wxCFRef
<CFLocaleRef
> userLocaleRef(CFLocaleCopyCurrent());
598 // because the locale identifier (kCFLocaleIdentifier) is formatted a little bit differently, eg
599 // az_Cyrl_AZ@calendar=buddhist;currency=JPY we just recreate the base info as expected by wx here
601 wxCFStringRef
str(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleLanguageCode
)));
602 langFull
= str
.AsString()+"_";
603 str
.reset(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleCountryCode
)));
604 langFull
+= str
.AsString();
606 if (!wxGetNonEmptyEnvVar(wxS("LC_ALL"), &langFull
) &&
607 !wxGetNonEmptyEnvVar(wxS("LC_MESSAGES"), &langFull
) &&
608 !wxGetNonEmptyEnvVar(wxS("LANG"), &langFull
))
610 // no language specified, treat it as English
611 return wxLANGUAGE_ENGLISH_US
;
614 if ( langFull
== wxS("C") || langFull
== wxS("POSIX") )
616 // default C locale is English too
617 return wxLANGUAGE_ENGLISH_US
;
621 // the language string has the following form
623 // lang[_LANG][.encoding][@modifier]
625 // (see environ(5) in the Open Unix specification)
627 // where lang is the primary language, LANG is a sublang/territory,
628 // encoding is the charset to use and modifier "allows the user to select
629 // a specific instance of localization data within a single category"
631 // for example, the following strings are valid:
636 // de_DE.iso88591@euro
638 // for now we don't use the encoding, although we probably should (doing
639 // translations of the msg catalogs on the fly as required) (TODO)
641 // we need the modified for languages like Valencian: ca_ES@valencia
642 // though, remember it
644 size_t posModifier
= langFull
.find_first_of(wxS("@"));
645 if ( posModifier
!= wxString::npos
)
646 modifier
= langFull
.Mid(posModifier
);
648 size_t posEndLang
= langFull
.find_first_of(wxS("@."));
649 if ( posEndLang
!= wxString::npos
)
651 langFull
.Truncate(posEndLang
);
654 // do we have just the language (or sublang too)?
655 const bool justLang
= langFull
.find('_') == wxString::npos
;
657 // 0. Make sure the lang is according to latest ISO 639
658 // (this is necessary because glibc uses iw and in instead
659 // of he and id respectively).
661 // the language itself (second part is the dialect/sublang)
662 wxString langOrig
= ExtractLang(langFull
);
665 if ( langOrig
== wxS("iw"))
667 else if (langOrig
== wxS("in"))
669 else if (langOrig
== wxS("ji"))
671 else if (langOrig
== wxS("no_NO"))
673 else if (langOrig
== wxS("no_NY"))
675 else if (langOrig
== wxS("no"))
681 if ( lang
!= langOrig
)
683 langFull
= lang
+ ExtractNotLang(langFull
);
686 // 1. Try to find the language either as is:
687 // a) With modifier if set
688 if ( !modifier
.empty() )
690 wxString langFullWithModifier
= langFull
+ modifier
;
691 for ( i
= 0; i
< count
; i
++ )
693 if ( ms_languagesDB
->Item(i
).CanonicalName
== langFullWithModifier
)
698 // b) Without modifier
699 if ( modifier
.empty() || i
== count
)
701 for ( i
= 0; i
< count
; i
++ )
703 if ( ms_languagesDB
->Item(i
).CanonicalName
== langFull
)
708 // 2. If langFull is of the form xx_YY, try to find xx:
709 if ( i
== count
&& !justLang
)
711 for ( i
= 0; i
< count
; i
++ )
713 if ( ms_languagesDB
->Item(i
).CanonicalName
== lang
)
720 // 3. If langFull is of the form xx, try to find any xx_YY record:
721 if ( i
== count
&& justLang
)
723 for ( i
= 0; i
< count
; i
++ )
725 if ( ExtractLang(ms_languagesDB
->Item(i
).CanonicalName
)
736 // In addition to the format above, we also can have full language
737 // names in LANG env var - for example, SuSE is known to use
738 // LANG="german" - so check for use of non-standard format and try to
739 // find the name in verbose description.
740 for ( i
= 0; i
< count
; i
++ )
742 if (ms_languagesDB
->Item(i
).Description
.CmpNoCase(langFull
) == 0)
748 #elif defined(__WIN32__)
749 LCID lcid
= GetUserDefaultLCID();
752 wxUint32 lang
= PRIMARYLANGID(LANGIDFROMLCID(lcid
));
753 wxUint32 sublang
= SUBLANGID(LANGIDFROMLCID(lcid
));
755 for ( i
= 0; i
< count
; i
++ )
757 if (ms_languagesDB
->Item(i
).WinLang
== lang
&&
758 ms_languagesDB
->Item(i
).WinSublang
== sublang
)
764 //else: leave wxlang == wxLANGUAGE_UNKNOWN
769 // we did find a matching entry, use it
770 return ms_languagesDB
->Item(i
).Language
;
773 // no info about this language in the database
774 return wxLANGUAGE_UNKNOWN
;
777 // ----------------------------------------------------------------------------
779 // ----------------------------------------------------------------------------
781 // this is a bit strange as under Windows we get the encoding name using its
782 // numeric value and under Unix we do it the other way round, but this just
783 // reflects the way different systems provide the encoding info
786 wxString
wxLocale::GetSystemEncodingName()
790 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
791 // FIXME: what is the error return value for GetACP()?
792 UINT codepage
= ::GetACP();
793 encname
.Printf(wxS("windows-%u"), codepage
);
794 #elif defined(__WXMAC__)
795 encname
= wxCFStringRef::AsString(
796 CFStringGetNameOfEncoding(CFStringGetSystemEncoding())
798 #elif defined(__UNIX_LIKE__)
800 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
801 // GNU libc provides current character set this way (this conforms
803 char *oldLocale
= strdup(setlocale(LC_CTYPE
, NULL
));
804 setlocale(LC_CTYPE
, "");
805 const char *alang
= nl_langinfo(CODESET
);
806 setlocale(LC_CTYPE
, oldLocale
);
811 encname
= wxString::FromAscii( alang
);
813 else // nl_langinfo() failed
814 #endif // HAVE_LANGINFO_H
816 // if we can't get at the character set directly, try to see if it's in
817 // the environment variables (in most cases this won't work, but I was
819 char *lang
= getenv( "LC_ALL");
820 char *dot
= lang
? strchr(lang
, '.') : NULL
;
823 lang
= getenv( "LC_CTYPE" );
825 dot
= strchr(lang
, '.' );
829 lang
= getenv( "LANG");
831 dot
= strchr(lang
, '.');
836 encname
= wxString::FromAscii( dot
+1 );
845 wxFontEncoding
wxLocale::GetSystemEncoding()
847 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
848 UINT codepage
= ::GetACP();
850 // wxWidgets only knows about CP1250-1257, 874, 932, 936, 949, 950
851 if ( codepage
>= 1250 && codepage
<= 1257 )
853 return (wxFontEncoding
)(wxFONTENCODING_CP1250
+ codepage
- 1250);
856 if ( codepage
== 874 )
858 return wxFONTENCODING_CP874
;
861 if ( codepage
== 932 )
863 return wxFONTENCODING_CP932
;
866 if ( codepage
== 936 )
868 return wxFONTENCODING_CP936
;
871 if ( codepage
== 949 )
873 return wxFONTENCODING_CP949
;
876 if ( codepage
== 950 )
878 return wxFONTENCODING_CP950
;
880 #elif defined(__WXMAC__)
881 CFStringEncoding encoding
= 0 ;
882 encoding
= CFStringGetSystemEncoding() ;
883 return wxMacGetFontEncFromSystemEnc( encoding
) ;
884 #elif defined(__UNIX_LIKE__) && wxUSE_FONTMAP
885 const wxString encname
= GetSystemEncodingName();
886 if ( !encname
.empty() )
888 wxFontEncoding enc
= wxFontMapperBase::GetEncodingFromName(encname
);
890 // on some modern Linux systems (RedHat 8) the default system locale
891 // is UTF8 -- but it isn't supported by wxGTK1 in ANSI build at all so
892 // don't even try to use it in this case
893 #if !wxUSE_UNICODE && \
894 ((defined(__WXGTK__) && !defined(__WXGTK20__)) || defined(__WXMOTIF__))
895 if ( enc
== wxFONTENCODING_UTF8
)
897 // the most similar supported encoding...
898 enc
= wxFONTENCODING_ISO8859_1
;
900 #endif // !wxUSE_UNICODE
902 // GetEncodingFromName() returns wxFONTENCODING_DEFAULT for C locale
903 // (a.k.a. US-ASCII) which is arguably a bug but keep it like this for
904 // backwards compatibility and just take care to not return
905 // wxFONTENCODING_DEFAULT from here as this surely doesn't make sense
906 if ( enc
== wxFONTENCODING_DEFAULT
)
908 // we don't have wxFONTENCODING_ASCII, so use the closest one
909 return wxFONTENCODING_ISO8859_1
;
912 if ( enc
!= wxFONTENCODING_MAX
)
916 //else: return wxFONTENCODING_SYSTEM below
920 return wxFONTENCODING_SYSTEM
;
924 void wxLocale::AddLanguage(const wxLanguageInfo
& info
)
927 ms_languagesDB
->Add(info
);
931 const wxLanguageInfo
*wxLocale::GetLanguageInfo(int lang
)
935 // calling GetLanguageInfo(wxLANGUAGE_DEFAULT) is a natural thing to do, so
937 if ( lang
== wxLANGUAGE_DEFAULT
)
938 lang
= GetSystemLanguage();
940 const size_t count
= ms_languagesDB
->GetCount();
941 for ( size_t i
= 0; i
< count
; i
++ )
943 if ( ms_languagesDB
->Item(i
).Language
== lang
)
945 // We need to create a temporary here in order to make this work with BCC in final build mode
946 wxLanguageInfo
*ptr
= &ms_languagesDB
->Item(i
);
955 wxString
wxLocale::GetLanguageName(int lang
)
957 if ( lang
== wxLANGUAGE_DEFAULT
|| lang
== wxLANGUAGE_UNKNOWN
)
958 return wxEmptyString
;
960 const wxLanguageInfo
*info
= GetLanguageInfo(lang
);
962 return wxEmptyString
;
964 return info
->Description
;
968 wxString
wxLocale::GetLanguageCanonicalName(int lang
)
970 if ( lang
== wxLANGUAGE_DEFAULT
|| lang
== wxLANGUAGE_UNKNOWN
)
971 return wxEmptyString
;
973 const wxLanguageInfo
*info
= GetLanguageInfo(lang
);
975 return wxEmptyString
;
977 return info
->CanonicalName
;
981 const wxLanguageInfo
*wxLocale::FindLanguageInfo(const wxString
& locale
)
985 const wxLanguageInfo
*infoRet
= NULL
;
987 const size_t count
= ms_languagesDB
->GetCount();
988 for ( size_t i
= 0; i
< count
; i
++ )
990 const wxLanguageInfo
*info
= &ms_languagesDB
->Item(i
);
992 if ( wxStricmp(locale
, info
->CanonicalName
) == 0 ||
993 wxStricmp(locale
, info
->Description
) == 0 )
995 // exact match, stop searching
1000 if ( wxStricmp(locale
, info
->CanonicalName
.BeforeFirst(wxS('_'))) == 0 )
1002 // a match -- but maybe we'll find an exact one later, so continue
1005 // OTOH, maybe we had already found a language match and in this
1006 // case don't overwrite it because the entry for the default
1007 // country always appears first in ms_languagesDB
1016 wxString
wxLocale::GetSysName() const
1018 return wxSetlocale(LC_ALL
, NULL
);
1022 wxLocale::~wxLocale()
1024 // Restore old translations object.
1025 // See DoCommonInit() for explanation of why this is needed for backward
1027 if ( wxTranslations::Get() == &m_translations
)
1030 wxTranslations::SetNonOwned(&m_pOldLocale
->m_translations
);
1032 wxTranslations::Set(NULL
);
1035 // restore old locale pointer
1036 wxSetLocale(m_pOldLocale
);
1038 wxSetlocale(LC_ALL
, m_pszOldLocale
);
1039 free(const_cast<char *>(m_pszOldLocale
));
1043 // check if the given locale is provided by OS and C run time
1045 bool wxLocale::IsAvailable(int lang
)
1047 const wxLanguageInfo
*info
= wxLocale::GetLanguageInfo(lang
);
1050 // The language is unknown (this normally only happens when we're
1051 // passed wxLANGUAGE_DEFAULT), so we can't support it.
1052 wxASSERT_MSG( lang
== wxLANGUAGE_DEFAULT
,
1053 wxS("No info for a valid language?") );
1057 #if defined(__WIN32__)
1058 if ( !info
->WinLang
)
1061 if ( !::IsValidLocale(info
->GetLCID(), LCID_INSTALLED
) )
1064 #elif defined(__UNIX__)
1066 // Test if setting the locale works, then set it back.
1067 char * const oldLocale
= wxStrdupA(setlocale(LC_ALL
, NULL
));
1069 // Some platforms don't like xx_YY form and require xx only so test for
1072 available
= wxSetlocaleTryUTF8(LC_ALL
, info
->CanonicalName
) ||
1073 wxSetlocaleTryUTF8(LC_ALL
, ExtractLang(info
->CanonicalName
));
1075 // restore the original locale
1076 wxSetlocale(LC_ALL
, oldLocale
);
1088 bool wxLocale::AddCatalog(const wxString
& domain
)
1090 wxTranslations
*t
= wxTranslations::Get();
1093 return t
->AddCatalog(domain
);
1096 bool wxLocale::AddCatalog(const wxString
& domain
, wxLanguage msgIdLanguage
)
1098 wxTranslations
*t
= wxTranslations::Get();
1101 return t
->AddCatalog(domain
, msgIdLanguage
);
1104 // add a catalog to our linked list
1105 bool wxLocale::AddCatalog(const wxString
& szDomain
,
1106 wxLanguage msgIdLanguage
,
1107 const wxString
& msgIdCharset
)
1109 wxTranslations
*t
= wxTranslations::Get();
1113 wxUnusedVar(msgIdCharset
);
1114 return t
->AddCatalog(szDomain
, msgIdLanguage
);
1116 return t
->AddCatalog(szDomain
, msgIdLanguage
, msgIdCharset
);
1120 bool wxLocale::IsLoaded(const wxString
& domain
) const
1122 wxTranslations
*t
= wxTranslations::Get();
1125 return t
->IsLoaded(domain
);
1128 wxString
wxLocale::GetHeaderValue(const wxString
& header
,
1129 const wxString
& domain
) const
1131 wxTranslations
*t
= wxTranslations::Get();
1133 return wxEmptyString
;
1134 return t
->GetHeaderValue(header
, domain
);
1137 // ----------------------------------------------------------------------------
1138 // accessors for locale-dependent data
1139 // ----------------------------------------------------------------------------
1141 #if defined(__WINDOWS__) || defined(__WXOSX__)
1146 // This function translates from Unicode date formats described at
1148 // http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
1150 // to strftime()-like syntax. This translation is not lossless but we try to do
1153 static wxString
TranslateFromUnicodeFormat(const wxString
& fmt
)
1156 fmtWX
.reserve(fmt
.length());
1159 size_t lastCount
= 0;
1161 const char* formatchars
=
1169 for ( wxString::const_iterator p
= fmt
.begin(); /* end handled inside */; ++p
)
1171 if ( p
!= fmt
.end() )
1179 const wxUniChar ch
= (*p
).GetValue();
1180 if ( ch
.IsAscii() && strchr(formatchars
, ch
) )
1182 // these characters come in groups, start counting them
1189 // interpret any special characters we collected so far
1195 switch ( lastCount
)
1199 // these two are the same as we don't distinguish
1200 // between 1 and 2 digits for days
1213 wxFAIL_MSG( "too many 'd's" );
1218 switch ( lastCount
)
1227 wxFAIL_MSG( "wrong number of 'D's" );
1231 switch ( lastCount
)
1239 wxFAIL_MSG( "wrong number of 'w's" );
1243 switch ( lastCount
)
1258 wxFAIL_MSG( "wrong number of 'E's" );
1263 switch ( lastCount
)
1267 // as for 'd' and 'dd' above
1280 wxFAIL_MSG( "too many 'M's" );
1285 switch ( lastCount
)
1297 wxFAIL_MSG( "wrong number of 'y's" );
1302 switch ( lastCount
)
1310 wxFAIL_MSG( "wrong number of 'H's" );
1315 switch ( lastCount
)
1323 wxFAIL_MSG( "wrong number of 'h's" );
1328 switch ( lastCount
)
1336 wxFAIL_MSG( "wrong number of 'm's" );
1341 switch ( lastCount
)
1349 wxFAIL_MSG( "wrong number of 's's" );
1354 // strftime() doesn't have era string,
1355 // ignore this format
1356 wxASSERT_MSG( lastCount
<= 2, "too many 'g's" );
1366 switch ( lastCount
)
1374 wxFAIL_MSG( "too many 't's" );
1379 wxFAIL_MSG( "unreachable" );
1386 if ( p
== fmt
.end() )
1389 // not a special character so must be just a separator, treat as is
1390 if ( *p
== wxT('%') )
1392 // this one needs to be escaped
1402 } // anonymous namespace
1404 #endif // __WINDOWS__ || __WXOSX__
1406 #if defined(__WINDOWS__)
1411 LCTYPE
GetLCTYPEFormatFromLocalInfo(wxLocaleInfo index
)
1415 case wxLOCALE_SHORT_DATE_FMT
:
1416 return LOCALE_SSHORTDATE
;
1418 case wxLOCALE_LONG_DATE_FMT
:
1419 return LOCALE_SLONGDATE
;
1421 case wxLOCALE_TIME_FMT
:
1422 return LOCALE_STIMEFORMAT
;
1425 wxFAIL_MSG( "no matching LCTYPE" );
1431 } // anonymous namespace
1434 wxString
wxLocale::GetInfo(wxLocaleInfo index
, wxLocaleCategory cat
)
1436 const wxLanguageInfo
* const
1437 info
= wxGetLocale() ? GetLanguageInfo(wxGetLocale()->GetLanguage())
1441 // wxSetLocale() hadn't been called yet of failed, hence CRT must be
1442 // using "C" locale -- but check it to detect bugs that would happen if
1443 // this were not the case.
1444 wxASSERT_MSG( strcmp(setlocale(LC_ALL
, NULL
), "C") == 0,
1445 wxS("You probably called setlocale() directly instead ")
1446 wxS("of using wxLocale and now there is a ")
1447 wxS("mismatch between C/C++ and Windows locale.\n")
1448 wxS("Things are going to break, please only change ")
1449 wxS("locale by creating wxLocale objects to avoid this!") );
1452 // Return the hard coded values for C locale. This is really the right
1453 // thing to do as there is no LCID we can use in the code below in this
1454 // case, even LOCALE_INVARIANT is not quite the same as C locale (the
1455 // only difference is that it uses %Y instead of %y in the date format
1456 // but this difference is significant enough).
1459 case wxLOCALE_THOUSANDS_SEP
:
1462 case wxLOCALE_DECIMAL_POINT
:
1465 case wxLOCALE_SHORT_DATE_FMT
:
1468 case wxLOCALE_LONG_DATE_FMT
:
1469 return "%A, %B %d, %Y";
1471 case wxLOCALE_TIME_FMT
:
1474 case wxLOCALE_DATE_TIME_FMT
:
1475 return "%m/%d/%y %H:%M:%S";
1478 wxFAIL_MSG( "unknown wxLocaleInfo" );
1482 const wxUint32 lcid
= info
->GetLCID();
1491 case wxLOCALE_THOUSANDS_SEP
:
1492 if ( ::GetLocaleInfo(lcid
, LOCALE_STHOUSAND
, buf
, WXSIZEOF(buf
)) )
1496 case wxLOCALE_DECIMAL_POINT
:
1497 if ( ::GetLocaleInfo(lcid
,
1498 cat
== wxLOCALE_CAT_MONEY
1499 ? LOCALE_SMONDECIMALSEP
1506 // As we get our decimal point separator from Win32 and not the
1507 // CRT there is a possibility of mismatch between them and this
1508 // can easily happen if the user code called setlocale()
1509 // instead of using wxLocale to change the locale. And this can
1510 // result in very strange bugs elsewhere in the code as the
1511 // assumptions that formatted strings do use the decimal
1512 // separator actually fail, so check for it here.
1515 wxString::Format("%.3f", 1.23).find(str
) != wxString::npos
,
1516 "Decimal separator mismatch -- did you use setlocale()?"
1517 "If so, use wxLocale to change the locale instead."
1522 case wxLOCALE_SHORT_DATE_FMT
:
1523 case wxLOCALE_LONG_DATE_FMT
:
1524 case wxLOCALE_TIME_FMT
:
1525 if ( ::GetLocaleInfo(lcid
, GetLCTYPEFormatFromLocalInfo(index
),
1526 buf
, WXSIZEOF(buf
)) )
1528 return TranslateFromUnicodeFormat(buf
);
1532 case wxLOCALE_DATE_TIME_FMT
:
1533 // there doesn't seem to be any specific setting for this, so just
1534 // combine date and time ones
1536 // we use the short date because this is what "%c" uses by default
1537 // ("%#c" uses long date but we have no way to specify the
1538 // alternate representation here)
1540 const wxString datefmt
= GetInfo(wxLOCALE_SHORT_DATE_FMT
);
1541 if ( datefmt
.empty() )
1544 const wxString timefmt
= GetInfo(wxLOCALE_TIME_FMT
);
1545 if ( timefmt
.empty() )
1548 str
<< datefmt
<< ' ' << timefmt
;
1553 wxFAIL_MSG( "unknown wxLocaleInfo" );
1559 #elif defined(__WXOSX__)
1562 wxString
wxLocale::GetInfo(wxLocaleInfo index
, wxLocaleCategory
WXUNUSED(cat
))
1564 CFLocaleRef userLocaleRefRaw
;
1565 if ( wxGetLocale() )
1567 userLocaleRefRaw
= CFLocaleCreate
1569 kCFAllocatorDefault
,
1570 wxCFStringRef(wxGetLocale()->GetCanonicalName())
1573 else // no current locale, use the default one
1575 userLocaleRefRaw
= CFLocaleCopyCurrent();
1578 wxCFRef
<CFLocaleRef
> userLocaleRef(userLocaleRefRaw
);
1580 CFStringRef cfstr
= 0;
1583 case wxLOCALE_THOUSANDS_SEP
:
1584 cfstr
= (CFStringRef
) CFLocaleGetValue(userLocaleRef
, kCFLocaleGroupingSeparator
);
1587 case wxLOCALE_DECIMAL_POINT
:
1588 cfstr
= (CFStringRef
) CFLocaleGetValue(userLocaleRef
, kCFLocaleDecimalSeparator
);
1591 case wxLOCALE_SHORT_DATE_FMT
:
1592 case wxLOCALE_LONG_DATE_FMT
:
1593 case wxLOCALE_DATE_TIME_FMT
:
1594 case wxLOCALE_TIME_FMT
:
1596 CFDateFormatterStyle dateStyle
= kCFDateFormatterNoStyle
;
1597 CFDateFormatterStyle timeStyle
= kCFDateFormatterNoStyle
;
1600 case wxLOCALE_SHORT_DATE_FMT
:
1601 dateStyle
= kCFDateFormatterShortStyle
;
1603 case wxLOCALE_LONG_DATE_FMT
:
1604 dateStyle
= kCFDateFormatterFullStyle
;
1606 case wxLOCALE_DATE_TIME_FMT
:
1607 dateStyle
= kCFDateFormatterFullStyle
;
1608 timeStyle
= kCFDateFormatterMediumStyle
;
1610 case wxLOCALE_TIME_FMT
:
1611 timeStyle
= kCFDateFormatterMediumStyle
;
1614 wxFAIL_MSG( "unexpected time locale" );
1617 wxCFRef
<CFDateFormatterRef
> dateFormatter( CFDateFormatterCreate
1618 (NULL
, userLocaleRef
, dateStyle
, timeStyle
));
1619 wxCFStringRef cfs
= wxCFRetain( CFDateFormatterGetFormat(dateFormatter
));
1620 wxString format
= TranslateFromUnicodeFormat(cfs
.AsString());
1621 // we always want full years
1622 format
.Replace("%y","%Y");
1628 wxFAIL_MSG( "Unknown locale info" );
1632 wxCFStringRef
str(wxCFRetain(cfstr
));
1633 return str
.AsString();
1636 #else // !__WINDOWS__ && !__WXOSX__, assume generic POSIX
1641 wxString
GetDateFormatFromLangInfo(wxLocaleInfo index
)
1643 #ifdef HAVE_LANGINFO_H
1644 // array containing parameters for nl_langinfo() indexes by offset of index
1645 // from wxLOCALE_SHORT_DATE_FMT
1646 static const nl_item items
[] =
1648 D_FMT
, D_T_FMT
, D_T_FMT
, T_FMT
,
1651 const int nlidx
= index
- wxLOCALE_SHORT_DATE_FMT
;
1652 if ( nlidx
< 0 || nlidx
>= (int)WXSIZEOF(items
) )
1654 wxFAIL_MSG( "logic error in GetInfo() code" );
1658 const wxString
fmt(nl_langinfo(items
[nlidx
]));
1660 // just return the format returned by nl_langinfo() except for long date
1661 // format which we need to recover from date/time format ourselves (but not
1662 // if we failed completely)
1663 if ( fmt
.empty() || index
!= wxLOCALE_LONG_DATE_FMT
)
1666 // this is not 100% precise but the idea is that a typical date/time format
1667 // under POSIX systems is a combination of a long date format with time one
1668 // so we should be able to get just the long date format by removing all
1669 // time-specific format specifiers
1670 static const char *timeFmtSpecs
= "HIklMpPrRsSTXzZ";
1671 static const char *timeSep
= " :./-";
1673 wxString fmtDateOnly
;
1674 const wxString::const_iterator end
= fmt
.end();
1675 wxString::const_iterator lastSep
= end
;
1676 for ( wxString::const_iterator p
= fmt
.begin(); p
!= end
; ++p
)
1678 if ( strchr(timeSep
, *p
) )
1680 if ( lastSep
== end
)
1683 // skip it for now, we'll discard it if it's followed by a time
1684 // specifier later or add it to fmtDateOnly if it is not
1689 (p
+ 1 != end
) && strchr(timeFmtSpecs
, p
[1]) )
1691 // time specified found: skip it and any preceding separators
1697 if ( lastSep
!= end
)
1699 fmtDateOnly
+= wxString(lastSep
, p
);
1707 #else // !HAVE_LANGINFO_H
1710 // no fallback, let the application deal with unavailability of
1711 // nl_langinfo() itself as there is no good way for us to do it (well, we
1712 // could try to reverse engineer the format from strftime() output but this
1713 // looks like too much trouble considering the relatively small number of
1714 // systems without nl_langinfo() still in use)
1716 #endif // HAVE_LANGINFO_H/!HAVE_LANGINFO_H
1719 } // anonymous namespace
1722 wxString
wxLocale::GetInfo(wxLocaleInfo index
, wxLocaleCategory cat
)
1724 lconv
* const lc
= localeconv();
1730 case wxLOCALE_THOUSANDS_SEP
:
1731 if ( cat
== wxLOCALE_CAT_NUMBER
)
1732 return lc
->thousands_sep
;
1733 else if ( cat
== wxLOCALE_CAT_MONEY
)
1734 return lc
->mon_thousands_sep
;
1736 wxFAIL_MSG( "invalid wxLocaleCategory" );
1740 case wxLOCALE_DECIMAL_POINT
:
1741 if ( cat
== wxLOCALE_CAT_NUMBER
)
1742 return lc
->decimal_point
;
1743 else if ( cat
== wxLOCALE_CAT_MONEY
)
1744 return lc
->mon_decimal_point
;
1746 wxFAIL_MSG( "invalid wxLocaleCategory" );
1749 case wxLOCALE_SHORT_DATE_FMT
:
1750 case wxLOCALE_LONG_DATE_FMT
:
1751 case wxLOCALE_DATE_TIME_FMT
:
1752 case wxLOCALE_TIME_FMT
:
1753 if ( cat
!= wxLOCALE_CAT_DATE
&& cat
!= wxLOCALE_CAT_DEFAULT
)
1755 wxFAIL_MSG( "invalid wxLocaleCategory" );
1759 return GetDateFormatFromLangInfo(index
);
1763 wxFAIL_MSG( "unknown wxLocaleInfo value" );
1771 // ----------------------------------------------------------------------------
1772 // global functions and variables
1773 // ----------------------------------------------------------------------------
1775 // retrieve/change current locale
1776 // ------------------------------
1778 // the current locale object
1779 static wxLocale
*g_pLocale
= NULL
;
1781 wxLocale
*wxGetLocale()
1786 wxLocale
*wxSetLocale(wxLocale
*pLocale
)
1788 wxLocale
*pOld
= g_pLocale
;
1789 g_pLocale
= pLocale
;
1795 // ----------------------------------------------------------------------------
1796 // wxLocale module (for lazy destruction of languagesDB)
1797 // ----------------------------------------------------------------------------
1799 class wxLocaleModule
: public wxModule
1801 DECLARE_DYNAMIC_CLASS(wxLocaleModule
)
1812 wxLocale::DestroyLanguagesDB();
1816 IMPLEMENT_DYNAMIC_CLASS(wxLocaleModule
, wxModule
)
1818 #endif // wxUSE_INTL