]> git.saurik.com Git - wxWidgets.git/commitdiff
add wxLanguageInfo::GetLocaleName(), this simplifies the current code and will be...
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 11 Feb 2008 23:40:03 +0000 (23:40 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 11 Feb 2008 23:40:03 +0000 (23:40 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@51669 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/intl.h
src/common/intl.cpp

index 57a3ec063827ce4626d719f239ccfcf3bbaf0632..8b02196d99d945861f43ff76f335dda1526e13fc 100644 (file)
@@ -332,8 +332,18 @@ struct WXDLLIMPEXP_BASE wxLanguageInfo
     // return the LCID corresponding to this language
     wxUint32 GetLCID() const;
 #endif // __WXMSW__
+
+    // return the locale name corresponding to this language usable with
+    // setlocale() on the current system
+    wxString GetLocaleName() const;
 };
 
+// for Unix systems GetLocaleName() is trivial so implement it inline here, for
+// MSW it's implemented in intl.cpp
+#ifndef __WXMSW__
+inline wxString wxLanguageInfo::GetLocaleName() const { return CanonicalName; }
+#endif // !__WXMSW__
+
 // ----------------------------------------------------------------------------
 // wxLocaleCategory: the category of locale settings
 // ----------------------------------------------------------------------------
index 9294c25045507282a306c81dcb0ec35995bf3d29..2a4f5e16e704ad666f9cfac68a8e78d582116793 100644 (file)
@@ -1023,6 +1023,38 @@ 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;
+    }
+
+    if ( ::GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE,
+                         buffer, WXSIZEOF(buffer)) > 0 )
+    {
+        if ( buffer[0] != _T('0') || buffer[1] != _T('\0') )
+            locale << _T('.') << buffer;
+        //else: this locale doesn't use ANSI code page
+    }
+
+    return locale;
+}
+
 #endif // __WXMSW__
 
 // ----------------------------------------------------------------------------
@@ -1806,7 +1838,7 @@ bool wxLocale::Init(int language, int flags)
         //     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.
+        //     #ifdef SETLOCALE_FAILS_ON_UNICODE_LANGS below.
         #define SETLOCALE_FAILS_ON_UNICODE_LANGS
     #endif
 
@@ -1820,49 +1852,38 @@ bool wxLocale::Init(int language, int flags)
         }
         else
         {
-            int codepage
-                         #ifdef SETLOCALE_FAILS_ON_UNICODE_LANGS
-                         = -1
-                         #endif
-                         ;
             const wxUint32 lcid = info->GetLCID();
 
             // FIXME
 #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
+                // FIXME
 #ifndef __WXWINCE__
                 retloc = wxSetlocale(LC_ALL, locale);
 #endif
 #ifdef SETLOCALE_FAILS_ON_UNICODE_LANGS
-                if (codepage == 0 && retloc == NULL)
+                if ( !retloc )
                 {
-                    retloc = "C";
+                    // GetLocaleName() returns a string without period only if
+                    // there is no associated ANSI code page
+                    if ( locale.find(_T('.')) == wxString::npos )
+                    {
+                        retloc = "C";
+                    }
+                    //else: locale has code page information and hence this is
+                    //      a real error
                 }
-#endif
+#endif // SETLOCALE_FAILS_ON_UNICODE_LANGS
             }
         }
     }