X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2ec6905c69c73f908533697c770c690657f4371c..7d1f4c3ff01b09105c07d5e176a14428c834975e:/src/common/intl.cpp diff --git a/src/common/intl.cpp b/src/common/intl.cpp index 3b0a992d89..d84627fc7a 100644 --- a/src/common/intl.cpp +++ b/src/common/intl.cpp @@ -51,6 +51,8 @@ #include "wx/file.h" #include "wx/tokenzr.h" #include "wx/module.h" +#include "wx/fontmap.h" +#include "wx/encconv.h" #ifdef __WIN32__ #include "wx/msw/private.h" @@ -58,6 +60,10 @@ #include "wx/fontmap.h" // for CharsetToEncoding() #endif +#if defined(__WXMAC__) + #include "wx/mac/private.h" // includes mac headers +#endif + // ---------------------------------------------------------------------------- // simple types // ---------------------------------------------------------------------------- @@ -82,22 +88,9 @@ typedef unsigned char size_t8; typedef unsigned long size_t32; #else // assume sizeof(int) == 4 - what else can we do - typedef unsigned int size_t32; - - // ... but at least check it during run time - static class IntSizeChecker - { - public: - IntSizeChecker() - { - // Asserting a sizeof directly causes some compilers to - // issue a "using constant in a conditional expression" warning - size_t intsize = sizeof(int); + wxCOMPILE_TIME_ASSERT( sizeof(int) == 4, IntMustBeExactly4Bytes); - wxASSERT_MSG( intsize == 4, - "size_t32 is incorrectly defined!" ); - } - } intsizechecker; + typedef unsigned int size_t32; #endif #endif // Win/!Win @@ -341,6 +334,12 @@ static wxString GetFullSearchPath(const wxChar *lang) // then take the current directory // FIXME it should be the directory of the executable +#ifdef __WXMAC__ + wxChar cwd[512] ; + wxGetWorkingDirectory( cwd , sizeof( cwd ) ) ; + searchPath << GetAllMsgCatalogSubdirs(cwd, lang); + // generic search paths could be somewhere in the system folder preferences +#else searchPath << GetAllMsgCatalogSubdirs(wxT("."), lang); // and finally add some standard ones @@ -348,7 +347,7 @@ static wxString GetFullSearchPath(const wxChar *lang) << GetAllMsgCatalogSubdirs(wxT("/usr/share/locale"), lang) << GetAllMsgCatalogSubdirs(wxT("/usr/lib/locale"), lang) << GetAllMsgCatalogSubdirs(wxT("/usr/local/share/locale"), lang); - +#endif return searchPath; } @@ -506,17 +505,8 @@ const char *wxMsgCatalog::GetString(const char *szOrig) const return NULL; } - -#if wxUSE_GUI -#include "wx/fontmap.h" -#include "wx/encconv.h" -#endif - void wxMsgCatalog::ConvertEncoding() { -#if wxUSE_GUI - wxFontEncoding enc; - // first, find encoding header: const char *hdr = StringAtOfs(m_pOrigTable, 0); if ( hdr == NULL || hdr[0] != 0 ) { @@ -533,23 +523,12 @@ void wxMsgCatalog::ConvertEncoding() while (header[n] != wxT('\n')) charset << header[n++]; - enc = wxTheFontMapper->CharsetToEncoding(charset, FALSE); +#if wxUSE_FONTMAP + wxFontEncoding enc = wxTheFontMapper->CharsetToEncoding(charset, FALSE); if ( enc == wxFONTENCODING_SYSTEM ) return; // unknown encoding - wxFontEncoding targetEnc = wxFONTENCODING_SYSTEM; -#ifdef __UNIX__ - wxString langFull; - if (wxGetEnv(wxT("LC_ALL"), &langFull) || - wxGetEnv(wxT("LC_CTYPE"), &langFull) || - wxGetEnv(wxT("LANG"), &langFull)) - { - wxString lcharset = langFull.AfterFirst(wxT('.')).BeforeFirst(wxT('@')); - if (!lcharset.IsEmpty()) - targetEnc = wxTheFontMapper->CharsetToEncoding(lcharset, FALSE); - } -#endif - + wxFontEncoding targetEnc = wxLocale::GetSystemEncoding(); if (targetEnc == wxFONTENCODING_SYSTEM) { wxFontEncodingArray a = wxEncodingConverter::GetPlatformEquivalents(enc); @@ -565,7 +544,7 @@ void wxMsgCatalog::ConvertEncoding() for (size_t i = 0; i < m_numStrings; i++) converter.Convert((char*)StringAtOfs(m_pTransTable, i)); -#endif // wxUSE_GUI +#endif // wxUSE_FONTMAP } @@ -629,7 +608,12 @@ bool wxLocale::Init(const wxChar *szName, if ( m_strShort.IsEmpty() ) { // FIXME I don't know how these 2 letter abbreviations are formed, // this wild guess is surely wrong - m_strShort = tolower(szLocale[0]) + tolower(szLocale[1]); + if ( szLocale[0] ) + { + m_strShort += (wxChar)wxTolower(szLocale[0]); + if ( szLocale[1] ) + m_strShort += (wxChar)wxTolower(szLocale[1]); + } } // save the old locale to be able to restore it later @@ -685,7 +669,7 @@ bool wxLocale::Init(int language, int flags) wxString name = info->Description; wxString canonical = info->CanonicalName; wxString locale; - wxChar *retloc; + const wxChar *retloc; // Set the locale: #ifdef __UNIX__ @@ -769,7 +753,8 @@ bool wxLocale::Init(int language, int flags) wxLogError(wxT("Cannot set locale to language %s."), name.c_str()); return FALSE; } - +#elif defined(__WXMAC__) + retloc = wxSetlocale(LC_ALL , wxEmptyString); #else return FALSE; #endif @@ -809,7 +794,7 @@ void wxLocale::AddCatalogLookupPathPrefix(const wxString& prefix) return wxLANGUAGE_ENGLISH; } - if ( langFull == _T("C") ) + if ( langFull == _T("C") || langFull == _T("POSIX") ) { // default C locale return wxLANGUAGE_ENGLISH; @@ -817,18 +802,31 @@ void wxLocale::AddCatalogLookupPathPrefix(const wxString& prefix) // the language string has the following form // - // lang[_LANG[.encoding]] + // lang[_LANG][.encoding][@modifier] + // + // (see environ(5) in the Open Unix specification) // - // where lang is the primary language, LANG is a sublang + // where lang is the primary language, LANG is a sublang/territory, + // encoding is the charset to use and modifier "allows the user to select + // a specific instance of localization data within a single category" // // for example, the following strings are valid: // fr // fr_FR // de_DE.iso88591 + // de_DE@euro + // de_DE.iso88591@euro // for now we don't use the encoding, although we probably should (doing // translations of the msg catalogs on the fly as required) (TODO) - langFull = langFull.BeforeFirst(_T('.')); + // + // we don't use the modifiers neither but we probably should translate + // "euro" into iso885915 + size_t posEndLang = langFull.find_first_of(_T("@.")); + if ( posEndLang != wxString::npos ) + { + langFull.Truncate(posEndLang); + } // in addition to the format above, we also can have full language names // in LANG env var - for example, SuSE is known to use LANG="german" - so @@ -907,6 +905,292 @@ void wxLocale::AddCatalogLookupPathPrefix(const wxString& prefix) } } } +#elif defined(__WXMAC__) + char* lc = NULL ; + long lang = GetScriptVariable( smSystemScript, smScriptLang) ; + switch( GetScriptManagerVariable( smRegionCode ) ) { + case verUS : + lc = "en_US" ; + break ; + case verFrance : + lc = "fr_FR" ; + break ; + case verBritain : + lc = "en_GB" ; + break ; + case verGermany : + lc = "de_DE" ; + break ; + case verItaly : + lc = "it_IT" ; + break ; + case verNetherlands : + lc = "nl_NL" ; + break ; + case verFlemish : + lc = "nl_BE" ; + break ; + case verSweden : + lc = "sv_SE" ; + break ; + case verSpain : + lc = "es_ES" ; + break ; + case verDenmark : + lc = "da_DK" ; + break ; + case verPortugal : + lc = "pt_PT" ; + break ; + case verFrCanada: + lc = "fr_CA" ; + break ; + case verNorway: + lc = "no_NO" ; + break ; + case verIsrael: + lc = "iw_IL" ; + break ; + case verJapan: + lc = "ja_JP" ; + break ; + case verAustralia: + lc = "en_AU" ; + break ; + case verArabic: + lc = "ar" ; + break ; + case verFinland: + lc = "fi_FI" ; + break ; + case verFrSwiss: + lc = "fr_CH" ; + break ; + case verGrSwiss: + lc = "de_CH" ; + break ; + case verGreece: + lc = "el_GR" ; + break ; + case verIceland: + lc = "is_IS" ; + break ; + case verMalta: + lc = "mt_MT" ; + break ; + case verCyprus: + // _CY is not part of wx, so we have to translate according to the system language + if ( lang == langGreek ) { + lc = "el_GR" ; + } + else if ( lang == langTurkish ) { + lc = "tr_TR" ; + } + break ; + case verTurkey: + lc = "tr_TR" ; + break ; + case verYugoCroatian: + lc = "hr_HR" ; + break ; + case verIndiaHindi: + lc = "hi_IN" ; + break ; + case verPakistanUrdu: + lc = "ur_PK" ; + break ; + case verTurkishModified: + lc = "tr_TR" ; + break ; + case verItalianSwiss: + lc = "it_CH" ; + break ; + case verInternational: + lc = "en" ; + break ; + case verRomania: + lc = "ro_RO" ; + break ; + case verGreecePoly: + lc = "el_GR" ; + break ; + case verLithuania: + lc = "lt_LT" ; + break ; + case verPoland: + lc = "pl_PL" ; + break ; + case verMagyar : + case verHungary: + lc = "hu_HU" ; + break ; + case verEstonia: + lc = "et_EE" ; + break ; + case verLatvia: + lc = "lv_LV" ; + break ; + case verSami: + // not known + break ; + case verFaroeIsl: + lc = "fo_FO" ; + break ; + case verIran: + lc = "fa_IR" ; + break ; + case verRussia: + lc = "ru_RU" ; + break ; + case verIreland: + lc = "ga_IE" ; + break ; + case verKorea: + lc = "ko_KR" ; + break ; + case verChina: + lc = "zh_CN" ; + break ; + case verTaiwan: + lc = "zh_TW" ; + break ; + case verThailand: + lc = "th_TH" ; + break ; + case verCzech: + lc = "cs_CZ" ; + break ; + case verSlovak: + lc = "sk_SK" ; + break ; + case verBengali: + lc = "bn" ; + break ; + case verByeloRussian: + lc = "be_BY" ; + break ; + case verUkraine: + lc = "uk_UA" ; + break ; + case verGreeceAlt: + lc = "el_GR" ; + break ; + case verSerbian: + lc = "sr_YU" ; + break ; + case verSlovenian: + lc = "sl_SI" ; + break ; + case verMacedonian: + lc = "mk_MK" ; + break ; + case verCroatia: + lc = "hr_HR" ; + break ; + case verBrazil: + lc = "pt_BR " ; + break ; + case verBulgaria: + lc = "bg_BG" ; + break ; + case verCatalonia: + lc = "ca_ES" ; + break ; + case verScottishGaelic: + lc = "gd" ; + break ; + case verManxGaelic: + lc = "gv" ; + break ; + case verBreton: + lc = "br" ; + break ; + case verNunavut: + lc = "iu_CA" ; + break ; + case verWelsh: + lc = "cy" ; + break ; + case verIrishGaelicScript: + lc = "ga_IE" ; + break ; + case verEngCanada: + lc = "en_CA" ; + break ; + case verBhutan: + lc = "dz_BT" ; + break ; + case verArmenian: + lc = "hy_AM" ; + break ; + case verGeorgian: + lc = "ka_GE" ; + break ; + case verSpLatinAmerica: + lc = "es_AR" ; + break ; + case verTonga: + lc = "to_TO" ; + break ; + case verFrenchUniversal: + lc = "fr_FR" ; + break ; + case verAustria: + lc = "de_AT" ; + break ; + case verGujarati: + lc = "gu_IN" ; + break ; + case verPunjabi: + lc = "pa" ; + break ; + case verIndiaUrdu: + lc = "ur_IN" ; + break ; + case verVietnam: + lc = "vi_VN" ; + break ; + case verFrBelgium: + lc = "fr_BE" ; + break ; + case verUzbek: + lc = "uz_UZ" ; + break ; + case verSingapore: + lc = "zh_SG" ; + break ; + case verNynorsk: + lc = "nn_NO" ; + break ; + case verAfrikaans: + lc = "af_ZA" ; + break ; + case verEsperanto: + lc = "eo" ; + break ; + case verMarathi: + lc = "mr_IN" ; + break ; + case verTibetan: + lc = "bo" ; + break ; + case verNepal: + lc = "ne_NP" ; + break ; + case verGreenland: + lc = "kl_GL" ; + break ; + default : + break ; + } + for ( i = 0; i < count; i++ ) + { + if ( ms_languagesDB->Item(i).CanonicalName == lc ) + { + break; + } + } + #elif defined(__WIN32__) LCID lcid = GetUserDefaultLCID(); if ( lcid != 0 ) @@ -949,10 +1233,10 @@ wxString wxLocale::GetSystemEncodingName() { wxString encname; -#ifdef __WIN32__ +#if defined(__WIN32__) && !defined(__WXMICROWIN__) // FIXME: what is the error return value for GetACP()? UINT codepage = ::GetACP(); - encname.Printf(_T("cp%u"), codepage); + encname.Printf(_T("windows-%u"), codepage); #elif defined(__UNIX_LIKE__) #if defined(HAVE_LANGINFO_H) && defined(CODESET) @@ -962,9 +1246,22 @@ wxString wxLocale::GetSystemEncodingName() setlocale(LC_CTYPE, ""); char *alang = nl_langinfo(CODESET); setlocale(LC_CTYPE, oldLocale); + free(oldLocale); if (alang) { - encname = wxConvLibc.cMB2WX(alang); +#ifdef __SOLARIS__ + // nl_langinfo() under Solaris returns 646 by default which stands for + // ISO-646, i.e. 7 bit ASCII and we should recognize it to avoid + // warnings about unrecognized encoding on each program startup + if ( strcmp(alang, "646") == 0 ) + { + encname = _T("US-ASCII"); + } + else +#endif // __SOLARIS__ + { + encname = wxConvLibc.cMB2WX(alang); + } } else #endif // HAVE_LANGINFO_H @@ -1000,20 +1297,52 @@ wxString wxLocale::GetSystemEncodingName() /* static */ wxFontEncoding wxLocale::GetSystemEncoding() { -#ifdef __WIN32__ +#if defined(__WIN32__) && !defined(__WXMICROWIN__) UINT codepage = ::GetACP(); - // wxWindows only knows about CP1250-1257 + // wxWindows only knows about CP1250-1257, 932, 936, 949, 950 if ( codepage >= 1250 && codepage <= 1257 ) { return (wxFontEncoding)(wxFONTENCODING_CP1250 + codepage - 1250); } -#elif defined(__UNIX_LIKE__) + + 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(__UNIX_LIKE__) && wxUSE_FONTMAP wxString encname = GetSystemEncodingName(); if ( !encname.empty() ) { - return wxTheFontMapper-> + wxFontEncoding enc = wxTheFontMapper-> CharsetToEncoding(encname, FALSE /* not interactive */); + + // this should probably be considered as a bug in CharsetToEncoding(): + // it shouldn't return wxFONTENCODING_DEFAULT at all - but it does it + // for US-ASCII charset + // + // we, OTOH, definitely shouldn't return it as it doesn't make sense at + // all (which encoding is it?) + if ( enc != wxFONTENCODING_DEFAULT ) + { + return enc; + } + //else: return wxFONTENCODING_SYSTEM below } #endif // Win32/Unix @@ -1052,7 +1381,7 @@ const wxMB2WXbuf wxLocale::GetString(const wxChar *szOrigString, const wxChar *szDomain) const { if ( wxIsEmpty(szOrigString) ) - return szDomain; + return _T(""); const char *pszTrans = NULL; #if wxUSE_UNICODE @@ -1098,12 +1427,13 @@ const wxMB2WXbuf wxLocale::GetString(const wxChar *szOrigString, return (wxMB2WXbuf)(szOrigString); } - else - { - return wxConvertMB2WX(pszTrans); // or preferably wxCSConv(charset).cMB2WX(pszTrans) or something, - // a macro similar to wxConvertMB2WX could be written for that - } + // or preferably wxCSConv(charset).cMB2WX(pszTrans) or something, a macro + // similar to wxConvertMB2WX could be written for that + + return wxConvertMB2WX(pszTrans); + + // undo the hack from the beginning of this function #undef szOrgString } @@ -1147,6 +1477,62 @@ bool wxLocale::AddCatalog(const wxChar *szDomain) } } +// ---------------------------------------------------------------------------- +// accessors for locale-dependent data +// ---------------------------------------------------------------------------- + +#if 0 + +#ifdef __WXMSW__ + +/* static */ +wxString wxLocale::GetInfo(wxLocaleInfo index) +{ + wxString str; + wxChar buffer[256]; + size_t count; + buffer[0] = wxT('\0'); + switch (index) + { + case wxSYS_DECIMAL_SEPARATOR: + count = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, buffer, 256); + if (!count) + str << "."; + else + str << buffer; + break; + case wxSYS_LIST_SEPARATOR: + count = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, buffer, 256); + if (!count) + str << ","; + else + str << buffer; + break; + case wxSYS_LEADING_ZERO: // 0 means no leading zero, 1 means leading zero + count = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ILZERO, buffer, 256); + if (!count) + str << "0"; + else + str << buffer; + break; + default: + wxFAIL_MSG("Unknown System String !"); + } + return str; +} + +#else // !__WXMSW__ + +/* static */ +wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory) +{ + return wxEmptyString; +} + +#endif // __WXMSW__/!__WXMSW__ + +#endif // 0 + // ---------------------------------------------------------------------------- // global functions and variables // ---------------------------------------------------------------------------- @@ -1199,7 +1585,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxLocaleModule, wxModule) // This table is generated by misc/languages/genlang.py // When making changes, please put them into misc/languages/langtabl.txt -#ifndef __WIN32__ +#if !defined(__WIN32__) || defined(__WXMICROWIN__) #define SETWINLANG(info,lang,sublang) @@ -1935,7 +2321,5 @@ void wxLocale::InitLanguagesDB() // --- --- --- generated code ends here --- --- --- - - #endif // wxUSE_INTL