+// this is a bit strange as under Windows we get the encoding name using its
+// numeric value and under Unix we do it the other way round, but this just
+// reflects the way different systems provide the encoding info
+
+/* static */
+wxString wxLocale::GetSystemEncodingName()
+{
+ wxString encname;
+
+#if defined(__WIN32__) && !defined(__WXMICROWIN__)
+ // FIXME: what is the error return value for GetACP()?
+ UINT codepage = ::GetACP();
+ encname.Printf(wxS("windows-%u"), codepage);
+#elif defined(__WXMAC__)
+ // default is just empty string, this resolves to the default system
+ // encoding later
+#elif defined(__UNIX_LIKE__)
+
+#if defined(HAVE_LANGINFO_H) && defined(CODESET)
+ // GNU libc provides current character set this way (this conforms
+ // to Unix98)
+ char *oldLocale = strdup(setlocale(LC_CTYPE, NULL));
+ setlocale(LC_CTYPE, "");
+ const char *alang = nl_langinfo(CODESET);
+ setlocale(LC_CTYPE, oldLocale);
+ free(oldLocale);
+
+ if ( alang )
+ {
+ encname = wxString::FromAscii( alang );
+ }
+ else // nl_langinfo() failed
+#endif // HAVE_LANGINFO_H
+ {
+ // if we can't get at the character set directly, try to see if it's in
+ // the environment variables (in most cases this won't work, but I was
+ // out of ideas)
+ char *lang = getenv( "LC_ALL");
+ char *dot = lang ? strchr(lang, '.') : NULL;
+ if (!dot)
+ {
+ lang = getenv( "LC_CTYPE" );
+ if ( lang )
+ dot = strchr(lang, '.' );
+ }
+ if (!dot)
+ {
+ lang = getenv( "LANG");
+ if ( lang )
+ dot = strchr(lang, '.');
+ }
+
+ if ( dot )
+ {
+ encname = wxString::FromAscii( dot+1 );
+ }
+ }
+#endif // Win32/Unix
+
+ return encname;
+}
+
+/* static */
+wxFontEncoding wxLocale::GetSystemEncoding()
+{
+#if defined(__WIN32__) && !defined(__WXMICROWIN__)
+ UINT codepage = ::GetACP();
+
+ // wxWidgets only knows about CP1250-1257, 874, 932, 936, 949, 950
+ if ( codepage >= 1250 && codepage <= 1257 )
+ {
+ return (wxFontEncoding)(wxFONTENCODING_CP1250 + codepage - 1250);
+ }
+
+ if ( codepage == 874 )
+ {
+ return wxFONTENCODING_CP874;
+ }
+
+ if ( codepage == 932 )
+ {
+ return wxFONTENCODING_CP932;
+ }
+
+ if ( codepage == 936 )
+ {
+ return wxFONTENCODING_CP936;
+ }
+
+ if ( codepage == 949 )
+ {
+ return wxFONTENCODING_CP949;
+ }
+
+ if ( codepage == 950 )
+ {
+ return wxFONTENCODING_CP950;
+ }
+#elif defined(__WXMAC__)
+ CFStringEncoding encoding = 0 ;
+ encoding = CFStringGetSystemEncoding() ;
+ return wxMacGetFontEncFromSystemEnc( encoding ) ;
+#elif defined(__UNIX_LIKE__) && wxUSE_FONTMAP
+ const wxString encname = GetSystemEncodingName();
+ if ( !encname.empty() )
+ {
+ wxFontEncoding enc = wxFontMapperBase::GetEncodingFromName(encname);
+
+ // on some modern Linux systems (RedHat 8) the default system locale
+ // is UTF8 -- but it isn't supported by wxGTK1 in ANSI build at all so
+ // don't even try to use it in this case
+#if !wxUSE_UNICODE && \
+ ((defined(__WXGTK__) && !defined(__WXGTK20__)) || defined(__WXMOTIF__))
+ if ( enc == wxFONTENCODING_UTF8 )
+ {
+ // the most similar supported encoding...
+ enc = wxFONTENCODING_ISO8859_1;
+ }
+#endif // !wxUSE_UNICODE
+
+ // GetEncodingFromName() returns wxFONTENCODING_DEFAULT for C locale
+ // (a.k.a. US-ASCII) which is arguably a bug but keep it like this for
+ // backwards compatibility and just take care to not return
+ // wxFONTENCODING_DEFAULT from here as this surely doesn't make sense
+ if ( enc == wxFONTENCODING_DEFAULT )
+ {
+ // we don't have wxFONTENCODING_ASCII, so use the closest one
+ return wxFONTENCODING_ISO8859_1;
+ }
+
+ if ( enc != wxFONTENCODING_MAX )
+ {
+ return enc;
+ }
+ //else: return wxFONTENCODING_SYSTEM below
+ }
+#endif // Win32/Unix
+
+ return wxFONTENCODING_SYSTEM;
+}
+
+/* static */
+void wxLocale::AddLanguage(const wxLanguageInfo& info)
+{
+ CreateLanguagesDB();
+ ms_languagesDB->Add(info);
+}
+
+/* static */
+const wxLanguageInfo *wxLocale::GetLanguageInfo(int lang)
+{
+ CreateLanguagesDB();
+
+ // calling GetLanguageInfo(wxLANGUAGE_DEFAULT) is a natural thing to do, so
+ // make it work
+ if ( lang == wxLANGUAGE_DEFAULT )
+ lang = GetSystemLanguage();
+
+ const size_t count = ms_languagesDB->GetCount();
+ for ( size_t i = 0; i < count; i++ )
+ {
+ if ( ms_languagesDB->Item(i).Language == lang )
+ {
+ // We need to create a temporary here in order to make this work with BCC in final build mode
+ wxLanguageInfo *ptr = &ms_languagesDB->Item(i);
+ return ptr;
+ }
+ }
+
+ return NULL;
+}
+
+/* static */
+wxString wxLocale::GetLanguageName(int lang)
+{
+ if ( lang == wxLANGUAGE_DEFAULT || lang == wxLANGUAGE_UNKNOWN )
+ return wxEmptyString;
+
+ const wxLanguageInfo *info = GetLanguageInfo(lang);
+ if ( !info )
+ return wxEmptyString;
+ else
+ return info->Description;
+}
+
+/* static */
+wxString wxLocale::GetLanguageCanonicalName(int lang)
+{
+ if ( lang == wxLANGUAGE_DEFAULT || lang == wxLANGUAGE_UNKNOWN )
+ return wxEmptyString;
+
+ const wxLanguageInfo *info = GetLanguageInfo(lang);
+ if ( !info )
+ return wxEmptyString;
+ else
+ return info->CanonicalName;
+}
+
+/* static */
+const wxLanguageInfo *wxLocale::FindLanguageInfo(const wxString& locale)
+{
+ CreateLanguagesDB();
+
+ const wxLanguageInfo *infoRet = NULL;
+
+ const size_t count = ms_languagesDB->GetCount();
+ for ( size_t i = 0; i < count; i++ )
+ {
+ const wxLanguageInfo *info = &ms_languagesDB->Item(i);
+
+ if ( wxStricmp(locale, info->CanonicalName) == 0 ||
+ wxStricmp(locale, info->Description) == 0 )
+ {
+ // exact match, stop searching
+ infoRet = info;
+ break;
+ }
+
+ if ( wxStricmp(locale, info->CanonicalName.BeforeFirst(wxS('_'))) == 0 )
+ {
+ // a match -- but maybe we'll find an exact one later, so continue
+ // looking
+ //
+ // OTOH, maybe we had already found a language match and in this
+ // case don't overwrite it because the entry for the default
+ // country always appears first in ms_languagesDB
+ if ( !infoRet )
+ infoRet = info;
+ }
+ }
+
+ return infoRet;
+}
+
+wxString wxLocale::GetSysName() const
+{
+ return wxSetlocale(LC_ALL, NULL);