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)
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
29 // The following define is needed by Innotek's libc to
30 // make the definition of struct localeconv available.
31 #define __INTERNAL_DEFS
37 #include "wx/dynarray.h"
38 #include "wx/string.h"
43 #include "wx/hashmap.h"
44 #include "wx/module.h"
54 #ifdef HAVE_LANGINFO_H
59 #include "wx/msw/private.h"
63 #include "wx/filename.h"
64 #include "wx/tokenzr.h"
65 #include "wx/fontmap.h"
66 #include "wx/scopedptr.h"
67 #include "wx/apptrait.h"
68 #include "wx/stdpaths.h"
69 #include "wx/hashset.h"
71 #if defined(__WXOSX__)
72 #include "wx/osx/core/cfref.h"
73 #include <CoreFoundation/CFLocale.h>
74 #include <CoreFoundation/CFDateFormatter.h>
75 #include "wx/osx/core/cfstring.h"
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 // the constants describing the format of ll_CC locale string
83 static const size_t LEN_LANG
= 2;
84 static const size_t LEN_SUBLANG
= 2;
85 static const size_t LEN_FULL
= LEN_LANG
+ 1 + LEN_SUBLANG
; // 1 for '_'
87 #define TRACE_I18N wxS("i18n")
89 // ============================================================================
91 // ============================================================================
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 static wxLocale
*wxSetLocale(wxLocale
*pLocale
);
102 // get just the language part
103 inline wxString
ExtractLang(const wxString
& langFull
)
105 return langFull
.Left(LEN_LANG
);
108 // helper functions of GetSystemLanguage()
111 // get everything else (including the leading '_')
112 inline wxString
ExtractNotLang(const wxString
& langFull
)
114 return langFull
.Mid(LEN_LANG
);
119 } // anonymous namespace
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
127 // helper used by wxLanguageInfo::GetLocaleName() and elsewhere to determine
128 // whether the locale is Unicode-only (it is if this function returns empty
130 static wxString
wxGetANSICodePageForLocale(LCID lcid
)
135 if ( ::GetLocaleInfo(lcid
, LOCALE_IDEFAULTANSICODEPAGE
,
136 buffer
, WXSIZEOF(buffer
)) > 0 )
138 if ( buffer
[0] != wxT('0') || buffer
[1] != wxT('\0') )
140 //else: this locale doesn't use ANSI code page
146 wxUint32
wxLanguageInfo::GetLCID() const
148 return MAKELCID(MAKELANGID(WinLang
, WinSublang
), SORT_DEFAULT
);
151 wxString
wxLanguageInfo::GetLocaleName() const
155 const LCID lcid
= GetLCID();
158 buffer
[0] = wxT('\0');
159 if ( !::GetLocaleInfo(lcid
, LOCALE_SENGLANGUAGE
, buffer
, WXSIZEOF(buffer
)) )
161 wxLogLastError(wxT("GetLocaleInfo(LOCALE_SENGLANGUAGE)"));
166 if ( ::GetLocaleInfo(lcid
, LOCALE_SENGCOUNTRY
,
167 buffer
, WXSIZEOF(buffer
)) > 0 )
169 locale
<< wxT('_') << buffer
;
172 const wxString cp
= wxGetANSICodePageForLocale(lcid
);
175 locale
<< wxT('.') << cp
;
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 #include "wx/arrimpl.cpp"
188 WX_DECLARE_EXPORTED_OBJARRAY(wxLanguageInfo
, wxLanguageInfoArray
);
189 WX_DEFINE_OBJARRAY(wxLanguageInfoArray
)
191 wxLanguageInfoArray
*wxLocale::ms_languagesDB
= NULL
;
193 /*static*/ void wxLocale::CreateLanguagesDB()
195 if (ms_languagesDB
== NULL
)
197 ms_languagesDB
= new wxLanguageInfoArray
;
202 /*static*/ void wxLocale::DestroyLanguagesDB()
204 delete ms_languagesDB
;
205 ms_languagesDB
= NULL
;
209 void wxLocale::DoCommonInit()
211 m_pszOldLocale
= NULL
;
213 m_pOldLocale
= wxSetLocale(this);
215 // Set translations object, but only if the user didn't do so yet.
216 // This is to preserve compatibility with wx-2.8 where wxLocale was
217 // the only API for translations. wxLocale works as a stack, with
218 // latest-created one being the active one:
219 // wxLocale loc_fr(wxLANGUAGE_FRENCH);
220 // // _() returns French
222 // wxLocale loc_cs(wxLANGUAGE_CZECH);
223 // // _() returns Czech
225 // // _() returns French again
226 wxTranslations
*oldTrans
= wxTranslations::Get();
228 (m_pOldLocale
&& oldTrans
== &m_pOldLocale
->m_translations
) )
230 wxTranslations::SetNonOwned(&m_translations
);
233 m_language
= wxLANGUAGE_UNKNOWN
;
234 m_initialized
= false;
237 // NB: this function has (desired) side effect of changing current locale
238 bool wxLocale::Init(const wxString
& name
,
239 const wxString
& shortName
,
240 const wxString
& locale
,
242 #if WXWIN_COMPATIBILITY_2_8
243 ,bool bConvertEncoding
247 #if WXWIN_COMPATIBILITY_2_8
248 wxASSERT_MSG( bConvertEncoding
,
249 wxS("wxLocale::Init with bConvertEncoding=false is no longer supported, add charset to your catalogs") );
252 bool ret
= DoInit(name
, shortName
, locale
);
254 // NB: don't use 'lang' here, 'language' may be wxLANGUAGE_DEFAULT
255 wxTranslations
*t
= wxTranslations::Get();
258 t
->SetLanguage(shortName
);
267 bool wxLocale::DoInit(const wxString
& name
,
268 const wxString
& shortName
,
269 const wxString
& locale
)
271 wxASSERT_MSG( !m_initialized
,
272 wxS("you can't call wxLocale::Init more than once") );
274 m_initialized
= true;
276 m_strShort
= shortName
;
277 m_language
= wxLANGUAGE_UNKNOWN
;
279 // change current locale (default: same as long name)
280 wxString
szLocale(locale
);
281 if ( szLocale
.empty() )
283 // the argument to setlocale()
284 szLocale
= shortName
;
286 wxCHECK_MSG( !szLocale
.empty(), false,
287 wxS("no locale to set in wxLocale::Init()") );
290 const char *oldLocale
= wxSetlocale(LC_ALL
, szLocale
);
292 m_pszOldLocale
= wxStrdup(oldLocale
);
294 m_pszOldLocale
= NULL
;
296 if ( m_pszOldLocale
== NULL
)
298 wxLogError(_("locale '%s' can not be set."), szLocale
);
301 // the short name will be used to look for catalog files as well,
302 // so we need something here
303 if ( m_strShort
.empty() ) {
304 // FIXME I don't know how these 2 letter abbreviations are formed,
305 // this wild guess is surely wrong
306 if ( !szLocale
.empty() )
308 m_strShort
+= (wxChar
)wxTolower(szLocale
[0]);
309 if ( szLocale
.length() > 1 )
310 m_strShort
+= (wxChar
)wxTolower(szLocale
[1]);
318 #if defined(__UNIX__) && wxUSE_UNICODE && !defined(__WXMAC__)
319 static const char *wxSetlocaleTryUTF8(int c
, const wxString
& lc
)
321 const char *l
= NULL
;
323 // NB: We prefer to set UTF-8 locale if it's possible and only fall back to
324 // non-UTF-8 locale if it fails
330 buf2
= buf
+ wxS(".UTF-8");
331 l
= wxSetlocale(c
, buf2
);
334 buf2
= buf
+ wxS(".utf-8");
335 l
= wxSetlocale(c
, buf2
);
339 buf2
= buf
+ wxS(".UTF8");
340 l
= wxSetlocale(c
, buf2
);
344 buf2
= buf
+ wxS(".utf8");
345 l
= wxSetlocale(c
, buf2
);
349 // if we can't set UTF-8 locale, try non-UTF-8 one:
351 l
= wxSetlocale(c
, lc
);
356 #define wxSetlocaleTryUTF8(c, lc) wxSetlocale(c, lc)
359 bool wxLocale::Init(int language
, int flags
)
361 #if WXWIN_COMPATIBILITY_2_8
362 wxASSERT_MSG( !(flags
& wxLOCALE_CONV_ENCODING
),
363 wxS("wxLOCALE_CONV_ENCODING is no longer supported, add charset to your catalogs") );
369 if (lang
== wxLANGUAGE_DEFAULT
)
371 // auto detect the language
372 lang
= GetSystemLanguage();
375 // We failed to detect system language, so we will use English:
376 if (lang
== wxLANGUAGE_UNKNOWN
)
381 const wxLanguageInfo
*info
= GetLanguageInfo(lang
);
386 wxLogError(wxS("Unknown language %i."), lang
);
390 wxString name
= info
->Description
;
391 wxString canonical
= info
->CanonicalName
;
396 const char *retloc
= wxSetlocale(LC_ALL
, wxEmptyString
);
397 #elif defined(__UNIX__) && !defined(__WXMAC__)
398 if (language
!= wxLANGUAGE_DEFAULT
)
399 locale
= info
->CanonicalName
;
401 const char *retloc
= wxSetlocaleTryUTF8(LC_ALL
, locale
);
403 const wxString langOnly
= ExtractLang(locale
);
406 // Some C libraries don't like xx_YY form and require xx only
407 retloc
= wxSetlocaleTryUTF8(LC_ALL
, langOnly
);
411 // some systems (e.g. FreeBSD and HP-UX) don't have xx_YY aliases but
412 // require the full xx_YY.encoding form, so try using UTF-8 because this is
413 // the only thing we can do generically
415 // TODO: add encodings applicable to each language to the lang DB and try
416 // them all in turn here
419 const wxChar
**names
=
420 wxFontMapperBase::GetAllEncodingNames(wxFONTENCODING_UTF8
);
423 retloc
= wxSetlocale(LC_ALL
, locale
+ wxS('.') + *names
++);
428 #endif // wxUSE_FONTMAP
432 // Some C libraries (namely glibc) still use old ISO 639,
433 // so will translate the abbrev for them
435 if ( langOnly
== wxS("he") )
436 localeAlt
= wxS("iw") + ExtractNotLang(locale
);
437 else if ( langOnly
== wxS("id") )
438 localeAlt
= wxS("in") + ExtractNotLang(locale
);
439 else if ( langOnly
== wxS("yi") )
440 localeAlt
= wxS("ji") + ExtractNotLang(locale
);
441 else if ( langOnly
== wxS("nb") )
442 localeAlt
= wxS("no_NO");
443 else if ( langOnly
== wxS("nn") )
444 localeAlt
= wxS("no_NY");
446 if ( !localeAlt
.empty() )
448 retloc
= wxSetlocaleTryUTF8(LC_ALL
, localeAlt
);
450 retloc
= wxSetlocaleTryUTF8(LC_ALL
, ExtractLang(localeAlt
));
458 // at least in AIX 5.2 libc is buggy and the string returned from
459 // setlocale(LC_ALL) can't be passed back to it because it returns 6
460 // strings (one for each locale category), i.e. for C locale we get back
463 // this contradicts IBM own docs but this is not of much help, so just work
464 // around it in the crudest possible manner
465 char* p
= const_cast<char*>(wxStrchr(retloc
, ' '));
470 #elif defined(__WIN32__)
471 const char *retloc
= "C";
472 if ( language
!= wxLANGUAGE_DEFAULT
)
474 if ( info
->WinLang
== 0 )
476 wxLogWarning(wxS("Locale '%s' not supported by OS."), name
.c_str());
477 // retloc already set to "C"
479 else // language supported by Windows
481 // Windows CE doesn't have SetThreadLocale() and there doesn't seem
482 // to be any equivalent
484 const wxUint32 lcid
= info
->GetLCID();
486 // change locale used by Windows functions
487 ::SetThreadLocale(lcid
);
490 // and also call setlocale() to change locale used by the CRT
491 locale
= info
->GetLocaleName();
492 if ( locale
.empty() )
496 else // have a valid locale
498 retloc
= wxSetlocale(LC_ALL
, locale
);
502 else // language == wxLANGUAGE_DEFAULT
504 retloc
= wxSetlocale(LC_ALL
, wxEmptyString
);
507 #if wxUSE_UNICODE && (defined(__VISUALC__) || defined(__MINGW32__))
508 // VC++ setlocale() (also used by Mingw) can't set locale to languages that
509 // can only be written using Unicode, therefore wxSetlocale() call fails
510 // for such languages but we don't want to report it as an error -- so that
511 // at least message catalogs can be used.
514 if ( wxGetANSICodePageForLocale(LOCALE_USER_DEFAULT
).empty() )
516 // we set the locale to a Unicode-only language, don't treat the
517 // inability of CRT to use it as an error
521 #endif // CRT not handling Unicode-only languages
525 #elif defined(__WXMAC__)
526 if (lang
== wxLANGUAGE_DEFAULT
)
527 locale
= wxEmptyString
;
529 locale
= info
->CanonicalName
;
531 const char *retloc
= wxSetlocale(LC_ALL
, locale
);
535 // Some C libraries don't like xx_YY form and require xx only
536 retloc
= wxSetlocale(LC_ALL
, ExtractLang(locale
));
541 #define WX_NO_LOCALE_SUPPORT
544 #ifndef WX_NO_LOCALE_SUPPORT
547 wxLogWarning(_("Cannot set locale to language \"%s\"."), name
.c_str());
549 // continue nevertheless and try to load at least the translations for
553 if ( !DoInit(name
, canonical
, retloc
) )
558 if (IsOk()) // setlocale() succeeded
561 // NB: don't use 'lang' here, 'language'
562 wxTranslations
*t
= wxTranslations::Get();
565 t
->SetLanguage(static_cast<wxLanguage
>(language
));
567 if ( flags
& wxLOCALE_LOAD_DEFAULT
)
572 #endif // !WX_NO_LOCALE_SUPPORT
575 /*static*/ int wxLocale::GetSystemLanguage()
579 // init i to avoid compiler warning
581 count
= ms_languagesDB
->GetCount();
583 #if defined(__UNIX__)
584 // first get the string identifying the language from the environment
587 wxCFRef
<CFLocaleRef
> userLocaleRef(CFLocaleCopyCurrent());
589 // because the locale identifier (kCFLocaleIdentifier) is formatted a little bit differently, eg
590 // az_Cyrl_AZ@calendar=buddhist;currency=JPY we just recreate the base info as expected by wx here
592 wxCFStringRef
str(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleLanguageCode
)));
593 langFull
= str
.AsString()+"_";
594 str
.reset(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleCountryCode
)));
595 langFull
+= str
.AsString();
597 if (!wxGetEnv(wxS("LC_ALL"), &langFull
) &&
598 !wxGetEnv(wxS("LC_MESSAGES"), &langFull
) &&
599 !wxGetEnv(wxS("LANG"), &langFull
))
601 // no language specified, treat it as English
602 return wxLANGUAGE_ENGLISH_US
;
605 if ( langFull
== wxS("C") || langFull
== wxS("POSIX") )
607 // default C locale is English too
608 return wxLANGUAGE_ENGLISH_US
;
612 // the language string has the following form
614 // lang[_LANG][.encoding][@modifier]
616 // (see environ(5) in the Open Unix specification)
618 // where lang is the primary language, LANG is a sublang/territory,
619 // encoding is the charset to use and modifier "allows the user to select
620 // a specific instance of localization data within a single category"
622 // for example, the following strings are valid:
627 // de_DE.iso88591@euro
629 // for now we don't use the encoding, although we probably should (doing
630 // translations of the msg catalogs on the fly as required) (TODO)
632 // we need the modified for languages like Valencian: ca_ES@valencia
633 // though, remember it
635 size_t posModifier
= langFull
.find_first_of(wxS("@"));
636 if ( posModifier
!= wxString::npos
)
637 modifier
= langFull
.Mid(posModifier
);
639 size_t posEndLang
= langFull
.find_first_of(wxS("@."));
640 if ( posEndLang
!= wxString::npos
)
642 langFull
.Truncate(posEndLang
);
645 // in addition to the format above, we also can have full language names
646 // in LANG env var - for example, SuSE is known to use LANG="german" - so
649 // do we have just the language (or sublang too)?
650 bool justLang
= langFull
.length() == LEN_LANG
;
652 (langFull
.length() == LEN_FULL
&& langFull
[LEN_LANG
] == wxS('_')) )
654 // 0. Make sure the lang is according to latest ISO 639
655 // (this is necessary because glibc uses iw and in instead
656 // of he and id respectively).
658 // the language itself (second part is the dialect/sublang)
659 wxString langOrig
= ExtractLang(langFull
);
662 if ( langOrig
== wxS("iw"))
664 else if (langOrig
== wxS("in"))
666 else if (langOrig
== wxS("ji"))
668 else if (langOrig
== wxS("no_NO"))
670 else if (langOrig
== wxS("no_NY"))
672 else if (langOrig
== wxS("no"))
678 if ( lang
!= langOrig
)
680 langFull
= lang
+ ExtractNotLang(langFull
);
683 // 1. Try to find the language either as is:
684 // a) With modifier if set
685 if ( !modifier
.empty() )
687 wxString langFullWithModifier
= langFull
+ modifier
;
688 for ( i
= 0; i
< count
; i
++ )
690 if ( ms_languagesDB
->Item(i
).CanonicalName
== langFullWithModifier
)
695 // b) Without modifier
696 if ( modifier
.empty() || i
== count
)
698 for ( i
= 0; i
< count
; i
++ )
700 if ( ms_languagesDB
->Item(i
).CanonicalName
== langFull
)
705 // 2. If langFull is of the form xx_YY, try to find xx:
706 if ( i
== count
&& !justLang
)
708 for ( i
= 0; i
< count
; i
++ )
710 if ( ms_languagesDB
->Item(i
).CanonicalName
== lang
)
717 // 3. If langFull is of the form xx, try to find any xx_YY record:
718 if ( i
== count
&& justLang
)
720 for ( i
= 0; i
< count
; i
++ )
722 if ( ExtractLang(ms_languagesDB
->Item(i
).CanonicalName
)
730 else // not standard format
732 // try to find the name in verbose description
733 for ( i
= 0; i
< count
; i
++ )
735 if (ms_languagesDB
->Item(i
).Description
.CmpNoCase(langFull
) == 0)
741 #elif defined(__WIN32__)
742 LCID lcid
= GetUserDefaultLCID();
745 wxUint32 lang
= PRIMARYLANGID(LANGIDFROMLCID(lcid
));
746 wxUint32 sublang
= SUBLANGID(LANGIDFROMLCID(lcid
));
748 for ( i
= 0; i
< count
; i
++ )
750 if (ms_languagesDB
->Item(i
).WinLang
== lang
&&
751 ms_languagesDB
->Item(i
).WinSublang
== sublang
)
757 //else: leave wxlang == wxLANGUAGE_UNKNOWN
762 // we did find a matching entry, use it
763 return ms_languagesDB
->Item(i
).Language
;
766 // no info about this language in the database
767 return wxLANGUAGE_UNKNOWN
;
770 // ----------------------------------------------------------------------------
772 // ----------------------------------------------------------------------------
774 // this is a bit strange as under Windows we get the encoding name using its
775 // numeric value and under Unix we do it the other way round, but this just
776 // reflects the way different systems provide the encoding info
779 wxString
wxLocale::GetSystemEncodingName()
783 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
784 // FIXME: what is the error return value for GetACP()?
785 UINT codepage
= ::GetACP();
786 encname
.Printf(wxS("windows-%u"), codepage
);
787 #elif defined(__WXMAC__)
788 // default is just empty string, this resolves to the default system
790 #elif defined(__UNIX_LIKE__)
792 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
793 // GNU libc provides current character set this way (this conforms
795 char *oldLocale
= strdup(setlocale(LC_CTYPE
, NULL
));
796 setlocale(LC_CTYPE
, "");
797 const char *alang
= nl_langinfo(CODESET
);
798 setlocale(LC_CTYPE
, oldLocale
);
803 encname
= wxString::FromAscii( alang
);
805 else // nl_langinfo() failed
806 #endif // HAVE_LANGINFO_H
808 // if we can't get at the character set directly, try to see if it's in
809 // the environment variables (in most cases this won't work, but I was
811 char *lang
= getenv( "LC_ALL");
812 char *dot
= lang
? strchr(lang
, '.') : NULL
;
815 lang
= getenv( "LC_CTYPE" );
817 dot
= strchr(lang
, '.' );
821 lang
= getenv( "LANG");
823 dot
= strchr(lang
, '.');
828 encname
= wxString::FromAscii( dot
+1 );
837 wxFontEncoding
wxLocale::GetSystemEncoding()
839 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
840 UINT codepage
= ::GetACP();
842 // wxWidgets only knows about CP1250-1257, 874, 932, 936, 949, 950
843 if ( codepage
>= 1250 && codepage
<= 1257 )
845 return (wxFontEncoding
)(wxFONTENCODING_CP1250
+ codepage
- 1250);
848 if ( codepage
== 874 )
850 return wxFONTENCODING_CP874
;
853 if ( codepage
== 932 )
855 return wxFONTENCODING_CP932
;
858 if ( codepage
== 936 )
860 return wxFONTENCODING_CP936
;
863 if ( codepage
== 949 )
865 return wxFONTENCODING_CP949
;
868 if ( codepage
== 950 )
870 return wxFONTENCODING_CP950
;
872 #elif defined(__WXMAC__)
873 CFStringEncoding encoding
= 0 ;
874 encoding
= CFStringGetSystemEncoding() ;
875 return wxMacGetFontEncFromSystemEnc( encoding
) ;
876 #elif defined(__UNIX_LIKE__) && wxUSE_FONTMAP
877 const wxString encname
= GetSystemEncodingName();
878 if ( !encname
.empty() )
880 wxFontEncoding enc
= wxFontMapperBase::GetEncodingFromName(encname
);
882 // on some modern Linux systems (RedHat 8) the default system locale
883 // is UTF8 -- but it isn't supported by wxGTK1 in ANSI build at all so
884 // don't even try to use it in this case
885 #if !wxUSE_UNICODE && \
886 ((defined(__WXGTK__) && !defined(__WXGTK20__)) || defined(__WXMOTIF__))
887 if ( enc
== wxFONTENCODING_UTF8
)
889 // the most similar supported encoding...
890 enc
= wxFONTENCODING_ISO8859_1
;
892 #endif // !wxUSE_UNICODE
894 // GetEncodingFromName() returns wxFONTENCODING_DEFAULT for C locale
895 // (a.k.a. US-ASCII) which is arguably a bug but keep it like this for
896 // backwards compatibility and just take care to not return
897 // wxFONTENCODING_DEFAULT from here as this surely doesn't make sense
898 if ( enc
== wxFONTENCODING_DEFAULT
)
900 // we don't have wxFONTENCODING_ASCII, so use the closest one
901 return wxFONTENCODING_ISO8859_1
;
904 if ( enc
!= wxFONTENCODING_MAX
)
908 //else: return wxFONTENCODING_SYSTEM below
912 return wxFONTENCODING_SYSTEM
;
916 void wxLocale::AddLanguage(const wxLanguageInfo
& info
)
919 ms_languagesDB
->Add(info
);
923 const wxLanguageInfo
*wxLocale::GetLanguageInfo(int lang
)
927 // calling GetLanguageInfo(wxLANGUAGE_DEFAULT) is a natural thing to do, so
929 if ( lang
== wxLANGUAGE_DEFAULT
)
930 lang
= GetSystemLanguage();
932 const size_t count
= ms_languagesDB
->GetCount();
933 for ( size_t i
= 0; i
< count
; i
++ )
935 if ( ms_languagesDB
->Item(i
).Language
== lang
)
937 // We need to create a temporary here in order to make this work with BCC in final build mode
938 wxLanguageInfo
*ptr
= &ms_languagesDB
->Item(i
);
947 wxString
wxLocale::GetLanguageName(int lang
)
949 if ( lang
== wxLANGUAGE_DEFAULT
|| lang
== wxLANGUAGE_UNKNOWN
)
950 return wxEmptyString
;
952 const wxLanguageInfo
*info
= GetLanguageInfo(lang
);
954 return wxEmptyString
;
956 return info
->Description
;
960 wxString
wxLocale::GetLanguageCanonicalName(int lang
)
962 if ( lang
== wxLANGUAGE_DEFAULT
|| lang
== wxLANGUAGE_UNKNOWN
)
963 return wxEmptyString
;
965 const wxLanguageInfo
*info
= GetLanguageInfo(lang
);
967 return wxEmptyString
;
969 return info
->CanonicalName
;
973 const wxLanguageInfo
*wxLocale::FindLanguageInfo(const wxString
& locale
)
977 const wxLanguageInfo
*infoRet
= NULL
;
979 const size_t count
= ms_languagesDB
->GetCount();
980 for ( size_t i
= 0; i
< count
; i
++ )
982 const wxLanguageInfo
*info
= &ms_languagesDB
->Item(i
);
984 if ( wxStricmp(locale
, info
->CanonicalName
) == 0 ||
985 wxStricmp(locale
, info
->Description
) == 0 )
987 // exact match, stop searching
992 if ( wxStricmp(locale
, info
->CanonicalName
.BeforeFirst(wxS('_'))) == 0 )
994 // a match -- but maybe we'll find an exact one later, so continue
997 // OTOH, maybe we had already found a language match and in this
998 // case don't overwrite it because the entry for the default
999 // country always appears first in ms_languagesDB
1008 wxString
wxLocale::GetSysName() const
1010 return wxSetlocale(LC_ALL
, NULL
);
1014 wxLocale::~wxLocale()
1016 // Restore old translations object.
1017 // See DoCommonInit() for explanation of why this is needed for backward
1019 if ( wxTranslations::Get() == &m_translations
)
1022 wxTranslations::SetNonOwned(&m_pOldLocale
->m_translations
);
1024 wxTranslations::Set(NULL
);
1027 // restore old locale pointer
1028 wxSetLocale(m_pOldLocale
);
1030 wxSetlocale(LC_ALL
, m_pszOldLocale
);
1031 free((wxChar
*)m_pszOldLocale
); // const_cast
1035 // check if the given locale is provided by OS and C run time
1037 bool wxLocale::IsAvailable(int lang
)
1039 const wxLanguageInfo
*info
= wxLocale::GetLanguageInfo(lang
);
1040 wxCHECK_MSG( info
, false, wxS("invalid language") );
1042 #if defined(__WIN32__)
1043 if ( !info
->WinLang
)
1046 if ( !::IsValidLocale(info
->GetLCID(), LCID_INSTALLED
) )
1049 #elif defined(__UNIX__)
1051 // Test if setting the locale works, then set it back.
1052 const char *oldLocale
= wxSetlocaleTryUTF8(LC_ALL
, info
->CanonicalName
);
1055 // Some C libraries don't like xx_YY form and require xx only
1056 oldLocale
= wxSetlocaleTryUTF8(LC_ALL
, ExtractLang(info
->CanonicalName
));
1060 // restore the original locale
1061 wxSetlocale(LC_ALL
, oldLocale
);
1068 bool wxLocale::AddCatalog(const wxString
& domain
)
1070 wxTranslations
*t
= wxTranslations::Get();
1073 return t
->AddCatalog(domain
);
1076 bool wxLocale::AddCatalog(const wxString
& domain
, wxLanguage msgIdLanguage
)
1078 wxTranslations
*t
= wxTranslations::Get();
1081 return t
->AddCatalog(domain
, msgIdLanguage
);
1084 // add a catalog to our linked list
1085 bool wxLocale::AddCatalog(const wxString
& szDomain
,
1086 wxLanguage msgIdLanguage
,
1087 const wxString
& msgIdCharset
)
1089 wxTranslations
*t
= wxTranslations::Get();
1093 wxUnusedVar(msgIdCharset
);
1094 return t
->AddCatalog(szDomain
, msgIdLanguage
);
1096 return t
->AddCatalog(szDomain
, msgIdLanguage
, msgIdCharset
);
1100 bool wxLocale::IsLoaded(const wxString
& domain
) const
1102 wxTranslations
*t
= wxTranslations::Get();
1105 return t
->IsLoaded(domain
);
1108 wxString
wxLocale::GetHeaderValue(const wxString
& header
,
1109 const wxString
& domain
) const
1111 wxTranslations
*t
= wxTranslations::Get();
1113 return wxEmptyString
;
1114 return t
->GetHeaderValue(header
, domain
);
1117 // ----------------------------------------------------------------------------
1118 // accessors for locale-dependent data
1119 // ----------------------------------------------------------------------------
1121 #if defined(__WXMSW__) || defined(__WXOSX__)
1126 // This function translates from Unicode date formats described at
1128 // http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
1130 // to strftime()-like syntax. This translation is not lossless but we try to do
1133 static wxString
TranslateFromUnicodeFormat(const wxString
& fmt
)
1136 fmtWX
.reserve(fmt
.length());
1139 size_t lastCount
= 0;
1141 const char* formatchars
=
1149 for ( wxString::const_iterator p
= fmt
.begin(); /* end handled inside */; ++p
)
1151 if ( p
!= fmt
.end() )
1159 const wxUniChar ch
= (*p
).GetValue();
1160 if ( ch
.IsAscii() && strchr(formatchars
, ch
) )
1162 // these characters come in groups, start counting them
1169 // interpret any special characters we collected so far
1175 switch ( lastCount
)
1179 // these two are the same as we don't distinguish
1180 // between 1 and 2 digits for days
1193 wxFAIL_MSG( "too many 'd's" );
1198 switch ( lastCount
)
1207 wxFAIL_MSG( "wrong number of 'D's" );
1211 switch ( lastCount
)
1219 wxFAIL_MSG( "wrong number of 'w's" );
1223 switch ( lastCount
)
1238 wxFAIL_MSG( "wrong number of 'E's" );
1243 switch ( lastCount
)
1247 // as for 'd' and 'dd' above
1260 wxFAIL_MSG( "too many 'M's" );
1265 switch ( lastCount
)
1277 wxFAIL_MSG( "wrong number of 'y's" );
1282 switch ( lastCount
)
1290 wxFAIL_MSG( "wrong number of 'H's" );
1295 switch ( lastCount
)
1303 wxFAIL_MSG( "wrong number of 'h's" );
1308 switch ( lastCount
)
1316 wxFAIL_MSG( "wrong number of 'm's" );
1321 switch ( lastCount
)
1329 wxFAIL_MSG( "wrong number of 's's" );
1334 // strftime() doesn't have era string,
1335 // ignore this format
1336 wxASSERT_MSG( lastCount
<= 2, "too many 'g's" );
1346 switch ( lastCount
)
1354 wxFAIL_MSG( "too many 't's" );
1359 wxFAIL_MSG( "unreachable" );
1366 if ( p
== fmt
.end() )
1369 // not a special character so must be just a separator, treat as is
1370 if ( *p
== wxT('%') )
1372 // this one needs to be escaped
1382 } // anonymous namespace
1384 #endif // __WXMSW__ || __WXOSX__
1386 #if defined(__WXMSW__)
1391 LCTYPE
GetLCTYPEFormatFromLocalInfo(wxLocaleInfo index
)
1395 case wxLOCALE_SHORT_DATE_FMT
:
1396 return LOCALE_SSHORTDATE
;
1398 case wxLOCALE_LONG_DATE_FMT
:
1399 return LOCALE_SLONGDATE
;
1401 case wxLOCALE_TIME_FMT
:
1402 return LOCALE_STIMEFORMAT
;
1405 wxFAIL_MSG( "no matching LCTYPE" );
1411 } // anonymous namespace
1414 wxString
wxLocale::GetInfo(wxLocaleInfo index
, wxLocaleCategory
WXUNUSED(cat
))
1416 wxUint32 lcid
= LOCALE_USER_DEFAULT
;
1417 if ( wxGetLocale() )
1419 const wxLanguageInfo
* const
1420 info
= GetLanguageInfo(wxGetLocale()->GetLanguage());
1422 lcid
= info
->GetLCID();
1432 case wxLOCALE_DECIMAL_POINT
:
1433 if ( ::GetLocaleInfo(lcid
, LOCALE_SDECIMAL
, buf
, WXSIZEOF(buf
)) )
1437 case wxLOCALE_SHORT_DATE_FMT
:
1438 case wxLOCALE_LONG_DATE_FMT
:
1439 case wxLOCALE_TIME_FMT
:
1440 if ( ::GetLocaleInfo(lcid
, GetLCTYPEFormatFromLocalInfo(index
),
1441 buf
, WXSIZEOF(buf
)) )
1443 return TranslateFromUnicodeFormat(buf
);
1447 case wxLOCALE_DATE_TIME_FMT
:
1448 // there doesn't seem to be any specific setting for this, so just
1449 // combine date and time ones
1451 // we use the short date because this is what "%c" uses by default
1452 // ("%#c" uses long date but we have no way to specify the
1453 // alternate representation here)
1455 const wxString datefmt
= GetInfo(wxLOCALE_SHORT_DATE_FMT
);
1456 if ( datefmt
.empty() )
1459 const wxString timefmt
= GetInfo(wxLOCALE_TIME_FMT
);
1460 if ( timefmt
.empty() )
1463 str
<< datefmt
<< ' ' << timefmt
;
1468 wxFAIL_MSG( "unknown wxLocaleInfo" );
1474 #elif defined(__WXOSX__)
1477 wxString
wxLocale::GetInfo(wxLocaleInfo index
, wxLocaleCategory
WXUNUSED(cat
))
1479 CFLocaleRef userLocaleRefRaw
;
1480 if ( wxGetLocale() )
1482 userLocaleRefRaw
= CFLocaleCreate
1484 kCFAllocatorDefault
,
1485 wxCFStringRef(wxGetLocale()->GetCanonicalName())
1488 else // no current locale, use the default one
1490 userLocaleRefRaw
= CFLocaleCopyCurrent();
1493 wxCFRef
<CFLocaleRef
> userLocaleRef(userLocaleRefRaw
);
1495 CFStringRef cfstr
= 0;
1498 case wxLOCALE_THOUSANDS_SEP
:
1499 cfstr
= (CFStringRef
) CFLocaleGetValue(userLocaleRef
, kCFLocaleGroupingSeparator
);
1502 case wxLOCALE_DECIMAL_POINT
:
1503 cfstr
= (CFStringRef
) CFLocaleGetValue(userLocaleRef
, kCFLocaleDecimalSeparator
);
1506 case wxLOCALE_SHORT_DATE_FMT
:
1507 case wxLOCALE_LONG_DATE_FMT
:
1508 case wxLOCALE_DATE_TIME_FMT
:
1509 case wxLOCALE_TIME_FMT
:
1511 CFDateFormatterStyle dateStyle
= kCFDateFormatterNoStyle
;
1512 CFDateFormatterStyle timeStyle
= kCFDateFormatterNoStyle
;
1515 case wxLOCALE_SHORT_DATE_FMT
:
1516 dateStyle
= kCFDateFormatterShortStyle
;
1518 case wxLOCALE_LONG_DATE_FMT
:
1519 dateStyle
= kCFDateFormatterFullStyle
;
1521 case wxLOCALE_DATE_TIME_FMT
:
1522 dateStyle
= kCFDateFormatterFullStyle
;
1523 timeStyle
= kCFDateFormatterMediumStyle
;
1525 case wxLOCALE_TIME_FMT
:
1526 timeStyle
= kCFDateFormatterMediumStyle
;
1529 wxFAIL_MSG( "unexpected time locale" );
1532 wxCFRef
<CFDateFormatterRef
> dateFormatter( CFDateFormatterCreate
1533 (NULL
, userLocaleRef
, dateStyle
, timeStyle
));
1534 wxCFStringRef cfs
= wxCFRetain( CFDateFormatterGetFormat(dateFormatter
));
1535 wxString format
= TranslateFromUnicodeFormat(cfs
.AsString());
1536 // we always want full years
1537 format
.Replace("%y","%Y");
1543 wxFAIL_MSG( "Unknown locale info" );
1547 wxCFStringRef
str(wxCFRetain(cfstr
));
1548 return str
.AsString();
1551 #else // !__WXMSW__ && !__WXOSX__, assume generic POSIX
1556 wxString
GetDateFormatFromLangInfo(wxLocaleInfo index
)
1558 #ifdef HAVE_LANGINFO_H
1559 // array containing parameters for nl_langinfo() indexes by offset of index
1560 // from wxLOCALE_SHORT_DATE_FMT
1561 static const nl_item items
[] =
1563 D_FMT
, D_T_FMT
, D_T_FMT
, T_FMT
,
1566 const int nlidx
= index
- wxLOCALE_SHORT_DATE_FMT
;
1567 if ( nlidx
< 0 || nlidx
>= (int)WXSIZEOF(items
) )
1569 wxFAIL_MSG( "logic error in GetInfo() code" );
1573 const wxString
fmt(nl_langinfo(items
[nlidx
]));
1575 // just return the format returned by nl_langinfo() except for long date
1576 // format which we need to recover from date/time format ourselves (but not
1577 // if we failed completely)
1578 if ( fmt
.empty() || index
!= wxLOCALE_LONG_DATE_FMT
)
1581 // this is not 100% precise but the idea is that a typical date/time format
1582 // under POSIX systems is a combination of a long date format with time one
1583 // so we should be able to get just the long date format by removing all
1584 // time-specific format specifiers
1585 static const char *timeFmtSpecs
= "HIklMpPrRsSTXzZ";
1586 static const char *timeSep
= " :./-";
1588 wxString fmtDateOnly
;
1589 const wxString::const_iterator end
= fmt
.end();
1590 wxString::const_iterator lastSep
= end
;
1591 for ( wxString::const_iterator p
= fmt
.begin(); p
!= end
; ++p
)
1593 if ( strchr(timeSep
, *p
) )
1595 if ( lastSep
== end
)
1598 // skip it for now, we'll discard it if it's followed by a time
1599 // specifier later or add it to fmtDateOnly if it is not
1604 (p
+ 1 != end
) && strchr(timeFmtSpecs
, p
[1]) )
1606 // time specified found: skip it and any preceding separators
1612 if ( lastSep
!= end
)
1614 fmtDateOnly
+= wxString(lastSep
, p
);
1622 #else // !HAVE_LANGINFO_H
1625 // no fallback, let the application deal with unavailability of
1626 // nl_langinfo() itself as there is no good way for us to do it (well, we
1627 // could try to reverse engineer the format from strftime() output but this
1628 // looks like too much trouble considering the relatively small number of
1629 // systems without nl_langinfo() still in use)
1631 #endif // HAVE_LANGINFO_H/!HAVE_LANGINFO_H
1634 } // anonymous namespace
1637 wxString
wxLocale::GetInfo(wxLocaleInfo index
, wxLocaleCategory cat
)
1639 lconv
* const lc
= localeconv();
1645 case wxLOCALE_THOUSANDS_SEP
:
1646 if ( cat
== wxLOCALE_CAT_NUMBER
)
1647 return lc
->thousands_sep
;
1648 else if ( cat
== wxLOCALE_CAT_MONEY
)
1649 return lc
->mon_thousands_sep
;
1651 wxFAIL_MSG( "invalid wxLocaleCategory" );
1655 case wxLOCALE_DECIMAL_POINT
:
1656 if ( cat
== wxLOCALE_CAT_NUMBER
)
1657 return lc
->decimal_point
;
1658 else if ( cat
== wxLOCALE_CAT_MONEY
)
1659 return lc
->mon_decimal_point
;
1661 wxFAIL_MSG( "invalid wxLocaleCategory" );
1664 case wxLOCALE_SHORT_DATE_FMT
:
1665 case wxLOCALE_LONG_DATE_FMT
:
1666 case wxLOCALE_DATE_TIME_FMT
:
1667 case wxLOCALE_TIME_FMT
:
1668 if ( cat
!= wxLOCALE_CAT_DATE
&& cat
!= wxLOCALE_CAT_DEFAULT
)
1670 wxFAIL_MSG( "invalid wxLocaleCategory" );
1674 return GetDateFormatFromLangInfo(index
);
1678 wxFAIL_MSG( "unknown wxLocaleInfo value" );
1686 // ----------------------------------------------------------------------------
1687 // global functions and variables
1688 // ----------------------------------------------------------------------------
1690 // retrieve/change current locale
1691 // ------------------------------
1693 // the current locale object
1694 static wxLocale
*g_pLocale
= NULL
;
1696 wxLocale
*wxGetLocale()
1701 wxLocale
*wxSetLocale(wxLocale
*pLocale
)
1703 wxLocale
*pOld
= g_pLocale
;
1704 g_pLocale
= pLocale
;
1710 // ----------------------------------------------------------------------------
1711 // wxLocale module (for lazy destruction of languagesDB)
1712 // ----------------------------------------------------------------------------
1714 class wxLocaleModule
: public wxModule
1716 DECLARE_DYNAMIC_CLASS(wxLocaleModule
)
1727 wxLocale::DestroyLanguagesDB();
1731 IMPLEMENT_DYNAMIC_CLASS(wxLocaleModule
, wxModule
)
1733 #endif // wxUSE_INTL