]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
dccce9ea | 2 | // Name: src/common/intl.cpp |
77ffb593 | 3 | // Purpose: Internationalization and localisation for wxWidgets |
c801d85f | 4 | // Author: Vadim Zeitlin |
849a28d0 VS |
5 | // Modified by: Michael N. Filippov <michael@idisys.iae.nsk.su> |
6 | // (2003/09/30 - PluralForms support) | |
c801d85f | 7 | // Created: 29/01/98 |
c801d85f | 8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
65571936 | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
1678ad78 | 13 | // declaration |
c801d85f KB |
14 | // ============================================================================ |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
c801d85f KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
84c18814 | 24 | #pragma hdrstop |
c801d85f KB |
25 | #endif |
26 | ||
6908078e VZ |
27 | #ifdef __EMX__ |
28 | // The following define is needed by Innotek's libc to | |
29 | // make the definition of struct localeconv available. | |
30 | #define __INTERNAL_DEFS | |
31 | #endif | |
32 | ||
d427503c VZ |
33 | #if wxUSE_INTL |
34 | ||
ad9835c9 WS |
35 | #ifndef WX_PRECOMP |
36 | #include "wx/dynarray.h" | |
37 | #include "wx/string.h" | |
38 | #include "wx/intl.h" | |
39 | #include "wx/log.h" | |
ad9835c9 | 40 | #include "wx/utils.h" |
670f9935 | 41 | #include "wx/app.h" |
df69528b | 42 | #include "wx/hashmap.h" |
02761f6c | 43 | #include "wx/module.h" |
ad9835c9 | 44 | #endif // WX_PRECOMP |
1c193821 JS |
45 | |
46 | #ifndef __WXWINCE__ | |
ad9835c9 | 47 | #include <locale.h> |
1c193821 JS |
48 | #endif |
49 | ||
ad9835c9 | 50 | // standard headers |
dccce9ea VZ |
51 | #include <ctype.h> |
52 | #include <stdlib.h> | |
2ec6905c | 53 | #ifdef HAVE_LANGINFO_H |
670f9935 | 54 | #include <langinfo.h> |
2ec6905c | 55 | #endif |
7502ba29 | 56 | |
1c193821 JS |
57 | #ifdef __WIN32__ |
58 | #include "wx/msw/private.h" | |
1c193821 JS |
59 | #endif |
60 | ||
c801d85f | 61 | #include "wx/file.h" |
58ad1bab | 62 | #include "wx/filename.h" |
dccce9ea | 63 | #include "wx/tokenzr.h" |
030c0bea | 64 | #include "wx/fontmap.h" |
664e1314 | 65 | #include "wx/scopedptr.h" |
4d931bcc | 66 | #include "wx/apptrait.h" |
f830b2b1 | 67 | #include "wx/stdpaths.h" |
a64be16e | 68 | #include "wx/hashset.h" |
dccce9ea | 69 | |
89a7e1ff | 70 | #if defined(__WXOSX__) |
c933e267 | 71 | #include "wx/osx/core/cfref.h" |
529e125d | 72 | #include <CoreFoundation/CFLocale.h> |
e9e53eb1 | 73 | #include <CoreFoundation/CFDateFormatter.h> |
c933e267 | 74 | #include "wx/osx/core/cfstring.h" |
5a9cd2d1 | 75 | #endif |
dff6cf57 | 76 | |
c801d85f KB |
77 | // ---------------------------------------------------------------------------- |
78 | // constants | |
79 | // ---------------------------------------------------------------------------- | |
80 | ||
a0516656 | 81 | #define TRACE_I18N wxS("i18n") |
ca6a9a14 | 82 | |
ea144923 VS |
83 | // ============================================================================ |
84 | // implementation | |
85 | // ============================================================================ | |
563d535e | 86 | |
849a28d0 | 87 | // ---------------------------------------------------------------------------- |
ea144923 | 88 | // global functions |
18e065b4 VS |
89 | // ---------------------------------------------------------------------------- |
90 | ||
ea144923 VS |
91 | static wxLocale *wxSetLocale(wxLocale *pLocale); |
92 | ||
18e065b4 VS |
93 | namespace |
94 | { | |
95 | ||
ce411ca8 | 96 | // get just the language part ("en" in "en_GB") |
ea144923 | 97 | inline wxString ExtractLang(const wxString& langFull) |
18e065b4 | 98 | { |
ce411ca8 | 99 | return langFull.BeforeFirst('_'); |
18e065b4 VS |
100 | } |
101 | ||
ea144923 VS |
102 | // helper functions of GetSystemLanguage() |
103 | #ifdef __UNIX__ | |
18e065b4 | 104 | |
ea144923 VS |
105 | // get everything else (including the leading '_') |
106 | inline wxString ExtractNotLang(const wxString& langFull) | |
107 | { | |
ce411ca8 VS |
108 | size_t pos = langFull.find('_'); |
109 | if ( pos != wxString::npos ) | |
110 | return langFull.substr(pos); | |
111 | else | |
112 | return wxString(); | |
ea144923 | 113 | } |
18e065b4 | 114 | |
ea144923 | 115 | #endif // __UNIX__ |
18e065b4 | 116 | |
ea144923 | 117 | } // anonymous namespace |
18e065b4 | 118 | |
ea144923 VS |
119 | // ---------------------------------------------------------------------------- |
120 | // wxLanguageInfo | |
121 | // ---------------------------------------------------------------------------- | |
18e065b4 | 122 | |
d98a58c5 | 123 | #ifdef __WINDOWS__ |
18e065b4 | 124 | |
ea144923 VS |
125 | // helper used by wxLanguageInfo::GetLocaleName() and elsewhere to determine |
126 | // whether the locale is Unicode-only (it is if this function returns empty | |
127 | // string) | |
128 | static wxString wxGetANSICodePageForLocale(LCID lcid) | |
129 | { | |
130 | wxString cp; | |
18e065b4 | 131 | |
ea144923 VS |
132 | wxChar buffer[16]; |
133 | if ( ::GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE, | |
134 | buffer, WXSIZEOF(buffer)) > 0 ) | |
18e065b4 | 135 | { |
ea144923 VS |
136 | if ( buffer[0] != wxT('0') || buffer[1] != wxT('\0') ) |
137 | cp = buffer; | |
138 | //else: this locale doesn't use ANSI code page | |
18e065b4 VS |
139 | } |
140 | ||
ea144923 | 141 | return cp; |
18e065b4 VS |
142 | } |
143 | ||
ea144923 | 144 | wxUint32 wxLanguageInfo::GetLCID() const |
18e065b4 | 145 | { |
ea144923 | 146 | return MAKELCID(MAKELANGID(WinLang, WinSublang), SORT_DEFAULT); |
18e065b4 VS |
147 | } |
148 | ||
ea144923 | 149 | wxString wxLanguageInfo::GetLocaleName() const |
18e065b4 | 150 | { |
ea144923 | 151 | wxString locale; |
18e065b4 | 152 | |
ea144923 | 153 | const LCID lcid = GetLCID(); |
18e065b4 | 154 | |
ea144923 VS |
155 | wxChar buffer[256]; |
156 | buffer[0] = wxT('\0'); | |
157 | if ( !::GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE, buffer, WXSIZEOF(buffer)) ) | |
849a28d0 | 158 | { |
ea144923 VS |
159 | wxLogLastError(wxT("GetLocaleInfo(LOCALE_SENGLANGUAGE)")); |
160 | return locale; | |
849a28d0 | 161 | } |
18e065b4 | 162 | |
ea144923 VS |
163 | locale << buffer; |
164 | if ( ::GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY, | |
165 | buffer, WXSIZEOF(buffer)) > 0 ) | |
849a28d0 | 166 | { |
ea144923 | 167 | locale << wxT('_') << buffer; |
849a28d0 VS |
168 | } |
169 | ||
ea144923 VS |
170 | const wxString cp = wxGetANSICodePageForLocale(lcid); |
171 | if ( !cp.empty() ) | |
563d535e | 172 | { |
ea144923 | 173 | locale << wxT('.') << cp; |
563d535e | 174 | } |
18e065b4 | 175 | |
ea144923 | 176 | return locale; |
563d535e | 177 | } |
afc94fa6 | 178 | |
d98a58c5 | 179 | #endif // __WINDOWS__ |
18e065b4 | 180 | |
c801d85f KB |
181 | // ---------------------------------------------------------------------------- |
182 | // wxLocale | |
183 | // ---------------------------------------------------------------------------- | |
184 | ||
d3f3e35f VS |
185 | #include "wx/arrimpl.cpp" |
186 | WX_DECLARE_EXPORTED_OBJARRAY(wxLanguageInfo, wxLanguageInfoArray); | |
4115960d | 187 | WX_DEFINE_OBJARRAY(wxLanguageInfoArray) |
d3f3e35f | 188 | |
41780009 VS |
189 | wxLanguageInfoArray *wxLocale::ms_languagesDB = NULL; |
190 | ||
191 | /*static*/ void wxLocale::CreateLanguagesDB() | |
192 | { | |
193 | if (ms_languagesDB == NULL) | |
194 | { | |
195 | ms_languagesDB = new wxLanguageInfoArray; | |
196 | InitLanguagesDB(); | |
197 | } | |
198 | } | |
199 | ||
200 | /*static*/ void wxLocale::DestroyLanguagesDB() | |
201 | { | |
5276b0a5 | 202 | wxDELETE(ms_languagesDB); |
41780009 VS |
203 | } |
204 | ||
205 | ||
ea8f6fc7 | 206 | void wxLocale::DoCommonInit() |
c801d85f | 207 | { |
700256bb VZ |
208 | // Store the current locale in order to be able to restore it in the dtor. |
209 | m_pszOldLocale = wxSetlocale(LC_ALL, NULL); | |
210 | if ( m_pszOldLocale ) | |
211 | m_pszOldLocale = wxStrdup(m_pszOldLocale); | |
212 | ||
eb48778d | 213 | |
ca642c8a | 214 | m_pOldLocale = wxSetLocale(this); |
0d28a1f9 VS |
215 | |
216 | // Set translations object, but only if the user didn't do so yet. | |
217 | // This is to preserve compatibility with wx-2.8 where wxLocale was | |
218 | // the only API for translations. wxLocale works as a stack, with | |
219 | // latest-created one being the active one: | |
220 | // wxLocale loc_fr(wxLANGUAGE_FRENCH); | |
221 | // // _() returns French | |
222 | // { | |
223 | // wxLocale loc_cs(wxLANGUAGE_CZECH); | |
224 | // // _() returns Czech | |
225 | // } | |
226 | // // _() returns French again | |
227 | wxTranslations *oldTrans = wxTranslations::Get(); | |
228 | if ( !oldTrans || | |
229 | (m_pOldLocale && oldTrans == &m_pOldLocale->m_translations) ) | |
230 | { | |
231 | wxTranslations::SetNonOwned(&m_translations); | |
232 | } | |
eb48778d | 233 | |
ca642c8a FM |
234 | m_language = wxLANGUAGE_UNKNOWN; |
235 | m_initialized = false; | |
23fcecf7 VZ |
236 | } |
237 | ||
238 | // NB: this function has (desired) side effect of changing current locale | |
31b7522e VS |
239 | bool wxLocale::Init(const wxString& name, |
240 | const wxString& shortName, | |
241 | const wxString& locale, | |
3acf8a8d VS |
242 | bool bLoadDefault |
243 | #if WXWIN_COMPATIBILITY_2_8 | |
f675b4f5 | 244 | ,bool WXUNUSED_UNLESS_DEBUG(bConvertEncoding) |
3acf8a8d VS |
245 | #endif |
246 | ) | |
23fcecf7 | 247 | { |
3acf8a8d VS |
248 | #if WXWIN_COMPATIBILITY_2_8 |
249 | wxASSERT_MSG( bConvertEncoding, | |
250 | wxS("wxLocale::Init with bConvertEncoding=false is no longer supported, add charset to your catalogs") ); | |
251 | #endif | |
252 | ||
18e065b4 VS |
253 | bool ret = DoInit(name, shortName, locale); |
254 | ||
255 | // NB: don't use 'lang' here, 'language' may be wxLANGUAGE_DEFAULT | |
0d28a1f9 VS |
256 | wxTranslations *t = wxTranslations::Get(); |
257 | if ( t ) | |
258 | { | |
259 | t->SetLanguage(shortName); | |
18e065b4 | 260 | |
0d28a1f9 VS |
261 | if ( bLoadDefault ) |
262 | t->AddStdCatalog(); | |
263 | } | |
18e065b4 VS |
264 | |
265 | return ret; | |
266 | } | |
267 | ||
268 | bool wxLocale::DoInit(const wxString& name, | |
269 | const wxString& shortName, | |
270 | const wxString& locale) | |
271 | { | |
272 | wxASSERT_MSG( !m_initialized, | |
273 | wxS("you can't call wxLocale::Init more than once") ); | |
274 | ||
ca642c8a FM |
275 | m_initialized = true; |
276 | m_strLocale = name; | |
277 | m_strShort = shortName; | |
ca642c8a | 278 | m_language = wxLANGUAGE_UNKNOWN; |
23fcecf7 | 279 | |
ca642c8a FM |
280 | // change current locale (default: same as long name) |
281 | wxString szLocale(locale); | |
282 | if ( szLocale.empty() ) | |
283 | { | |
284 | // the argument to setlocale() | |
285 | szLocale = shortName; | |
52558258 | 286 | |
ca642c8a FM |
287 | wxCHECK_MSG( !szLocale.empty(), false, |
288 | wxS("no locale to set in wxLocale::Init()") ); | |
289 | } | |
10545ca4 | 290 | |
700256bb | 291 | if ( !wxSetlocale(LC_ALL, szLocale) ) |
af588446 | 292 | { |
4c51a665 | 293 | wxLogError(_("locale '%s' cannot be set."), szLocale); |
af588446 | 294 | } |
c801d85f | 295 | |
ca642c8a FM |
296 | // the short name will be used to look for catalog files as well, |
297 | // so we need something here | |
298 | if ( m_strShort.empty() ) { | |
299 | // FIXME I don't know how these 2 letter abbreviations are formed, | |
300 | // this wild guess is surely wrong | |
301 | if ( !szLocale.empty() ) | |
302 | { | |
303 | m_strShort += (wxChar)wxTolower(szLocale[0]); | |
304 | if ( szLocale.length() > 1 ) | |
305 | m_strShort += (wxChar)wxTolower(szLocale[1]); | |
306 | } | |
70426fba | 307 | } |
7af89395 | 308 | |
18e065b4 | 309 | return true; |
c801d85f KB |
310 | } |
311 | ||
41524ffc | 312 | |
1cc549e5 | 313 | #if defined(__UNIX__) && wxUSE_UNICODE && !defined(__WXMAC__) |
52de37c7 | 314 | static const char *wxSetlocaleTryUTF8(int c, const wxString& lc) |
41524ffc | 315 | { |
aff909dd | 316 | const char *l = NULL; |
cb352236 VS |
317 | |
318 | // NB: We prefer to set UTF-8 locale if it's possible and only fall back to | |
319 | // non-UTF-8 locale if it fails | |
320 | ||
52de37c7 | 321 | if ( !lc.empty() ) |
41524ffc | 322 | { |
7beb59f3 | 323 | wxString buf(lc); |
2d34a303 | 324 | wxString buf2; |
a0516656 | 325 | buf2 = buf + wxS(".UTF-8"); |
52de37c7 | 326 | l = wxSetlocale(c, buf2); |
2d34a303 VS |
327 | if ( !l ) |
328 | { | |
a0516656 | 329 | buf2 = buf + wxS(".utf-8"); |
52de37c7 | 330 | l = wxSetlocale(c, buf2); |
2d34a303 VS |
331 | } |
332 | if ( !l ) | |
333 | { | |
a0516656 | 334 | buf2 = buf + wxS(".UTF8"); |
52de37c7 | 335 | l = wxSetlocale(c, buf2); |
2d34a303 VS |
336 | } |
337 | if ( !l ) | |
338 | { | |
a0516656 | 339 | buf2 = buf + wxS(".utf8"); |
52de37c7 | 340 | l = wxSetlocale(c, buf2); |
2d34a303 | 341 | } |
41524ffc | 342 | } |
cb352236 VS |
343 | |
344 | // if we can't set UTF-8 locale, try non-UTF-8 one: | |
345 | if ( !l ) | |
346 | l = wxSetlocale(c, lc); | |
347 | ||
41524ffc VS |
348 | return l; |
349 | } | |
350 | #else | |
cb352236 | 351 | #define wxSetlocaleTryUTF8(c, lc) wxSetlocale(c, lc) |
41524ffc VS |
352 | #endif |
353 | ||
d3f3e35f VS |
354 | bool wxLocale::Init(int language, int flags) |
355 | { | |
3acf8a8d VS |
356 | #if WXWIN_COMPATIBILITY_2_8 |
357 | wxASSERT_MSG( !(flags & wxLOCALE_CONV_ENCODING), | |
358 | wxS("wxLOCALE_CONV_ENCODING is no longer supported, add charset to your catalogs") ); | |
359 | #endif | |
360 | ||
2aebd278 VZ |
361 | bool ret = true; |
362 | ||
d3f3e35f | 363 | int lang = language; |
ec37df57 VZ |
364 | if (lang == wxLANGUAGE_DEFAULT) |
365 | { | |
366 | // auto detect the language | |
367 | lang = GetSystemLanguage(); | |
368 | } | |
369 | ||
370 | // We failed to detect system language, so we will use English: | |
371 | if (lang == wxLANGUAGE_UNKNOWN) | |
372 | { | |
ca642c8a | 373 | return false; |
ec37df57 VZ |
374 | } |
375 | ||
14f8fa9d | 376 | const wxLanguageInfo *info = GetLanguageInfo(lang); |
d3f3e35f | 377 | |
d3f3e35f VS |
378 | // Unknown language: |
379 | if (info == NULL) | |
380 | { | |
a0516656 | 381 | wxLogError(wxS("Unknown language %i."), lang); |
7beb59f3 | 382 | return false; |
d3f3e35f VS |
383 | } |
384 | ||
385 | wxString name = info->Description; | |
386 | wxString canonical = info->CanonicalName; | |
387 | wxString locale; | |
ec37df57 | 388 | |
d3f3e35f | 389 | // Set the locale: |
55034339 | 390 | #if defined(__OS2__) |
52de37c7 | 391 | const char *retloc = wxSetlocale(LC_ALL , wxEmptyString); |
55034339 | 392 | #elif defined(__UNIX__) && !defined(__WXMAC__) |
d368dbea | 393 | if (language != wxLANGUAGE_DEFAULT) |
ec37df57 | 394 | locale = info->CanonicalName; |
d3f3e35f | 395 | |
52de37c7 | 396 | const char *retloc = wxSetlocaleTryUTF8(LC_ALL, locale); |
d3f3e35f | 397 | |
f6ba36e4 | 398 | const wxString langOnly = ExtractLang(locale); |
2b5f62a0 | 399 | if ( !retloc ) |
d3f3e35f VS |
400 | { |
401 | // Some C libraries don't like xx_YY form and require xx only | |
cb352236 | 402 | retloc = wxSetlocaleTryUTF8(LC_ALL, langOnly); |
d3f3e35f | 403 | } |
d368dbea VZ |
404 | |
405 | #if wxUSE_FONTMAP | |
406 | // some systems (e.g. FreeBSD and HP-UX) don't have xx_YY aliases but | |
407 | // require the full xx_YY.encoding form, so try using UTF-8 because this is | |
408 | // the only thing we can do generically | |
409 | // | |
410 | // TODO: add encodings applicable to each language to the lang DB and try | |
411 | // them all in turn here | |
2b5f62a0 | 412 | if ( !retloc ) |
d3f3e35f | 413 | { |
d368dbea VZ |
414 | const wxChar **names = |
415 | wxFontMapperBase::GetAllEncodingNames(wxFONTENCODING_UTF8); | |
416 | while ( *names ) | |
417 | { | |
a0516656 | 418 | retloc = wxSetlocale(LC_ALL, locale + wxS('.') + *names++); |
d368dbea VZ |
419 | if ( retloc ) |
420 | break; | |
421 | } | |
d3f3e35f | 422 | } |
d368dbea VZ |
423 | #endif // wxUSE_FONTMAP |
424 | ||
2b5f62a0 | 425 | if ( !retloc ) |
d3f3e35f | 426 | { |
d368dbea VZ |
427 | // Some C libraries (namely glibc) still use old ISO 639, |
428 | // so will translate the abbrev for them | |
429 | wxString localeAlt; | |
a0516656 | 430 | if ( langOnly == wxS("he") ) |
f6ba36e4 | 431 | localeAlt = wxS("iw") + ExtractNotLang(locale); |
a0516656 | 432 | else if ( langOnly == wxS("id") ) |
f6ba36e4 | 433 | localeAlt = wxS("in") + ExtractNotLang(locale); |
a0516656 | 434 | else if ( langOnly == wxS("yi") ) |
f6ba36e4 | 435 | localeAlt = wxS("ji") + ExtractNotLang(locale); |
a0516656 VZ |
436 | else if ( langOnly == wxS("nb") ) |
437 | localeAlt = wxS("no_NO"); | |
438 | else if ( langOnly == wxS("nn") ) | |
439 | localeAlt = wxS("no_NY"); | |
d368dbea VZ |
440 | |
441 | if ( !localeAlt.empty() ) | |
442 | { | |
cb352236 | 443 | retloc = wxSetlocaleTryUTF8(LC_ALL, localeAlt); |
d368dbea | 444 | if ( !retloc ) |
f6ba36e4 | 445 | retloc = wxSetlocaleTryUTF8(LC_ALL, ExtractLang(localeAlt)); |
d368dbea | 446 | } |
d3f3e35f | 447 | } |
d368dbea | 448 | |
2b5f62a0 | 449 | if ( !retloc ) |
2aebd278 | 450 | ret = false; |
d368dbea VZ |
451 | |
452 | #ifdef __AIX__ | |
31b7522e VS |
453 | // at least in AIX 5.2 libc is buggy and the string returned from |
454 | // setlocale(LC_ALL) can't be passed back to it because it returns 6 | |
455 | // strings (one for each locale category), i.e. for C locale we get back | |
456 | // "C C C C C C" | |
d368dbea | 457 | // |
31b7522e VS |
458 | // this contradicts IBM own docs but this is not of much help, so just work |
459 | // around it in the crudest possible manner | |
c222ad41 | 460 | char* p = const_cast<char*>(wxStrchr(retloc, ' ')); |
d368dbea | 461 | if ( p ) |
52de37c7 | 462 | *p = '\0'; |
d368dbea VZ |
463 | #endif // __AIX__ |
464 | ||
d3f3e35f | 465 | #elif defined(__WIN32__) |
52de37c7 | 466 | const char *retloc = "C"; |
42db82fd | 467 | if ( language != wxLANGUAGE_DEFAULT ) |
d3f3e35f | 468 | { |
42db82fd | 469 | if ( info->WinLang == 0 ) |
d3f3e35f | 470 | { |
a0516656 | 471 | wxLogWarning(wxS("Locale '%s' not supported by OS."), name.c_str()); |
2b5f62a0 | 472 | // retloc already set to "C" |
63986ba6 | 473 | } |
42db82fd | 474 | else // language supported by Windows |
ec37df57 | 475 | { |
99abd9c8 VZ |
476 | // Windows CE doesn't have SetThreadLocale() and there doesn't seem |
477 | // to be any equivalent | |
1c193821 | 478 | #ifndef __WXWINCE__ |
fb090fa1 VZ |
479 | const wxUint32 lcid = info->GetLCID(); |
480 | ||
86d62b51 VZ |
481 | // change locale used by Windows functions |
482 | ::SetThreadLocale(lcid); | |
1c193821 | 483 | #endif |
99abd9c8 | 484 | |
86d62b51 VZ |
485 | // and also call setlocale() to change locale used by the CRT |
486 | locale = info->GetLocaleName(); | |
487 | if ( locale.empty() ) | |
65dc921d | 488 | { |
2aebd278 | 489 | ret = false; |
2b5f62a0 | 490 | } |
86d62b51 | 491 | else // have a valid locale |
c611452f | 492 | { |
65dc921d | 493 | retloc = wxSetlocale(LC_ALL, locale); |
c611452f | 494 | } |
d3f3e35f VS |
495 | } |
496 | } | |
42db82fd | 497 | else // language == wxLANGUAGE_DEFAULT |
2b5f62a0 | 498 | { |
c611452f | 499 | retloc = wxSetlocale(LC_ALL, wxEmptyString); |
42db82fd | 500 | } |
99abd9c8 | 501 | |
42db82fd VZ |
502 | #if wxUSE_UNICODE && (defined(__VISUALC__) || defined(__MINGW32__)) |
503 | // VC++ setlocale() (also used by Mingw) can't set locale to languages that | |
504 | // can only be written using Unicode, therefore wxSetlocale() call fails | |
505 | // for such languages but we don't want to report it as an error -- so that | |
506 | // at least message catalogs can be used. | |
507 | if ( !retloc ) | |
508 | { | |
509 | if ( wxGetANSICodePageForLocale(LOCALE_USER_DEFAULT).empty() ) | |
65dc921d | 510 | { |
42db82fd VZ |
511 | // we set the locale to a Unicode-only language, don't treat the |
512 | // inability of CRT to use it as an error | |
513 | retloc = "C"; | |
65dc921d | 514 | } |
2b5f62a0 | 515 | } |
42db82fd | 516 | #endif // CRT not handling Unicode-only languages |
c611452f | 517 | |
2b5f62a0 | 518 | if ( !retloc ) |
2aebd278 | 519 | ret = false; |
85fb2206 | 520 | #elif defined(__WXMAC__) |
b51daa4f | 521 | if (lang == wxLANGUAGE_DEFAULT) |
85fb2206 SC |
522 | locale = wxEmptyString; |
523 | else | |
524 | locale = info->CanonicalName; | |
525 | ||
52de37c7 | 526 | const char *retloc = wxSetlocale(LC_ALL, locale); |
85fb2206 SC |
527 | |
528 | if ( !retloc ) | |
529 | { | |
530 | // Some C libraries don't like xx_YY form and require xx only | |
f6ba36e4 | 531 | retloc = wxSetlocale(LC_ALL, ExtractLang(locale)); |
85fb2206 | 532 | } |
d3f3e35f | 533 | #else |
ead190e4 | 534 | wxUnusedVar(flags); |
7beb59f3 | 535 | return false; |
e879e020 | 536 | #define WX_NO_LOCALE_SUPPORT |
d3f3e35f | 537 | #endif |
ec37df57 | 538 | |
e879e020 | 539 | #ifndef WX_NO_LOCALE_SUPPORT |
2aebd278 VZ |
540 | if ( !ret ) |
541 | { | |
542 | wxLogWarning(_("Cannot set locale to language \"%s\"."), name.c_str()); | |
543 | ||
544 | // continue nevertheless and try to load at least the translations for | |
545 | // this language | |
546 | } | |
547 | ||
18e065b4 | 548 | if ( !DoInit(name, canonical, retloc) ) |
2aebd278 VZ |
549 | { |
550 | ret = false; | |
551 | } | |
10545ca4 | 552 | |
549a7c37 | 553 | if (IsOk()) // setlocale() succeeded |
10545ca4 VZ |
554 | m_language = lang; |
555 | ||
18e065b4 | 556 | // NB: don't use 'lang' here, 'language' |
0d28a1f9 VS |
557 | wxTranslations *t = wxTranslations::Get(); |
558 | if ( t ) | |
559 | { | |
560 | t->SetLanguage(static_cast<wxLanguage>(language)); | |
d3f3e35f | 561 | |
0d28a1f9 VS |
562 | if ( flags & wxLOCALE_LOAD_DEFAULT ) |
563 | t->AddStdCatalog(); | |
564 | } | |
d3f3e35f | 565 | |
18e065b4 VS |
566 | return ret; |
567 | #endif // !WX_NO_LOCALE_SUPPORT | |
fd323a5e VZ |
568 | } |
569 | ||
968b44d3 VZ |
570 | namespace |
571 | { | |
572 | ||
17473a77 | 573 | #ifndef __WXOSX__ |
968b44d3 VZ |
574 | // Small helper function: get the value of the given environment variable and |
575 | // return true only if the variable was found and has non-empty value. | |
576 | inline bool wxGetNonEmptyEnvVar(const wxString& name, wxString* value) | |
577 | { | |
578 | return wxGetEnv(name, value) && !value->empty(); | |
579 | } | |
17473a77 | 580 | #endif |
968b44d3 VZ |
581 | |
582 | } // anonymous namespace | |
583 | ||
41780009 | 584 | /*static*/ int wxLocale::GetSystemLanguage() |
d3f3e35f | 585 | { |
41780009 | 586 | CreateLanguagesDB(); |
ec37df57 VZ |
587 | |
588 | // init i to avoid compiler warning | |
589 | size_t i = 0, | |
ca642c8a | 590 | count = ms_languagesDB->GetCount(); |
d3f3e35f | 591 | |
9b3dbb11 | 592 | #if defined(__UNIX__) |
ec37df57 VZ |
593 | // first get the string identifying the language from the environment |
594 | wxString langFull; | |
17473a77 | 595 | #ifdef __WXOSX__ |
9b3dbb11 | 596 | wxCFRef<CFLocaleRef> userLocaleRef(CFLocaleCopyCurrent()); |
529e125d JS |
597 | |
598 | // because the locale identifier (kCFLocaleIdentifier) is formatted a little bit differently, eg | |
9b3dbb11 | 599 | // az_Cyrl_AZ@calendar=buddhist;currency=JPY we just recreate the base info as expected by wx here |
529e125d | 600 | |
80539f04 | 601 | wxCFStringRef str(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleLanguageCode))); |
9b3dbb11 | 602 | langFull = str.AsString()+"_"; |
80539f04 | 603 | str.reset(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleCountryCode))); |
9b3dbb11 SC |
604 | langFull += str.AsString(); |
605 | #else | |
968b44d3 VZ |
606 | if (!wxGetNonEmptyEnvVar(wxS("LC_ALL"), &langFull) && |
607 | !wxGetNonEmptyEnvVar(wxS("LC_MESSAGES"), &langFull) && | |
608 | !wxGetNonEmptyEnvVar(wxS("LANG"), &langFull)) | |
ec37df57 | 609 | { |
ff1bd293 VZ |
610 | // no language specified, treat it as English |
611 | return wxLANGUAGE_ENGLISH_US; | |
ec37df57 | 612 | } |
d3f3e35f | 613 | |
a0516656 | 614 | if ( langFull == wxS("C") || langFull == wxS("POSIX") ) |
d3f3e35f | 615 | { |
ff1bd293 VZ |
616 | // default C locale is English too |
617 | return wxLANGUAGE_ENGLISH_US; | |
d3f3e35f | 618 | } |
9b3dbb11 | 619 | #endif |
d3f3e35f | 620 | |
ec37df57 VZ |
621 | // the language string has the following form |
622 | // | |
efd1393b | 623 | // lang[_LANG][.encoding][@modifier] |
ec37df57 | 624 | // |
efd1393b VZ |
625 | // (see environ(5) in the Open Unix specification) |
626 | // | |
627 | // where lang is the primary language, LANG is a sublang/territory, | |
628 | // encoding is the charset to use and modifier "allows the user to select | |
629 | // a specific instance of localization data within a single category" | |
ec37df57 VZ |
630 | // |
631 | // for example, the following strings are valid: | |
632 | // fr | |
633 | // fr_FR | |
634 | // de_DE.iso88591 | |
efd1393b VZ |
635 | // de_DE@euro |
636 | // de_DE.iso88591@euro | |
ec37df57 VZ |
637 | |
638 | // for now we don't use the encoding, although we probably should (doing | |
639 | // translations of the msg catalogs on the fly as required) (TODO) | |
efd1393b | 640 | // |
8cee59b5 VZ |
641 | // we need the modified for languages like Valencian: ca_ES@valencia |
642 | // though, remember it | |
643 | wxString modifier; | |
644 | size_t posModifier = langFull.find_first_of(wxS("@")); | |
645 | if ( posModifier != wxString::npos ) | |
646 | modifier = langFull.Mid(posModifier); | |
647 | ||
a0516656 | 648 | size_t posEndLang = langFull.find_first_of(wxS("@.")); |
efd1393b VZ |
649 | if ( posEndLang != wxString::npos ) |
650 | { | |
651 | langFull.Truncate(posEndLang); | |
652 | } | |
ec37df57 | 653 | |
ec37df57 | 654 | // do we have just the language (or sublang too)? |
ce411ca8 VS |
655 | const bool justLang = langFull.find('_') == wxString::npos; |
656 | ||
657 | // 0. Make sure the lang is according to latest ISO 639 | |
658 | // (this is necessary because glibc uses iw and in instead | |
659 | // of he and id respectively). | |
660 | ||
661 | // the language itself (second part is the dialect/sublang) | |
662 | wxString langOrig = ExtractLang(langFull); | |
663 | ||
664 | wxString lang; | |
665 | if ( langOrig == wxS("iw")) | |
666 | lang = wxS("he"); | |
667 | else if (langOrig == wxS("in")) | |
668 | lang = wxS("id"); | |
669 | else if (langOrig == wxS("ji")) | |
670 | lang = wxS("yi"); | |
671 | else if (langOrig == wxS("no_NO")) | |
672 | lang = wxS("nb_NO"); | |
673 | else if (langOrig == wxS("no_NY")) | |
674 | lang = wxS("nn_NO"); | |
675 | else if (langOrig == wxS("no")) | |
676 | lang = wxS("nb_NO"); | |
677 | else | |
678 | lang = langOrig; | |
ec37df57 | 679 | |
ce411ca8 VS |
680 | // did we change it? |
681 | if ( lang != langOrig ) | |
682 | { | |
683 | langFull = lang + ExtractNotLang(langFull); | |
684 | } | |
685 | ||
686 | // 1. Try to find the language either as is: | |
687 | // a) With modifier if set | |
688 | if ( !modifier.empty() ) | |
689 | { | |
690 | wxString langFullWithModifier = langFull + modifier; | |
691 | for ( i = 0; i < count; i++ ) | |
ec37df57 | 692 | { |
ce411ca8 VS |
693 | if ( ms_languagesDB->Item(i).CanonicalName == langFullWithModifier ) |
694 | break; | |
8cee59b5 | 695 | } |
ce411ca8 | 696 | } |
8cee59b5 | 697 | |
ce411ca8 VS |
698 | // b) Without modifier |
699 | if ( modifier.empty() || i == count ) | |
700 | { | |
701 | for ( i = 0; i < count; i++ ) | |
8cee59b5 | 702 | { |
ce411ca8 VS |
703 | if ( ms_languagesDB->Item(i).CanonicalName == langFull ) |
704 | break; | |
d3f3e35f | 705 | } |
ce411ca8 | 706 | } |
d3f3e35f | 707 | |
ce411ca8 VS |
708 | // 2. If langFull is of the form xx_YY, try to find xx: |
709 | if ( i == count && !justLang ) | |
710 | { | |
711 | for ( i = 0; i < count; i++ ) | |
d3f3e35f | 712 | { |
ce411ca8 | 713 | if ( ms_languagesDB->Item(i).CanonicalName == lang ) |
d3f3e35f | 714 | { |
ce411ca8 | 715 | break; |
d3f3e35f VS |
716 | } |
717 | } | |
ce411ca8 | 718 | } |
d3f3e35f | 719 | |
ce411ca8 VS |
720 | // 3. If langFull is of the form xx, try to find any xx_YY record: |
721 | if ( i == count && justLang ) | |
722 | { | |
723 | for ( i = 0; i < count; i++ ) | |
d3f3e35f | 724 | { |
ce411ca8 VS |
725 | if ( ExtractLang(ms_languagesDB->Item(i).CanonicalName) |
726 | == langFull ) | |
d3f3e35f | 727 | { |
ce411ca8 | 728 | break; |
d3f3e35f VS |
729 | } |
730 | } | |
731 | } | |
ce411ca8 VS |
732 | |
733 | ||
734 | if ( i == count ) | |
d3f3e35f | 735 | { |
ce411ca8 VS |
736 | // In addition to the format above, we also can have full language |
737 | // names in LANG env var - for example, SuSE is known to use | |
738 | // LANG="german" - so check for use of non-standard format and try to | |
739 | // find the name in verbose description. | |
ec37df57 | 740 | for ( i = 0; i < count; i++ ) |
d3f3e35f | 741 | { |
41780009 | 742 | if (ms_languagesDB->Item(i).Description.CmpNoCase(langFull) == 0) |
d3f3e35f | 743 | { |
d3f3e35f VS |
744 | break; |
745 | } | |
746 | } | |
747 | } | |
d3f3e35f VS |
748 | #elif defined(__WIN32__) |
749 | LCID lcid = GetUserDefaultLCID(); | |
ec37df57 | 750 | if ( lcid != 0 ) |
d3f3e35f | 751 | { |
ec37df57 VZ |
752 | wxUint32 lang = PRIMARYLANGID(LANGIDFROMLCID(lcid)); |
753 | wxUint32 sublang = SUBLANGID(LANGIDFROMLCID(lcid)); | |
754 | ||
755 | for ( i = 0; i < count; i++ ) | |
d3f3e35f | 756 | { |
41780009 VS |
757 | if (ms_languagesDB->Item(i).WinLang == lang && |
758 | ms_languagesDB->Item(i).WinSublang == sublang) | |
ec37df57 VZ |
759 | { |
760 | break; | |
761 | } | |
d3f3e35f VS |
762 | } |
763 | } | |
ec37df57 VZ |
764 | //else: leave wxlang == wxLANGUAGE_UNKNOWN |
765 | #endif // Unix/Win32 | |
d3f3e35f | 766 | |
ec37df57 VZ |
767 | if ( i < count ) |
768 | { | |
769 | // we did find a matching entry, use it | |
41780009 | 770 | return ms_languagesDB->Item(i).Language; |
ec37df57 | 771 | } |
d3f3e35f | 772 | |
ec37df57 VZ |
773 | // no info about this language in the database |
774 | return wxLANGUAGE_UNKNOWN; | |
775 | } | |
d3f3e35f | 776 | |
dccce9ea VZ |
777 | // ---------------------------------------------------------------------------- |
778 | // encoding stuff | |
779 | // ---------------------------------------------------------------------------- | |
780 | ||
781 | // this is a bit strange as under Windows we get the encoding name using its | |
782 | // numeric value and under Unix we do it the other way round, but this just | |
97626624 | 783 | // reflects the way different systems provide the encoding info |
dccce9ea VZ |
784 | |
785 | /* static */ | |
786 | wxString wxLocale::GetSystemEncodingName() | |
787 | { | |
788 | wxString encname; | |
789 | ||
c67d6888 | 790 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
dccce9ea VZ |
791 | // FIXME: what is the error return value for GetACP()? |
792 | UINT codepage = ::GetACP(); | |
a0516656 | 793 | encname.Printf(wxS("windows-%u"), codepage); |
1ad48afb | 794 | #elif defined(__WXMAC__) |
b8613f81 VZ |
795 | encname = wxCFStringRef::AsString( |
796 | CFStringGetNameOfEncoding(CFStringGetSystemEncoding()) | |
797 | ); | |
dccce9ea VZ |
798 | #elif defined(__UNIX_LIKE__) |
799 | ||
800 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) | |
801 | // GNU libc provides current character set this way (this conforms | |
802 | // to Unix98) | |
2ec6905c VS |
803 | char *oldLocale = strdup(setlocale(LC_CTYPE, NULL)); |
804 | setlocale(LC_CTYPE, ""); | |
f284605f | 805 | const char *alang = nl_langinfo(CODESET); |
2ec6905c | 806 | setlocale(LC_CTYPE, oldLocale); |
030c0bea | 807 | free(oldLocale); |
84f858e9 VZ |
808 | |
809 | if ( alang ) | |
dccce9ea | 810 | { |
09d2016d | 811 | encname = wxString::FromAscii( alang ); |
dccce9ea | 812 | } |
09d2016d | 813 | else // nl_langinfo() failed |
dccce9ea VZ |
814 | #endif // HAVE_LANGINFO_H |
815 | { | |
816 | // if we can't get at the character set directly, try to see if it's in | |
817 | // the environment variables (in most cases this won't work, but I was | |
818 | // out of ideas) | |
b1ac3b56 | 819 | char *lang = getenv( "LC_ALL"); |
d3b9f782 | 820 | char *dot = lang ? strchr(lang, '.') : NULL; |
dccce9ea VZ |
821 | if (!dot) |
822 | { | |
b1ac3b56 | 823 | lang = getenv( "LC_CTYPE" ); |
dccce9ea | 824 | if ( lang ) |
b1ac3b56 | 825 | dot = strchr(lang, '.' ); |
dccce9ea VZ |
826 | } |
827 | if (!dot) | |
828 | { | |
b1ac3b56 | 829 | lang = getenv( "LANG"); |
dccce9ea | 830 | if ( lang ) |
b1ac3b56 | 831 | dot = strchr(lang, '.'); |
dccce9ea VZ |
832 | } |
833 | ||
834 | if ( dot ) | |
835 | { | |
b1ac3b56 | 836 | encname = wxString::FromAscii( dot+1 ); |
dccce9ea VZ |
837 | } |
838 | } | |
839 | #endif // Win32/Unix | |
840 | ||
841 | return encname; | |
842 | } | |
843 | ||
844 | /* static */ | |
845 | wxFontEncoding wxLocale::GetSystemEncoding() | |
846 | { | |
c67d6888 | 847 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
dccce9ea VZ |
848 | UINT codepage = ::GetACP(); |
849 | ||
3c832d58 | 850 | // wxWidgets only knows about CP1250-1257, 874, 932, 936, 949, 950 |
dccce9ea VZ |
851 | if ( codepage >= 1250 && codepage <= 1257 ) |
852 | { | |
853 | return (wxFontEncoding)(wxFONTENCODING_CP1250 + codepage - 1250); | |
854 | } | |
c7821f94 | 855 | |
3c832d58 DS |
856 | if ( codepage == 874 ) |
857 | { | |
858 | return wxFONTENCODING_CP874; | |
859 | } | |
860 | ||
c7821f94 VZ |
861 | if ( codepage == 932 ) |
862 | { | |
863 | return wxFONTENCODING_CP932; | |
864 | } | |
865 | ||
866 | if ( codepage == 936 ) | |
867 | { | |
868 | return wxFONTENCODING_CP936; | |
869 | } | |
870 | ||
871 | if ( codepage == 949 ) | |
872 | { | |
873 | return wxFONTENCODING_CP949; | |
874 | } | |
875 | ||
876 | if ( codepage == 950 ) | |
877 | { | |
878 | return wxFONTENCODING_CP950; | |
879 | } | |
ec79aded | 880 | #elif defined(__WXMAC__) |
73b51ea4 | 881 | CFStringEncoding encoding = 0 ; |
7beb59f3 | 882 | encoding = CFStringGetSystemEncoding() ; |
9e687455 | 883 | return wxMacGetFontEncFromSystemEnc( encoding ) ; |
1e6feb95 | 884 | #elif defined(__UNIX_LIKE__) && wxUSE_FONTMAP |
09d2016d | 885 | const wxString encname = GetSystemEncodingName(); |
dccce9ea VZ |
886 | if ( !encname.empty() ) |
887 | { | |
09d2016d | 888 | wxFontEncoding enc = wxFontMapperBase::GetEncodingFromName(encname); |
9cf28550 | 889 | |
2b5f62a0 | 890 | // on some modern Linux systems (RedHat 8) the default system locale |
13f095d2 | 891 | // is UTF8 -- but it isn't supported by wxGTK1 in ANSI build at all so |
2b5f62a0 | 892 | // don't even try to use it in this case |
13f095d2 VZ |
893 | #if !wxUSE_UNICODE && \ |
894 | ((defined(__WXGTK__) && !defined(__WXGTK20__)) || defined(__WXMOTIF__)) | |
2b5f62a0 VZ |
895 | if ( enc == wxFONTENCODING_UTF8 ) |
896 | { | |
897 | // the most similar supported encoding... | |
898 | enc = wxFONTENCODING_ISO8859_1; | |
899 | } | |
900 | #endif // !wxUSE_UNICODE | |
901 | ||
09d2016d VZ |
902 | // GetEncodingFromName() returns wxFONTENCODING_DEFAULT for C locale |
903 | // (a.k.a. US-ASCII) which is arguably a bug but keep it like this for | |
904 | // backwards compatibility and just take care to not return | |
905 | // wxFONTENCODING_DEFAULT from here as this surely doesn't make sense | |
8b34b163 VZ |
906 | if ( enc == wxFONTENCODING_DEFAULT ) |
907 | { | |
908 | // we don't have wxFONTENCODING_ASCII, so use the closest one | |
909 | return wxFONTENCODING_ISO8859_1; | |
910 | } | |
911 | ||
912 | if ( enc != wxFONTENCODING_MAX ) | |
9cf28550 VZ |
913 | { |
914 | return enc; | |
915 | } | |
916 | //else: return wxFONTENCODING_SYSTEM below | |
dccce9ea VZ |
917 | } |
918 | #endif // Win32/Unix | |
919 | ||
920 | return wxFONTENCODING_SYSTEM; | |
921 | } | |
922 | ||
14f8fa9d VZ |
923 | /* static */ |
924 | void wxLocale::AddLanguage(const wxLanguageInfo& info) | |
d3f3e35f | 925 | { |
41780009 VS |
926 | CreateLanguagesDB(); |
927 | ms_languagesDB->Add(info); | |
d3f3e35f VS |
928 | } |
929 | ||
14f8fa9d VZ |
930 | /* static */ |
931 | const wxLanguageInfo *wxLocale::GetLanguageInfo(int lang) | |
932 | { | |
933 | CreateLanguagesDB(); | |
934 | ||
f0ab09cc VZ |
935 | // calling GetLanguageInfo(wxLANGUAGE_DEFAULT) is a natural thing to do, so |
936 | // make it work | |
937 | if ( lang == wxLANGUAGE_DEFAULT ) | |
938 | lang = GetSystemLanguage(); | |
939 | ||
9d1e1be4 | 940 | const size_t count = ms_languagesDB->GetCount(); |
14f8fa9d VZ |
941 | for ( size_t i = 0; i < count; i++ ) |
942 | { | |
943 | if ( ms_languagesDB->Item(i).Language == lang ) | |
944 | { | |
b2fde0cf CE |
945 | // We need to create a temporary here in order to make this work with BCC in final build mode |
946 | wxLanguageInfo *ptr = &ms_languagesDB->Item(i); | |
947 | return ptr; | |
14f8fa9d VZ |
948 | } |
949 | } | |
950 | ||
951 | return NULL; | |
952 | } | |
953 | ||
4a6e4a46 VS |
954 | /* static */ |
955 | wxString wxLocale::GetLanguageName(int lang) | |
956 | { | |
18e065b4 VS |
957 | if ( lang == wxLANGUAGE_DEFAULT || lang == wxLANGUAGE_UNKNOWN ) |
958 | return wxEmptyString; | |
959 | ||
4a6e4a46 VS |
960 | const wxLanguageInfo *info = GetLanguageInfo(lang); |
961 | if ( !info ) | |
962 | return wxEmptyString; | |
963 | else | |
964 | return info->Description; | |
965 | } | |
966 | ||
18e065b4 VS |
967 | /* static */ |
968 | wxString wxLocale::GetLanguageCanonicalName(int lang) | |
969 | { | |
970 | if ( lang == wxLANGUAGE_DEFAULT || lang == wxLANGUAGE_UNKNOWN ) | |
971 | return wxEmptyString; | |
972 | ||
973 | const wxLanguageInfo *info = GetLanguageInfo(lang); | |
974 | if ( !info ) | |
975 | return wxEmptyString; | |
976 | else | |
977 | return info->CanonicalName; | |
978 | } | |
979 | ||
9d1e1be4 VZ |
980 | /* static */ |
981 | const wxLanguageInfo *wxLocale::FindLanguageInfo(const wxString& locale) | |
982 | { | |
983 | CreateLanguagesDB(); | |
984 | ||
985 | const wxLanguageInfo *infoRet = NULL; | |
986 | ||
987 | const size_t count = ms_languagesDB->GetCount(); | |
988 | for ( size_t i = 0; i < count; i++ ) | |
989 | { | |
990 | const wxLanguageInfo *info = &ms_languagesDB->Item(i); | |
991 | ||
992 | if ( wxStricmp(locale, info->CanonicalName) == 0 || | |
993 | wxStricmp(locale, info->Description) == 0 ) | |
994 | { | |
995 | // exact match, stop searching | |
996 | infoRet = info; | |
997 | break; | |
998 | } | |
999 | ||
a0516656 | 1000 | if ( wxStricmp(locale, info->CanonicalName.BeforeFirst(wxS('_'))) == 0 ) |
9d1e1be4 VZ |
1001 | { |
1002 | // a match -- but maybe we'll find an exact one later, so continue | |
1003 | // looking | |
1004 | // | |
1005 | // OTOH, maybe we had already found a language match and in this | |
52de37c7 | 1006 | // case don't overwrite it because the entry for the default |
9d1e1be4 VZ |
1007 | // country always appears first in ms_languagesDB |
1008 | if ( !infoRet ) | |
1009 | infoRet = info; | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | return infoRet; | |
1014 | } | |
1015 | ||
d3f3e35f VS |
1016 | wxString wxLocale::GetSysName() const |
1017 | { | |
1018 | return wxSetlocale(LC_ALL, NULL); | |
1019 | } | |
1020 | ||
c801d85f KB |
1021 | // clean up |
1022 | wxLocale::~wxLocale() | |
1023 | { | |
0d28a1f9 VS |
1024 | // Restore old translations object. |
1025 | // See DoCommonInit() for explanation of why this is needed for backward | |
1026 | // compatibility. | |
18e065b4 VS |
1027 | if ( wxTranslations::Get() == &m_translations ) |
1028 | { | |
1029 | if ( m_pOldLocale ) | |
1030 | wxTranslations::SetNonOwned(&m_pOldLocale->m_translations); | |
1031 | else | |
1032 | wxTranslations::Set(NULL); | |
fd323a5e VZ |
1033 | } |
1034 | ||
eb48778d VZ |
1035 | // restore old locale pointer |
1036 | wxSetLocale(m_pOldLocale); | |
bea561ce | 1037 | |
e36e6f95 | 1038 | wxSetlocale(LC_ALL, m_pszOldLocale); |
f48a1159 | 1039 | free(const_cast<char *>(m_pszOldLocale)); |
c801d85f KB |
1040 | } |
1041 | ||
c801d85f | 1042 | |
cec5ffc4 VZ |
1043 | // check if the given locale is provided by OS and C run time |
1044 | /* static */ | |
1045 | bool wxLocale::IsAvailable(int lang) | |
1046 | { | |
1047 | const wxLanguageInfo *info = wxLocale::GetLanguageInfo(lang); | |
0f4bf6ac VZ |
1048 | if ( !info ) |
1049 | { | |
1050 | // The language is unknown (this normally only happens when we're | |
1051 | // passed wxLANGUAGE_DEFAULT), so we can't support it. | |
1052 | wxASSERT_MSG( lang == wxLANGUAGE_DEFAULT, | |
1053 | wxS("No info for a valid language?") ); | |
1054 | return false; | |
1055 | } | |
cec5ffc4 | 1056 | |
327bf990 | 1057 | #if defined(__WIN32__) |
cec5ffc4 VZ |
1058 | if ( !info->WinLang ) |
1059 | return false; | |
1060 | ||
ea65760d | 1061 | if ( !::IsValidLocale(info->GetLCID(), LCID_INSTALLED) ) |
327bf990 RD |
1062 | return false; |
1063 | ||
1064 | #elif defined(__UNIX__) | |
52de37c7 VS |
1065 | |
1066 | // Test if setting the locale works, then set it back. | |
fd1c361c VZ |
1067 | char * const oldLocale = wxStrdupA(setlocale(LC_ALL, NULL)); |
1068 | ||
1069 | // Some platforms don't like xx_YY form and require xx only so test for | |
1070 | // it too. | |
1071 | const bool | |
1072 | available = wxSetlocaleTryUTF8(LC_ALL, info->CanonicalName) || | |
1073 | wxSetlocaleTryUTF8(LC_ALL, ExtractLang(info->CanonicalName)); | |
1074 | ||
327bf990 | 1075 | // restore the original locale |
52de37c7 | 1076 | wxSetlocale(LC_ALL, oldLocale); |
fd1c361c VZ |
1077 | |
1078 | free(oldLocale); | |
1079 | ||
1080 | if ( !available ) | |
1081 | return false; | |
52de37c7 | 1082 | #endif |
cec5ffc4 VZ |
1083 | |
1084 | return true; | |
1085 | } | |
1086 | ||
0d28a1f9 VS |
1087 | |
1088 | bool wxLocale::AddCatalog(const wxString& domain) | |
1089 | { | |
1090 | wxTranslations *t = wxTranslations::Get(); | |
1091 | if ( !t ) | |
1092 | return false; | |
1093 | return t->AddCatalog(domain); | |
1094 | } | |
1095 | ||
1096 | bool wxLocale::AddCatalog(const wxString& domain, wxLanguage msgIdLanguage) | |
1097 | { | |
1098 | wxTranslations *t = wxTranslations::Get(); | |
1099 | if ( !t ) | |
1100 | return false; | |
1101 | return t->AddCatalog(domain, msgIdLanguage); | |
1102 | } | |
1103 | ||
d721baa9 | 1104 | // add a catalog to our linked list |
31b7522e | 1105 | bool wxLocale::AddCatalog(const wxString& szDomain, |
ca642c8a FM |
1106 | wxLanguage msgIdLanguage, |
1107 | const wxString& msgIdCharset) | |
c801d85f | 1108 | { |
0d28a1f9 VS |
1109 | wxTranslations *t = wxTranslations::Get(); |
1110 | if ( !t ) | |
1111 | return false; | |
18e065b4 VS |
1112 | #if wxUSE_UNICODE |
1113 | wxUnusedVar(msgIdCharset); | |
0d28a1f9 | 1114 | return t->AddCatalog(szDomain, msgIdLanguage); |
18e065b4 | 1115 | #else |
0d28a1f9 | 1116 | return t->AddCatalog(szDomain, msgIdLanguage, msgIdCharset); |
18e065b4 | 1117 | #endif |
c801d85f KB |
1118 | } |
1119 | ||
0d28a1f9 VS |
1120 | bool wxLocale::IsLoaded(const wxString& domain) const |
1121 | { | |
1122 | wxTranslations *t = wxTranslations::Get(); | |
1123 | if ( !t ) | |
1124 | return false; | |
1125 | return t->IsLoaded(domain); | |
1126 | } | |
1127 | ||
1128 | wxString wxLocale::GetHeaderValue(const wxString& header, | |
1129 | const wxString& domain) const | |
1130 | { | |
1131 | wxTranslations *t = wxTranslations::Get(); | |
1132 | if ( !t ) | |
1133 | return wxEmptyString; | |
1134 | return t->GetHeaderValue(header, domain); | |
1135 | } | |
1136 | ||
cfb20656 VZ |
1137 | // ---------------------------------------------------------------------------- |
1138 | // accessors for locale-dependent data | |
1139 | // ---------------------------------------------------------------------------- | |
1140 | ||
d98a58c5 | 1141 | #if defined(__WINDOWS__) || defined(__WXOSX__) |
89a7e1ff VZ |
1142 | |
1143 | namespace | |
1144 | { | |
1145 | ||
1146 | // This function translates from Unicode date formats described at | |
1147 | // | |
1148 | // http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns | |
1149 | // | |
1150 | // to strftime()-like syntax. This translation is not lossless but we try to do | |
1151 | // our best. | |
1152 | ||
1153 | static wxString TranslateFromUnicodeFormat(const wxString& fmt) | |
1154 | { | |
1155 | wxString fmtWX; | |
1156 | fmtWX.reserve(fmt.length()); | |
1157 | ||
1158 | char chLast = '\0'; | |
1159 | size_t lastCount = 0; | |
03647350 VZ |
1160 | |
1161 | const char* formatchars = | |
3e43b647 | 1162 | "dghHmMsSy" |
d98a58c5 | 1163 | #ifdef __WINDOWS__ |
3e43b647 | 1164 | "t" |
47029843 SC |
1165 | #else |
1166 | "EawD" | |
3e43b647 SC |
1167 | #endif |
1168 | ; | |
89a7e1ff VZ |
1169 | for ( wxString::const_iterator p = fmt.begin(); /* end handled inside */; ++p ) |
1170 | { | |
1171 | if ( p != fmt.end() ) | |
1172 | { | |
1173 | if ( *p == chLast ) | |
1174 | { | |
1175 | lastCount++; | |
1176 | continue; | |
1177 | } | |
1178 | ||
1179 | const wxUniChar ch = (*p).GetValue(); | |
3e43b647 | 1180 | if ( ch.IsAscii() && strchr(formatchars, ch) ) |
89a7e1ff VZ |
1181 | { |
1182 | // these characters come in groups, start counting them | |
1183 | chLast = ch; | |
1184 | lastCount = 1; | |
1185 | continue; | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | // interpret any special characters we collected so far | |
1190 | if ( lastCount ) | |
1191 | { | |
1192 | switch ( chLast ) | |
1193 | { | |
1194 | case 'd': | |
1195 | switch ( lastCount ) | |
1196 | { | |
1197 | case 1: // d | |
1198 | case 2: // dd | |
1199 | // these two are the same as we don't distinguish | |
1200 | // between 1 and 2 digits for days | |
1201 | fmtWX += "%d"; | |
1202 | break; | |
d98a58c5 | 1203 | #ifdef __WINDOWS__ |
89a7e1ff VZ |
1204 | case 3: // ddd |
1205 | fmtWX += "%a"; | |
1206 | break; | |
1207 | ||
1208 | case 4: // dddd | |
1209 | fmtWX += "%A"; | |
1210 | break; | |
47029843 | 1211 | #endif |
89a7e1ff VZ |
1212 | default: |
1213 | wxFAIL_MSG( "too many 'd's" ); | |
1214 | } | |
1215 | break; | |
d98a58c5 | 1216 | #ifndef __WINDOWS__ |
47029843 SC |
1217 | case 'D': |
1218 | switch ( lastCount ) | |
1219 | { | |
1220 | case 1: // D | |
1221 | case 2: // DD | |
1222 | case 3: // DDD | |
1223 | fmtWX += "%j"; | |
1224 | break; | |
1225 | ||
1226 | default: | |
1227 | wxFAIL_MSG( "wrong number of 'D's" ); | |
1228 | } | |
1229 | break; | |
1230 | case 'w': | |
1231 | switch ( lastCount ) | |
1232 | { | |
1233 | case 1: // w | |
1234 | case 2: // ww | |
1235 | fmtWX += "%W"; | |
1236 | break; | |
1237 | ||
1238 | default: | |
1239 | wxFAIL_MSG( "wrong number of 'w's" ); | |
1240 | } | |
1241 | break; | |
1242 | case 'E': | |
1243 | switch ( lastCount ) | |
1244 | { | |
1245 | case 1: // E | |
1246 | case 2: // EE | |
1247 | case 3: // EEE | |
1248 | fmtWX += "%a"; | |
1249 | break; | |
1250 | case 4: // EEEE | |
1251 | fmtWX += "%A"; | |
1252 | break; | |
1253 | case 5: // EEEEE | |
1254 | fmtWX += "%a"; | |
1255 | break; | |
89a7e1ff | 1256 | |
47029843 SC |
1257 | default: |
1258 | wxFAIL_MSG( "wrong number of 'E's" ); | |
1259 | } | |
1260 | break; | |
1261 | #endif | |
89a7e1ff VZ |
1262 | case 'M': |
1263 | switch ( lastCount ) | |
1264 | { | |
1265 | case 1: // M | |
1266 | case 2: // MM | |
1267 | // as for 'd' and 'dd' above | |
1268 | fmtWX += "%m"; | |
1269 | break; | |
1270 | ||
1271 | case 3: | |
1272 | fmtWX += "%b"; | |
1273 | break; | |
1274 | ||
1275 | case 4: | |
1276 | fmtWX += "%B"; | |
1277 | break; | |
1278 | ||
1279 | default: | |
1280 | wxFAIL_MSG( "too many 'M's" ); | |
1281 | } | |
1282 | break; | |
1283 | ||
1284 | case 'y': | |
1285 | switch ( lastCount ) | |
1286 | { | |
1287 | case 1: // y | |
1288 | case 2: // yy | |
1289 | fmtWX += "%y"; | |
1290 | break; | |
1291 | ||
1292 | case 4: // yyyy | |
1293 | fmtWX += "%Y"; | |
1294 | break; | |
1295 | ||
1296 | default: | |
1297 | wxFAIL_MSG( "wrong number of 'y's" ); | |
1298 | } | |
1299 | break; | |
1300 | ||
1301 | case 'H': | |
1302 | switch ( lastCount ) | |
1303 | { | |
1304 | case 1: // H | |
1305 | case 2: // HH | |
1306 | fmtWX += "%H"; | |
1307 | break; | |
1308 | ||
1309 | default: | |
1310 | wxFAIL_MSG( "wrong number of 'H's" ); | |
1311 | } | |
1312 | break; | |
1313 | ||
1314 | case 'h': | |
1315 | switch ( lastCount ) | |
1316 | { | |
1317 | case 1: // h | |
1318 | case 2: // hh | |
756b5a98 | 1319 | fmtWX += "%I"; |
89a7e1ff VZ |
1320 | break; |
1321 | ||
1322 | default: | |
1323 | wxFAIL_MSG( "wrong number of 'h's" ); | |
1324 | } | |
1325 | break; | |
1326 | ||
1327 | case 'm': | |
1328 | switch ( lastCount ) | |
1329 | { | |
1330 | case 1: // m | |
1331 | case 2: // mm | |
1332 | fmtWX += "%M"; | |
1333 | break; | |
1334 | ||
1335 | default: | |
1336 | wxFAIL_MSG( "wrong number of 'm's" ); | |
1337 | } | |
1338 | break; | |
1339 | ||
1340 | case 's': | |
1341 | switch ( lastCount ) | |
1342 | { | |
1343 | case 1: // s | |
1344 | case 2: // ss | |
1345 | fmtWX += "%S"; | |
1346 | break; | |
1347 | ||
1348 | default: | |
1349 | wxFAIL_MSG( "wrong number of 's's" ); | |
1350 | } | |
1351 | break; | |
1352 | ||
1353 | case 'g': | |
1354 | // strftime() doesn't have era string, | |
1355 | // ignore this format | |
1356 | wxASSERT_MSG( lastCount <= 2, "too many 'g's" ); | |
03647350 | 1357 | |
89a7e1ff | 1358 | break; |
d98a58c5 | 1359 | #ifndef __WINDOWS__ |
3e43b647 SC |
1360 | case 'a': |
1361 | fmtWX += "%p"; | |
1362 | break; | |
47029843 | 1363 | #endif |
d98a58c5 | 1364 | #ifdef __WINDOWS__ |
3e43b647 SC |
1365 | case 't': |
1366 | switch ( lastCount ) | |
1367 | { | |
1368 | case 1: // t | |
1369 | case 2: // tt | |
1370 | fmtWX += "%p"; | |
1371 | break; | |
1372 | ||
1373 | default: | |
1374 | wxFAIL_MSG( "too many 't's" ); | |
1375 | } | |
1376 | break; | |
1377 | #endif | |
89a7e1ff VZ |
1378 | default: |
1379 | wxFAIL_MSG( "unreachable" ); | |
1380 | } | |
1381 | ||
1382 | chLast = '\0'; | |
1383 | lastCount = 0; | |
1384 | } | |
1385 | ||
1386 | if ( p == fmt.end() ) | |
1387 | break; | |
1388 | ||
1389 | // not a special character so must be just a separator, treat as is | |
9a83f860 | 1390 | if ( *p == wxT('%') ) |
89a7e1ff VZ |
1391 | { |
1392 | // this one needs to be escaped | |
9a83f860 | 1393 | fmtWX += wxT('%'); |
89a7e1ff VZ |
1394 | } |
1395 | ||
1396 | fmtWX += *p; | |
1397 | } | |
1398 | ||
1399 | return fmtWX; | |
1400 | } | |
1401 | ||
1402 | } // anonymous namespace | |
1403 | ||
d98a58c5 | 1404 | #endif // __WINDOWS__ || __WXOSX__ |
89a7e1ff | 1405 | |
d98a58c5 | 1406 | #if defined(__WINDOWS__) |
cfb20656 | 1407 | |
89a7e1ff VZ |
1408 | namespace |
1409 | { | |
1410 | ||
1411 | LCTYPE GetLCTYPEFormatFromLocalInfo(wxLocaleInfo index) | |
1412 | { | |
1413 | switch ( index ) | |
1414 | { | |
1415 | case wxLOCALE_SHORT_DATE_FMT: | |
1416 | return LOCALE_SSHORTDATE; | |
1417 | ||
1418 | case wxLOCALE_LONG_DATE_FMT: | |
1419 | return LOCALE_SLONGDATE; | |
1420 | ||
1421 | case wxLOCALE_TIME_FMT: | |
1422 | return LOCALE_STIMEFORMAT; | |
1423 | ||
1424 | default: | |
1425 | wxFAIL_MSG( "no matching LCTYPE" ); | |
1426 | } | |
1427 | ||
1428 | return 0; | |
1429 | } | |
1430 | ||
1431 | } // anonymous namespace | |
1432 | ||
cfb20656 | 1433 | /* static */ |
53250e3a | 1434 | wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat) |
cfb20656 | 1435 | { |
7582edfb VZ |
1436 | const wxLanguageInfo * const |
1437 | info = wxGetLocale() ? GetLanguageInfo(wxGetLocale()->GetLanguage()) | |
1438 | : NULL; | |
1439 | if ( !info ) | |
529e125d | 1440 | { |
7582edfb VZ |
1441 | // wxSetLocale() hadn't been called yet of failed, hence CRT must be |
1442 | // using "C" locale -- but check it to detect bugs that would happen if | |
1443 | // this were not the case. | |
1444 | wxASSERT_MSG( strcmp(setlocale(LC_ALL, NULL), "C") == 0, | |
1445 | wxS("You probably called setlocale() directly instead ") | |
49211791 | 1446 | wxS("of using wxLocale and now there is a ") |
7582edfb | 1447 | wxS("mismatch between C/C++ and Windows locale.\n") |
49211791 VZ |
1448 | wxS("Things are going to break, please only change ") |
1449 | wxS("locale by creating wxLocale objects to avoid this!") ); | |
7582edfb VZ |
1450 | |
1451 | ||
1452 | // Return the hard coded values for C locale. This is really the right | |
1453 | // thing to do as there is no LCID we can use in the code below in this | |
1454 | // case, even LOCALE_INVARIANT is not quite the same as C locale (the | |
1455 | // only difference is that it uses %Y instead of %y in the date format | |
1456 | // but this difference is significant enough). | |
1457 | switch ( index ) | |
1458 | { | |
1459 | case wxLOCALE_THOUSANDS_SEP: | |
1460 | return wxString(); | |
1461 | ||
1462 | case wxLOCALE_DECIMAL_POINT: | |
1463 | return "."; | |
1464 | ||
1465 | case wxLOCALE_SHORT_DATE_FMT: | |
1466 | return "%m/%d/%y"; | |
1467 | ||
1468 | case wxLOCALE_LONG_DATE_FMT: | |
1469 | return "%A, %B %d, %Y"; | |
1470 | ||
1471 | case wxLOCALE_TIME_FMT: | |
1472 | return "%H:%M:%S"; | |
1473 | ||
1474 | case wxLOCALE_DATE_TIME_FMT: | |
1475 | return "%m/%d/%y %H:%M:%S"; | |
1476 | ||
1477 | default: | |
1478 | wxFAIL_MSG( "unknown wxLocaleInfo" ); | |
1479 | } | |
529e125d JS |
1480 | } |
1481 | ||
7582edfb VZ |
1482 | const wxUint32 lcid = info->GetLCID(); |
1483 | ||
cfb20656 | 1484 | wxString str; |
89a7e1ff VZ |
1485 | |
1486 | wxChar buf[256]; | |
1487 | buf[0] = wxT('\0'); | |
1488 | ||
1489 | switch ( index ) | |
cfb20656 | 1490 | { |
f4f3c8f3 VZ |
1491 | case wxLOCALE_THOUSANDS_SEP: |
1492 | if ( ::GetLocaleInfo(lcid, LOCALE_STHOUSAND, buf, WXSIZEOF(buf)) ) | |
1493 | str = buf; | |
1494 | break; | |
1495 | ||
60d876f3 | 1496 | case wxLOCALE_DECIMAL_POINT: |
53250e3a VZ |
1497 | if ( ::GetLocaleInfo(lcid, |
1498 | cat == wxLOCALE_CAT_MONEY | |
1499 | ? LOCALE_SMONDECIMALSEP | |
1500 | : LOCALE_SDECIMAL, | |
1501 | buf, | |
1502 | WXSIZEOF(buf)) ) | |
0043f420 | 1503 | { |
89a7e1ff | 1504 | str = buf; |
0043f420 VZ |
1505 | |
1506 | // As we get our decimal point separator from Win32 and not the | |
1507 | // CRT there is a possibility of mismatch between them and this | |
1508 | // can easily happen if the user code called setlocale() | |
1509 | // instead of using wxLocale to change the locale. And this can | |
1510 | // result in very strange bugs elsewhere in the code as the | |
1511 | // assumptions that formatted strings do use the decimal | |
1512 | // separator actually fail, so check for it here. | |
1513 | wxASSERT_MSG | |
1514 | ( | |
1515 | wxString::Format("%.3f", 1.23).find(str) != wxString::npos, | |
1516 | "Decimal separator mismatch -- did you use setlocale()?" | |
1517 | "If so, use wxLocale to change the locale instead." | |
1518 | ); | |
1519 | } | |
cfb20656 | 1520 | break; |
89a7e1ff VZ |
1521 | |
1522 | case wxLOCALE_SHORT_DATE_FMT: | |
1523 | case wxLOCALE_LONG_DATE_FMT: | |
1524 | case wxLOCALE_TIME_FMT: | |
1525 | if ( ::GetLocaleInfo(lcid, GetLCTYPEFormatFromLocalInfo(index), | |
1526 | buf, WXSIZEOF(buf)) ) | |
1527 | { | |
1528 | return TranslateFromUnicodeFormat(buf); | |
1529 | } | |
cfb20656 | 1530 | break; |
89a7e1ff VZ |
1531 | |
1532 | case wxLOCALE_DATE_TIME_FMT: | |
1533 | // there doesn't seem to be any specific setting for this, so just | |
1534 | // combine date and time ones | |
cd8a8b45 VZ |
1535 | // |
1536 | // we use the short date because this is what "%c" uses by default | |
1537 | // ("%#c" uses long date but we have no way to specify the | |
1538 | // alternate representation here) | |
89a7e1ff | 1539 | { |
cd8a8b45 | 1540 | const wxString datefmt = GetInfo(wxLOCALE_SHORT_DATE_FMT); |
89a7e1ff VZ |
1541 | if ( datefmt.empty() ) |
1542 | break; | |
1543 | ||
1544 | const wxString timefmt = GetInfo(wxLOCALE_TIME_FMT); | |
1545 | if ( timefmt.empty() ) | |
1546 | break; | |
1547 | ||
1548 | str << datefmt << ' ' << timefmt; | |
1549 | } | |
cfb20656 | 1550 | break; |
89a7e1ff | 1551 | |
cfb20656 | 1552 | default: |
89a7e1ff | 1553 | wxFAIL_MSG( "unknown wxLocaleInfo" ); |
cfb20656 | 1554 | } |
89a7e1ff | 1555 | |
cfb20656 VZ |
1556 | return str; |
1557 | } | |
1558 | ||
89a7e1ff | 1559 | #elif defined(__WXOSX__) |
dff6cf57 VZ |
1560 | |
1561 | /* static */ | |
1562 | wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory WXUNUSED(cat)) | |
1563 | { | |
82de050c VZ |
1564 | CFLocaleRef userLocaleRefRaw; |
1565 | if ( wxGetLocale() ) | |
1566 | { | |
1567 | userLocaleRefRaw = CFLocaleCreate | |
ca642c8a | 1568 | ( |
82de050c VZ |
1569 | kCFAllocatorDefault, |
1570 | wxCFStringRef(wxGetLocale()->GetCanonicalName()) | |
ca642c8a | 1571 | ); |
82de050c VZ |
1572 | } |
1573 | else // no current locale, use the default one | |
1574 | { | |
1575 | userLocaleRefRaw = CFLocaleCopyCurrent(); | |
1576 | } | |
1577 | ||
1578 | wxCFRef<CFLocaleRef> userLocaleRef(userLocaleRefRaw); | |
1579 | ||
73b51ea4 | 1580 | CFStringRef cfstr = 0; |
dff6cf57 VZ |
1581 | switch ( index ) |
1582 | { | |
1583 | case wxLOCALE_THOUSANDS_SEP: | |
73b51ea4 | 1584 | cfstr = (CFStringRef) CFLocaleGetValue(userLocaleRef, kCFLocaleGroupingSeparator); |
dff6cf57 VZ |
1585 | break; |
1586 | ||
1587 | case wxLOCALE_DECIMAL_POINT: | |
73b51ea4 | 1588 | cfstr = (CFStringRef) CFLocaleGetValue(userLocaleRef, kCFLocaleDecimalSeparator); |
dff6cf57 VZ |
1589 | break; |
1590 | ||
89a7e1ff VZ |
1591 | case wxLOCALE_SHORT_DATE_FMT: |
1592 | case wxLOCALE_LONG_DATE_FMT: | |
1593 | case wxLOCALE_DATE_TIME_FMT: | |
1594 | case wxLOCALE_TIME_FMT: | |
e9e53eb1 SC |
1595 | { |
1596 | CFDateFormatterStyle dateStyle = kCFDateFormatterNoStyle; | |
1597 | CFDateFormatterStyle timeStyle = kCFDateFormatterNoStyle; | |
1598 | switch (index ) | |
1599 | { | |
1600 | case wxLOCALE_SHORT_DATE_FMT: | |
47029843 | 1601 | dateStyle = kCFDateFormatterShortStyle; |
e9e53eb1 SC |
1602 | break; |
1603 | case wxLOCALE_LONG_DATE_FMT: | |
9fbcc679 | 1604 | dateStyle = kCFDateFormatterFullStyle; |
e9e53eb1 SC |
1605 | break; |
1606 | case wxLOCALE_DATE_TIME_FMT: | |
47029843 | 1607 | dateStyle = kCFDateFormatterFullStyle; |
e9e53eb1 SC |
1608 | timeStyle = kCFDateFormatterMediumStyle; |
1609 | break; | |
1610 | case wxLOCALE_TIME_FMT: | |
1611 | timeStyle = kCFDateFormatterMediumStyle; | |
1612 | break; | |
1613 | default: | |
1614 | wxFAIL_MSG( "unexpected time locale" ); | |
1615 | return wxString(); | |
1616 | } | |
1617 | wxCFRef<CFDateFormatterRef> dateFormatter( CFDateFormatterCreate | |
1618 | (NULL, userLocaleRef, dateStyle, timeStyle)); | |
1619 | wxCFStringRef cfs = wxCFRetain( CFDateFormatterGetFormat(dateFormatter )); | |
47029843 SC |
1620 | wxString format = TranslateFromUnicodeFormat(cfs.AsString()); |
1621 | // we always want full years | |
1622 | format.Replace("%y","%Y"); | |
1623 | return format; | |
e9e53eb1 SC |
1624 | } |
1625 | break; | |
89a7e1ff | 1626 | |
dff6cf57 VZ |
1627 | default: |
1628 | wxFAIL_MSG( "Unknown locale info" ); | |
89a7e1ff | 1629 | return wxString(); |
dff6cf57 VZ |
1630 | } |
1631 | ||
73b51ea4 | 1632 | wxCFStringRef str(wxCFRetain(cfstr)); |
dff6cf57 VZ |
1633 | return str.AsString(); |
1634 | } | |
1635 | ||
d98a58c5 | 1636 | #else // !__WINDOWS__ && !__WXOSX__, assume generic POSIX |
89a7e1ff VZ |
1637 | |
1638 | namespace | |
1639 | { | |
1640 | ||
1641 | wxString GetDateFormatFromLangInfo(wxLocaleInfo index) | |
1642 | { | |
1643 | #ifdef HAVE_LANGINFO_H | |
1644 | // array containing parameters for nl_langinfo() indexes by offset of index | |
1645 | // from wxLOCALE_SHORT_DATE_FMT | |
1646 | static const nl_item items[] = | |
1647 | { | |
1648 | D_FMT, D_T_FMT, D_T_FMT, T_FMT, | |
1649 | }; | |
1650 | ||
1651 | const int nlidx = index - wxLOCALE_SHORT_DATE_FMT; | |
1652 | if ( nlidx < 0 || nlidx >= (int)WXSIZEOF(items) ) | |
1653 | { | |
1654 | wxFAIL_MSG( "logic error in GetInfo() code" ); | |
1655 | return wxString(); | |
1656 | } | |
1657 | ||
1658 | const wxString fmt(nl_langinfo(items[nlidx])); | |
1659 | ||
1660 | // just return the format returned by nl_langinfo() except for long date | |
1661 | // format which we need to recover from date/time format ourselves (but not | |
1662 | // if we failed completely) | |
1663 | if ( fmt.empty() || index != wxLOCALE_LONG_DATE_FMT ) | |
1664 | return fmt; | |
1665 | ||
1666 | // this is not 100% precise but the idea is that a typical date/time format | |
1667 | // under POSIX systems is a combination of a long date format with time one | |
1668 | // so we should be able to get just the long date format by removing all | |
1669 | // time-specific format specifiers | |
1670 | static const char *timeFmtSpecs = "HIklMpPrRsSTXzZ"; | |
1671 | static const char *timeSep = " :./-"; | |
1672 | ||
1673 | wxString fmtDateOnly; | |
1674 | const wxString::const_iterator end = fmt.end(); | |
1675 | wxString::const_iterator lastSep = end; | |
1676 | for ( wxString::const_iterator p = fmt.begin(); p != end; ++p ) | |
1677 | { | |
1678 | if ( strchr(timeSep, *p) ) | |
1679 | { | |
1680 | if ( lastSep == end ) | |
1681 | lastSep = p; | |
1682 | ||
1683 | // skip it for now, we'll discard it if it's followed by a time | |
1684 | // specifier later or add it to fmtDateOnly if it is not | |
1685 | continue; | |
1686 | } | |
1687 | ||
1688 | if ( *p == '%' && | |
1689 | (p + 1 != end) && strchr(timeFmtSpecs, p[1]) ) | |
1690 | { | |
1691 | // time specified found: skip it and any preceding separators | |
1692 | ++p; | |
1693 | lastSep = end; | |
1694 | continue; | |
1695 | } | |
1696 | ||
1697 | if ( lastSep != end ) | |
1698 | { | |
1699 | fmtDateOnly += wxString(lastSep, p); | |
1700 | lastSep = end; | |
1701 | } | |
1702 | ||
1703 | fmtDateOnly += *p; | |
1704 | } | |
1705 | ||
1706 | return fmtDateOnly; | |
1707 | #else // !HAVE_LANGINFO_H | |
c5147528 VZ |
1708 | wxUnusedVar(index); |
1709 | ||
89a7e1ff VZ |
1710 | // no fallback, let the application deal with unavailability of |
1711 | // nl_langinfo() itself as there is no good way for us to do it (well, we | |
1712 | // could try to reverse engineer the format from strftime() output but this | |
1713 | // looks like too much trouble considering the relatively small number of | |
1714 | // systems without nl_langinfo() still in use) | |
1715 | return wxString(); | |
1716 | #endif // HAVE_LANGINFO_H/!HAVE_LANGINFO_H | |
1717 | } | |
1718 | ||
1719 | } // anonymous namespace | |
cfb20656 VZ |
1720 | |
1721 | /* static */ | |
60d876f3 | 1722 | wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat) |
cfb20656 | 1723 | { |
f3342781 VZ |
1724 | lconv * const lc = localeconv(); |
1725 | if ( !lc ) | |
1726 | return wxString(); | |
1727 | ||
89a7e1ff | 1728 | switch ( index ) |
60d876f3 | 1729 | { |
89a7e1ff VZ |
1730 | case wxLOCALE_THOUSANDS_SEP: |
1731 | if ( cat == wxLOCALE_CAT_NUMBER ) | |
1732 | return lc->thousands_sep; | |
1733 | else if ( cat == wxLOCALE_CAT_MONEY ) | |
1734 | return lc->mon_thousands_sep; | |
f3342781 | 1735 | |
89a7e1ff | 1736 | wxFAIL_MSG( "invalid wxLocaleCategory" ); |
f3342781 VZ |
1737 | break; |
1738 | ||
f3342781 | 1739 | |
89a7e1ff VZ |
1740 | case wxLOCALE_DECIMAL_POINT: |
1741 | if ( cat == wxLOCALE_CAT_NUMBER ) | |
1742 | return lc->decimal_point; | |
1743 | else if ( cat == wxLOCALE_CAT_MONEY ) | |
1744 | return lc->mon_decimal_point; | |
1745 | ||
1746 | wxFAIL_MSG( "invalid wxLocaleCategory" ); | |
f3342781 VZ |
1747 | break; |
1748 | ||
89a7e1ff VZ |
1749 | case wxLOCALE_SHORT_DATE_FMT: |
1750 | case wxLOCALE_LONG_DATE_FMT: | |
1751 | case wxLOCALE_DATE_TIME_FMT: | |
1752 | case wxLOCALE_TIME_FMT: | |
1753 | if ( cat != wxLOCALE_CAT_DATE && cat != wxLOCALE_CAT_DEFAULT ) | |
1754 | { | |
1755 | wxFAIL_MSG( "invalid wxLocaleCategory" ); | |
1756 | break; | |
1757 | } | |
1758 | ||
1759 | return GetDateFormatFromLangInfo(index); | |
1760 | ||
1761 | ||
60d876f3 | 1762 | default: |
89a7e1ff | 1763 | wxFAIL_MSG( "unknown wxLocaleInfo value" ); |
60d876f3 | 1764 | } |
f3342781 | 1765 | |
f3342781 | 1766 | return wxString(); |
b7b97e77 | 1767 | } |
cfb20656 | 1768 | |
dff6cf57 | 1769 | #endif // platform |
cfb20656 | 1770 | |
c801d85f KB |
1771 | // ---------------------------------------------------------------------------- |
1772 | // global functions and variables | |
1773 | // ---------------------------------------------------------------------------- | |
1774 | ||
c801d85f KB |
1775 | // retrieve/change current locale |
1776 | // ------------------------------ | |
1777 | ||
1778 | // the current locale object | |
84c18814 | 1779 | static wxLocale *g_pLocale = NULL; |
c801d85f | 1780 | |
1678ad78 GL |
1781 | wxLocale *wxGetLocale() |
1782 | { | |
ca642c8a | 1783 | return g_pLocale; |
1678ad78 GL |
1784 | } |
1785 | ||
c801d85f KB |
1786 | wxLocale *wxSetLocale(wxLocale *pLocale) |
1787 | { | |
ca642c8a FM |
1788 | wxLocale *pOld = g_pLocale; |
1789 | g_pLocale = pLocale; | |
1790 | return pOld; | |
c801d85f | 1791 | } |
d427503c | 1792 | |
41780009 VS |
1793 | |
1794 | ||
1795 | // ---------------------------------------------------------------------------- | |
1796 | // wxLocale module (for lazy destruction of languagesDB) | |
1797 | // ---------------------------------------------------------------------------- | |
1798 | ||
1799 | class wxLocaleModule: public wxModule | |
1800 | { | |
1801 | DECLARE_DYNAMIC_CLASS(wxLocaleModule) | |
1802 | public: | |
1803 | wxLocaleModule() {} | |
18e065b4 VS |
1804 | |
1805 | bool OnInit() | |
1806 | { | |
1807 | return true; | |
1808 | } | |
1809 | ||
1810 | void OnExit() | |
1811 | { | |
18e065b4 VS |
1812 | wxLocale::DestroyLanguagesDB(); |
1813 | } | |
41780009 VS |
1814 | }; |
1815 | ||
1816 | IMPLEMENT_DYNAMIC_CLASS(wxLocaleModule, wxModule) | |
1817 | ||
d427503c | 1818 | #endif // wxUSE_INTL |