]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/intl.cpp
use platform-specific styles in xTR_DEFAULT_STYLE but don't impose them forcefully...
[wxWidgets.git] / src / common / intl.cpp
index c74b6fbb2141863c04636241176d2705cfb42c86..dfc8eb3ad35ce8302ff50b93ae3793b6bfe9c78b 100644 (file)
@@ -970,8 +970,10 @@ private:
 class wxMsgCatalog
 {
 public:
+#if !wxUSE_UNICODE
     wxMsgCatalog() { m_conv = NULL; }
     ~wxMsgCatalog();
+#endif
 
     // load the catalog from disk (szDirPrefix corresponds to language)
     bool Load(const wxString& dirPrefix, const wxString& name,
@@ -990,9 +992,11 @@ private:
     wxMessagesHash  m_messages; // all messages in the catalog
     wxString        m_name;     // name of the domain
 
+#if !wxUSE_UNICODE
     // the conversion corresponding to this catalog charset if we installed it
     // as the global one
     wxCSConv *m_conv;
+#endif
 
     wxPluralFormsCalculatorPtr  m_pluralFormsCalculator;
 };
@@ -1008,6 +1012,68 @@ static wxArrayString gs_searchPrefixes;
 // implementation
 // ============================================================================
 
+// ----------------------------------------------------------------------------
+// wxLanguageInfo
+// ----------------------------------------------------------------------------
+
+#ifdef __WXMSW__
+
+// helper used by wxLanguageInfo::GetLocaleName() and elsewhere to determine
+// whether the locale is Unicode-only (it is if this function returns empty
+// string)
+static wxString wxGetANSICodePageForLocale(LCID lcid)
+{
+    wxString cp;
+
+    wxChar buffer[16];
+    if ( ::GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE,
+                         buffer, WXSIZEOF(buffer)) > 0 )
+    {
+        if ( buffer[0] != _T('0') || buffer[1] != _T('\0') )
+            cp = buffer;
+        //else: this locale doesn't use ANSI code page
+    }
+
+    return cp;
+}
+
+wxUint32 wxLanguageInfo::GetLCID() const
+{
+    return MAKELCID(MAKELANGID(WinLang, WinSublang), SORT_DEFAULT);
+}
+
+wxString wxLanguageInfo::GetLocaleName() const
+{
+    wxString locale;
+
+    const LCID lcid = GetLCID();
+
+    wxChar buffer[256];
+    buffer[0] = _T('\0');
+    if ( !::GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE, buffer, WXSIZEOF(buffer)) )
+    {
+        wxLogLastError(_T("GetLocaleInfo(LOCALE_SENGLANGUAGE)"));
+        return locale;
+    }
+
+    locale << buffer;
+    if ( ::GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY,
+                         buffer, WXSIZEOF(buffer)) > 0 )
+    {
+        locale << _T('_') << buffer;
+    }
+
+    const wxString cp = wxGetANSICodePageForLocale(lcid);
+    if ( !cp.empty() )
+    {
+        locale << _T('.') << cp;
+    }
+
+    return locale;
+}
+
+#endif // __WXMSW__
+
 // ----------------------------------------------------------------------------
 // wxMsgCatalogFile class
 // ----------------------------------------------------------------------------
@@ -1434,6 +1500,7 @@ void wxMsgCatalogFile::FillHash(wxMessagesHash& hash,
 // wxMsgCatalog class
 // ----------------------------------------------------------------------------
 
+#if !wxUSE_UNICODE
 wxMsgCatalog::~wxMsgCatalog()
 {
     if ( m_conv )
@@ -1448,6 +1515,7 @@ wxMsgCatalog::~wxMsgCatalog()
         delete m_conv;
     }
 }
+#endif // !wxUSE_UNICODE
 
 bool wxMsgCatalog::Load(const wxString& dirPrefix, const wxString& name,
                         const wxString& msgIdCharset, bool bConvertEncoding)
@@ -1461,6 +1529,7 @@ bool wxMsgCatalog::Load(const wxString& dirPrefix, const wxString& name,
 
     file.FillHash(m_messages, msgIdCharset, bConvertEncoding);
 
+#if !wxUSE_UNICODE
     // we should use a conversion compatible with the message catalog encoding
     // in the GUI if we don't convert the strings to the current conversion but
     // as the encoding is global, only change it once, otherwise we could get
@@ -1476,6 +1545,7 @@ bool wxMsgCatalog::Load(const wxString& dirPrefix, const wxString& name,
         wxConvUI =
         m_conv = new wxCSConv(file.GetCharset());
     }
+#endif // !wxUSE_UNICODE
 
     return true;
 }
@@ -1569,27 +1639,11 @@ bool wxLocale::Init(const wxString& name,
                  wxS("no locale to set in wxLocale::Init()") );
   }
 
-#ifdef __WXWINCE__
-  // FIXME: I'm guessing here
-  wxChar localeName[256];
-  int ret = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLANGUAGE, localeName,
-      256);
-  if (ret != 0)
-  {
-    m_pszOldLocale = wxStrdup(wxConvLibc.cWC2MB(localeName));
-  }
-  else
-    m_pszOldLocale = NULL;
-
-  // TODO: how to find languageId
-  // SetLocaleInfo(languageId, SORT_DEFAULT, localeName);
-#else
   const char *oldLocale = wxSetlocale(LC_ALL, szLocale);
   if ( oldLocale )
       m_pszOldLocale = wxStrdup(oldLocale);
   else
       m_pszOldLocale = NULL;
-#endif
 
   if ( m_pszOldLocale == NULL )
     wxLogError(_("locale '%s' can not be set."), szLocale);
@@ -1778,94 +1832,57 @@ bool wxLocale::Init(int language, int flags)
 #endif // __AIX__
 
 #elif defined(__WIN32__)
-
-    #if wxUSE_UNICODE && (defined(__VISUALC__) || defined(__MINGW32__))
-        // NB: setlocale() from msvcrt.dll (used by VC++ and Mingw)
-        //     can't set locale to language that can only be written using
-        //     Unicode.  Therefore wxSetlocale call failed, but we don't want
-        //     to report it as an error -- so that at least message catalogs
-        //     can be used. Watch for code marked with
-        //     #ifdef SETLOCALE_FAILS_ON_UNICODE_LANGS bellow.
-        #define SETLOCALE_FAILS_ON_UNICODE_LANGS
-    #endif
-
     const char *retloc = "C";
-    if (language != wxLANGUAGE_DEFAULT)
+    if ( language != wxLANGUAGE_DEFAULT )
     {
-        if (info->WinLang == 0)
+        if ( info->WinLang == 0 )
         {
             wxLogWarning(wxS("Locale '%s' not supported by OS."), name.c_str());
             // retloc already set to "C"
         }
-        else
+        else // language supported by Windows
         {
-            int codepage
-                         #ifdef SETLOCALE_FAILS_ON_UNICODE_LANGS
-                         = -1
-                         #endif
-                         ;
-            wxUint32 lcid = MAKELCID(MAKELANGID(info->WinLang, info->WinSublang),
-                                     SORT_DEFAULT);
-            // FIXME
+            const wxUint32 lcid = info->GetLCID();
+
+            // Windows CE doesn't have SetThreadLocale() and there doesn't seem
+            // to be any equivalent
 #ifndef __WXWINCE__
-            SetThreadLocale(lcid);
-#endif
-            // NB: we must translate LCID to CRT's setlocale string ourselves,
-            //     because SetThreadLocale does not modify change the
-            //     interpretation of setlocale(LC_ALL, "") call:
-            wxChar buffer[256];
-            buffer[0] = wxS('\0');
-            GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE, buffer, 256);
-            locale << buffer;
-            if (GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY, buffer, 256) > 0)
-                locale << wxS("_") << buffer;
-            if (GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE, buffer, 256) > 0)
-            {
-                codepage = wxAtoi(buffer);
-                if (codepage != 0)
-                    locale << wxS(".") << buffer;
-            }
-            if (locale.empty())
+            // change locale used by Windows functions
+            ::SetThreadLocale(lcid);
+#endif
+
+            // and also call setlocale() to change locale used by the CRT
+            locale = info->GetLocaleName();
+            if ( locale.empty() )
             {
-                wxLogLastError(wxS("SetThreadLocale"));
                 ret = false;
             }
-            else
+            else // have a valid locale
             {
-            // FIXME
-#ifndef __WXWINCE__
                 retloc = wxSetlocale(LC_ALL, locale);
-#endif
-#ifdef SETLOCALE_FAILS_ON_UNICODE_LANGS
-                if (codepage == 0 && retloc == NULL)
-                {
-                    retloc = "C";
-                }
-#endif
             }
         }
     }
-    else
+    else // language == wxLANGUAGE_DEFAULT
     {
-            // FIXME
-#ifndef __WXWINCE__
         retloc = wxSetlocale(LC_ALL, wxEmptyString);
-#else
-        retloc = NULL;
-#endif
-#ifdef SETLOCALE_FAILS_ON_UNICODE_LANGS
-        if (retloc == NULL)
+    }
+
+#if wxUSE_UNICODE && (defined(__VISUALC__) || defined(__MINGW32__))
+    // VC++ setlocale() (also used by Mingw) can't set locale to languages that
+    // can only be written using Unicode, therefore wxSetlocale() call fails
+    // for such languages but we don't want to report it as an error -- so that
+    // at least message catalogs can be used.
+    if ( !retloc )
+    {
+        if ( wxGetANSICodePageForLocale(LOCALE_USER_DEFAULT).empty() )
         {
-            wxChar buffer[16];
-            if (GetLocaleInfo(LOCALE_USER_DEFAULT,
-                              LOCALE_IDEFAULTANSICODEPAGE, buffer, 16) > 0 &&
-                 wxStrcmp(buffer, wxS("0")) == 0)
-            {
-                retloc = "C";
-            }
+            // we set the locale to a Unicode-only language, don't treat the
+            // inability of CRT to use it as an error
+            retloc = "C";
         }
-#endif
     }
+#endif // CRT not handling Unicode-only languages
 
     if ( !retloc )
         ret = false;
@@ -2323,12 +2340,7 @@ const wxLanguageInfo *wxLocale::FindLanguageInfo(const wxString& locale)
 
 wxString wxLocale::GetSysName() const
 {
-            // FIXME
-#ifndef __WXWINCE__
     return wxSetlocale(LC_ALL, NULL);
-#else
-    return wxEmptyString;
-#endif
 }
 
 // clean up
@@ -2345,10 +2357,7 @@ wxLocale::~wxLocale()
     // restore old locale pointer
     wxSetLocale(m_pOldLocale);
 
-    // FIXME
-#ifndef __WXWINCE__
     wxSetlocale(LC_ALL, m_pszOldLocale);
-#endif
     free((wxChar *)m_pszOldLocale);     // const_cast
 }
 
@@ -2502,12 +2511,7 @@ bool wxLocale::IsAvailable(int lang)
     if ( !info->WinLang )
         return false;
 
-    if ( !::IsValidLocale
-            (
-                MAKELCID(MAKELANGID(info->WinLang, info->WinSublang),
-                         SORT_DEFAULT),
-                LCID_INSTALLED
-            ) )
+    if ( !::IsValidLocale(info->GetLCID(), LCID_INSTALLED) )
         return false;
 
 #elif defined(__UNIX__)
@@ -2595,11 +2599,8 @@ wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory WXUNUSED(cat))
     if (wxGetLocale())
     {
         const wxLanguageInfo *info = GetLanguageInfo(wxGetLocale()->GetLanguage());
-        if (info)
-        {                         ;
-            lcid = MAKELCID(MAKELANGID(info->WinLang, info->WinSublang),
-                                     SORT_DEFAULT);
-        }
+        if ( info )
+            lcid = info->GetLCID();
     }
 
     wxString str;
@@ -2642,7 +2643,22 @@ wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory WXUNUSED(cat))
 /* static */
 wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory WXUNUSED(cat))
 {
-    wxCFRef<CFLocaleRef> userLocaleRef(CFLocaleCopyCurrent());
+    CFLocaleRef userLocaleRefRaw;
+    if ( wxGetLocale() )
+    {
+        userLocaleRefRaw = CFLocaleCreate
+                           (
+                                kCFAllocatorDefault,
+                                wxCFStringRef(wxGetLocale()->GetCanonicalName())
+                           );
+    }
+    else // no current locale, use the default one
+    {
+        userLocaleRefRaw = CFLocaleCopyCurrent();
+    }
+
+    wxCFRef<CFLocaleRef> userLocaleRef(userLocaleRefRaw);
+
     CFTypeRef cfstr;
     switch ( index )
     {