]> git.saurik.com Git - wxWidgets.git/blob - src/common/intl.cpp
No changes, just use wxRecursionGuard instead of manual boolean flag.
[wxWidgets.git] / src / common / intl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/intl.cpp
3 // Purpose: Internationalization and localisation for wxWidgets
4 // Author: Vadim Zeitlin
5 // Modified by: Michael N. Filippov <michael@idisys.iae.nsk.su>
6 // (2003/09/30 - PluralForms support)
7 // Created: 29/01/98
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declaration
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
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
33 #if wxUSE_INTL
34
35 #ifndef WX_PRECOMP
36 #include "wx/dynarray.h"
37 #include "wx/string.h"
38 #include "wx/intl.h"
39 #include "wx/log.h"
40 #include "wx/utils.h"
41 #include "wx/app.h"
42 #include "wx/hashmap.h"
43 #include "wx/module.h"
44 #endif // WX_PRECOMP
45
46 #ifndef __WXWINCE__
47 #include <locale.h>
48 #endif
49
50 // standard headers
51 #include <ctype.h>
52 #include <stdlib.h>
53 #ifdef HAVE_LANGINFO_H
54 #include <langinfo.h>
55 #endif
56
57 #ifdef __WIN32__
58 #include "wx/msw/private.h"
59 #endif
60
61 #include "wx/file.h"
62 #include "wx/filename.h"
63 #include "wx/tokenzr.h"
64 #include "wx/fontmap.h"
65 #include "wx/scopedptr.h"
66 #include "wx/apptrait.h"
67 #include "wx/stdpaths.h"
68 #include "wx/hashset.h"
69
70 #if defined(__WXOSX__)
71 #include "wx/osx/core/cfref.h"
72 #include <CoreFoundation/CFLocale.h>
73 #include <CoreFoundation/CFDateFormatter.h>
74 #include "wx/osx/core/cfstring.h"
75 #endif
76
77 // ----------------------------------------------------------------------------
78 // constants
79 // ----------------------------------------------------------------------------
80
81 #define TRACE_I18N wxS("i18n")
82
83 // ============================================================================
84 // implementation
85 // ============================================================================
86
87 // ----------------------------------------------------------------------------
88 // global functions
89 // ----------------------------------------------------------------------------
90
91 static wxLocale *wxSetLocale(wxLocale *pLocale);
92
93 namespace
94 {
95
96 // get just the language part ("en" in "en_GB")
97 inline wxString ExtractLang(const wxString& langFull)
98 {
99 return langFull.BeforeFirst('_');
100 }
101
102 // helper functions of GetSystemLanguage()
103 #ifdef __UNIX__
104
105 // get everything else (including the leading '_')
106 inline wxString ExtractNotLang(const wxString& langFull)
107 {
108 size_t pos = langFull.find('_');
109 if ( pos != wxString::npos )
110 return langFull.substr(pos);
111 else
112 return wxString();
113 }
114
115 #endif // __UNIX__
116
117 } // anonymous namespace
118
119 // ----------------------------------------------------------------------------
120 // wxLanguageInfo
121 // ----------------------------------------------------------------------------
122
123 #ifdef __WINDOWS__
124
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;
131
132 wxChar buffer[16];
133 if ( ::GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE,
134 buffer, WXSIZEOF(buffer)) > 0 )
135 {
136 if ( buffer[0] != wxT('0') || buffer[1] != wxT('\0') )
137 cp = buffer;
138 //else: this locale doesn't use ANSI code page
139 }
140
141 return cp;
142 }
143
144 wxUint32 wxLanguageInfo::GetLCID() const
145 {
146 return MAKELCID(MAKELANGID(WinLang, WinSublang), SORT_DEFAULT);
147 }
148
149 wxString wxLanguageInfo::GetLocaleName() const
150 {
151 wxString locale;
152
153 const LCID lcid = GetLCID();
154
155 wxChar buffer[256];
156 buffer[0] = wxT('\0');
157 if ( !::GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE, buffer, WXSIZEOF(buffer)) )
158 {
159 wxLogLastError(wxT("GetLocaleInfo(LOCALE_SENGLANGUAGE)"));
160 return locale;
161 }
162
163 locale << buffer;
164 if ( ::GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY,
165 buffer, WXSIZEOF(buffer)) > 0 )
166 {
167 locale << wxT('_') << buffer;
168 }
169
170 const wxString cp = wxGetANSICodePageForLocale(lcid);
171 if ( !cp.empty() )
172 {
173 locale << wxT('.') << cp;
174 }
175
176 return locale;
177 }
178
179 #endif // __WINDOWS__
180
181 // ----------------------------------------------------------------------------
182 // wxLocale
183 // ----------------------------------------------------------------------------
184
185 #include "wx/arrimpl.cpp"
186 WX_DECLARE_EXPORTED_OBJARRAY(wxLanguageInfo, wxLanguageInfoArray);
187 WX_DEFINE_OBJARRAY(wxLanguageInfoArray)
188
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 {
202 wxDELETE(ms_languagesDB);
203 }
204
205
206 void wxLocale::DoCommonInit()
207 {
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
213
214 m_pOldLocale = wxSetLocale(this);
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 }
233
234 m_language = wxLANGUAGE_UNKNOWN;
235 m_initialized = false;
236 }
237
238 // NB: this function has (desired) side effect of changing current locale
239 bool wxLocale::Init(const wxString& name,
240 const wxString& shortName,
241 const wxString& locale,
242 bool bLoadDefault
243 #if WXWIN_COMPATIBILITY_2_8
244 ,bool WXUNUSED_UNLESS_DEBUG(bConvertEncoding)
245 #endif
246 )
247 {
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
253 bool ret = DoInit(name, shortName, locale);
254
255 // NB: don't use 'lang' here, 'language' may be wxLANGUAGE_DEFAULT
256 wxTranslations *t = wxTranslations::Get();
257 if ( t )
258 {
259 t->SetLanguage(shortName);
260
261 if ( bLoadDefault )
262 t->AddStdCatalog();
263 }
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
275 m_initialized = true;
276 m_strLocale = name;
277 m_strShort = shortName;
278 m_language = wxLANGUAGE_UNKNOWN;
279
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;
286
287 wxCHECK_MSG( !szLocale.empty(), false,
288 wxS("no locale to set in wxLocale::Init()") );
289 }
290
291 if ( !wxSetlocale(LC_ALL, szLocale) )
292 {
293 wxLogError(_("locale '%s' cannot be set."), szLocale);
294 }
295
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 }
307 }
308
309 return true;
310 }
311
312
313 #if defined(__UNIX__) && wxUSE_UNICODE && !defined(__WXMAC__)
314 static const char *wxSetlocaleTryUTF8(int c, const wxString& lc)
315 {
316 const char *l = NULL;
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
321 if ( !lc.empty() )
322 {
323 wxString buf(lc);
324 wxString buf2;
325 buf2 = buf + wxS(".UTF-8");
326 l = wxSetlocale(c, buf2);
327 if ( !l )
328 {
329 buf2 = buf + wxS(".utf-8");
330 l = wxSetlocale(c, buf2);
331 }
332 if ( !l )
333 {
334 buf2 = buf + wxS(".UTF8");
335 l = wxSetlocale(c, buf2);
336 }
337 if ( !l )
338 {
339 buf2 = buf + wxS(".utf8");
340 l = wxSetlocale(c, buf2);
341 }
342 }
343
344 // if we can't set UTF-8 locale, try non-UTF-8 one:
345 if ( !l )
346 l = wxSetlocale(c, lc);
347
348 return l;
349 }
350 #else
351 #define wxSetlocaleTryUTF8(c, lc) wxSetlocale(c, lc)
352 #endif
353
354 bool wxLocale::Init(int language, int flags)
355 {
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
361 bool ret = true;
362
363 int lang = language;
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 {
373 return false;
374 }
375
376 const wxLanguageInfo *info = GetLanguageInfo(lang);
377
378 // Unknown language:
379 if (info == NULL)
380 {
381 wxLogError(wxS("Unknown language %i."), lang);
382 return false;
383 }
384
385 wxString name = info->Description;
386 wxString canonical = info->CanonicalName;
387 wxString locale;
388
389 // Set the locale:
390 #if defined(__OS2__)
391 const char *retloc = wxSetlocale(LC_ALL , wxEmptyString);
392 #elif defined(__UNIX__) && !defined(__WXMAC__)
393 if (language != wxLANGUAGE_DEFAULT)
394 locale = info->CanonicalName;
395
396 const char *retloc = wxSetlocaleTryUTF8(LC_ALL, locale);
397
398 const wxString langOnly = ExtractLang(locale);
399 if ( !retloc )
400 {
401 // Some C libraries don't like xx_YY form and require xx only
402 retloc = wxSetlocaleTryUTF8(LC_ALL, langOnly);
403 }
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
412 if ( !retloc )
413 {
414 const wxChar **names =
415 wxFontMapperBase::GetAllEncodingNames(wxFONTENCODING_UTF8);
416 while ( *names )
417 {
418 retloc = wxSetlocale(LC_ALL, locale + wxS('.') + *names++);
419 if ( retloc )
420 break;
421 }
422 }
423 #endif // wxUSE_FONTMAP
424
425 if ( !retloc )
426 {
427 // Some C libraries (namely glibc) still use old ISO 639,
428 // so will translate the abbrev for them
429 wxString localeAlt;
430 if ( langOnly == wxS("he") )
431 localeAlt = wxS("iw") + ExtractNotLang(locale);
432 else if ( langOnly == wxS("id") )
433 localeAlt = wxS("in") + ExtractNotLang(locale);
434 else if ( langOnly == wxS("yi") )
435 localeAlt = wxS("ji") + ExtractNotLang(locale);
436 else if ( langOnly == wxS("nb") )
437 localeAlt = wxS("no_NO");
438 else if ( langOnly == wxS("nn") )
439 localeAlt = wxS("no_NY");
440
441 if ( !localeAlt.empty() )
442 {
443 retloc = wxSetlocaleTryUTF8(LC_ALL, localeAlt);
444 if ( !retloc )
445 retloc = wxSetlocaleTryUTF8(LC_ALL, ExtractLang(localeAlt));
446 }
447 }
448
449 if ( !retloc )
450 ret = false;
451
452 #ifdef __AIX__
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"
457 //
458 // this contradicts IBM own docs but this is not of much help, so just work
459 // around it in the crudest possible manner
460 char* p = const_cast<char*>(wxStrchr(retloc, ' '));
461 if ( p )
462 *p = '\0';
463 #endif // __AIX__
464
465 #elif defined(__WIN32__)
466 const char *retloc = "C";
467 if ( language != wxLANGUAGE_DEFAULT )
468 {
469 if ( info->WinLang == 0 )
470 {
471 wxLogWarning(wxS("Locale '%s' not supported by OS."), name.c_str());
472 // retloc already set to "C"
473 }
474 else // language supported by Windows
475 {
476 // Windows CE doesn't have SetThreadLocale() and there doesn't seem
477 // to be any equivalent
478 #ifndef __WXWINCE__
479 const wxUint32 lcid = info->GetLCID();
480
481 // change locale used by Windows functions
482 ::SetThreadLocale(lcid);
483 #endif
484
485 // and also call setlocale() to change locale used by the CRT
486 locale = info->GetLocaleName();
487 if ( locale.empty() )
488 {
489 ret = false;
490 }
491 else // have a valid locale
492 {
493 retloc = wxSetlocale(LC_ALL, locale);
494 }
495 }
496 }
497 else // language == wxLANGUAGE_DEFAULT
498 {
499 retloc = wxSetlocale(LC_ALL, wxEmptyString);
500 }
501
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() )
510 {
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";
514 }
515 }
516 #endif // CRT not handling Unicode-only languages
517
518 if ( !retloc )
519 ret = false;
520 #elif defined(__WXMAC__)
521 if (lang == wxLANGUAGE_DEFAULT)
522 locale = wxEmptyString;
523 else
524 locale = info->CanonicalName;
525
526 const char *retloc = wxSetlocale(LC_ALL, locale);
527
528 if ( !retloc )
529 {
530 // Some C libraries don't like xx_YY form and require xx only
531 retloc = wxSetlocale(LC_ALL, ExtractLang(locale));
532 }
533 #else
534 wxUnusedVar(flags);
535 return false;
536 #define WX_NO_LOCALE_SUPPORT
537 #endif
538
539 #ifndef WX_NO_LOCALE_SUPPORT
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
548 if ( !DoInit(name, canonical, retloc) )
549 {
550 ret = false;
551 }
552
553 if (IsOk()) // setlocale() succeeded
554 m_language = lang;
555
556 // NB: don't use 'lang' here, 'language'
557 wxTranslations *t = wxTranslations::Get();
558 if ( t )
559 {
560 t->SetLanguage(static_cast<wxLanguage>(language));
561
562 if ( flags & wxLOCALE_LOAD_DEFAULT )
563 t->AddStdCatalog();
564 }
565
566 return ret;
567 #endif // !WX_NO_LOCALE_SUPPORT
568 }
569
570 namespace
571 {
572
573 #ifndef __WXOSX__
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 }
580 #endif
581
582 } // anonymous namespace
583
584 /*static*/ int wxLocale::GetSystemLanguage()
585 {
586 CreateLanguagesDB();
587
588 // init i to avoid compiler warning
589 size_t i = 0,
590 count = ms_languagesDB->GetCount();
591
592 #if defined(__UNIX__)
593 // first get the string identifying the language from the environment
594 wxString langFull;
595 #ifdef __WXOSX__
596 wxCFRef<CFLocaleRef> userLocaleRef(CFLocaleCopyCurrent());
597
598 // because the locale identifier (kCFLocaleIdentifier) is formatted a little bit differently, eg
599 // az_Cyrl_AZ@calendar=buddhist;currency=JPY we just recreate the base info as expected by wx here
600
601 wxCFStringRef str(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleLanguageCode)));
602 langFull = str.AsString()+"_";
603 str.reset(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleCountryCode)));
604 langFull += str.AsString();
605 #else
606 if (!wxGetNonEmptyEnvVar(wxS("LC_ALL"), &langFull) &&
607 !wxGetNonEmptyEnvVar(wxS("LC_MESSAGES"), &langFull) &&
608 !wxGetNonEmptyEnvVar(wxS("LANG"), &langFull))
609 {
610 // no language specified, treat it as English
611 return wxLANGUAGE_ENGLISH_US;
612 }
613
614 if ( langFull == wxS("C") || langFull == wxS("POSIX") )
615 {
616 // default C locale is English too
617 return wxLANGUAGE_ENGLISH_US;
618 }
619 #endif
620
621 // the language string has the following form
622 //
623 // lang[_LANG][.encoding][@modifier]
624 //
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"
630 //
631 // for example, the following strings are valid:
632 // fr
633 // fr_FR
634 // de_DE.iso88591
635 // de_DE@euro
636 // de_DE.iso88591@euro
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)
640 //
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
648 size_t posEndLang = langFull.find_first_of(wxS("@."));
649 if ( posEndLang != wxString::npos )
650 {
651 langFull.Truncate(posEndLang);
652 }
653
654 // do we have just the language (or sublang too)?
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;
679
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++ )
692 {
693 if ( ms_languagesDB->Item(i).CanonicalName == langFullWithModifier )
694 break;
695 }
696 }
697
698 // b) Without modifier
699 if ( modifier.empty() || i == count )
700 {
701 for ( i = 0; i < count; i++ )
702 {
703 if ( ms_languagesDB->Item(i).CanonicalName == langFull )
704 break;
705 }
706 }
707
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++ )
712 {
713 if ( ms_languagesDB->Item(i).CanonicalName == lang )
714 {
715 break;
716 }
717 }
718 }
719
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++ )
724 {
725 if ( ExtractLang(ms_languagesDB->Item(i).CanonicalName)
726 == langFull )
727 {
728 break;
729 }
730 }
731 }
732
733
734 if ( i == count )
735 {
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.
740 for ( i = 0; i < count; i++ )
741 {
742 if (ms_languagesDB->Item(i).Description.CmpNoCase(langFull) == 0)
743 {
744 break;
745 }
746 }
747 }
748 #elif defined(__WIN32__)
749 LCID lcid = GetUserDefaultLCID();
750 if ( lcid != 0 )
751 {
752 wxUint32 lang = PRIMARYLANGID(LANGIDFROMLCID(lcid));
753 wxUint32 sublang = SUBLANGID(LANGIDFROMLCID(lcid));
754
755 for ( i = 0; i < count; i++ )
756 {
757 if (ms_languagesDB->Item(i).WinLang == lang &&
758 ms_languagesDB->Item(i).WinSublang == sublang)
759 {
760 break;
761 }
762 }
763 }
764 //else: leave wxlang == wxLANGUAGE_UNKNOWN
765 #endif // Unix/Win32
766
767 if ( i < count )
768 {
769 // we did find a matching entry, use it
770 return ms_languagesDB->Item(i).Language;
771 }
772
773 // no info about this language in the database
774 return wxLANGUAGE_UNKNOWN;
775 }
776
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
783 // reflects the way different systems provide the encoding info
784
785 /* static */
786 wxString wxLocale::GetSystemEncodingName()
787 {
788 wxString encname;
789
790 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
791 // FIXME: what is the error return value for GetACP()?
792 UINT codepage = ::GetACP();
793 encname.Printf(wxS("windows-%u"), codepage);
794 #elif defined(__WXMAC__)
795 encname = wxCFStringRef::AsString(
796 CFStringGetNameOfEncoding(CFStringGetSystemEncoding())
797 );
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)
803 char *oldLocale = strdup(setlocale(LC_CTYPE, NULL));
804 setlocale(LC_CTYPE, "");
805 const char *alang = nl_langinfo(CODESET);
806 setlocale(LC_CTYPE, oldLocale);
807 free(oldLocale);
808
809 if ( alang )
810 {
811 encname = wxString::FromAscii( alang );
812 }
813 else // nl_langinfo() failed
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)
819 char *lang = getenv( "LC_ALL");
820 char *dot = lang ? strchr(lang, '.') : NULL;
821 if (!dot)
822 {
823 lang = getenv( "LC_CTYPE" );
824 if ( lang )
825 dot = strchr(lang, '.' );
826 }
827 if (!dot)
828 {
829 lang = getenv( "LANG");
830 if ( lang )
831 dot = strchr(lang, '.');
832 }
833
834 if ( dot )
835 {
836 encname = wxString::FromAscii( dot+1 );
837 }
838 }
839 #endif // Win32/Unix
840
841 return encname;
842 }
843
844 /* static */
845 wxFontEncoding wxLocale::GetSystemEncoding()
846 {
847 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
848 UINT codepage = ::GetACP();
849
850 // wxWidgets only knows about CP1250-1257, 874, 932, 936, 949, 950
851 if ( codepage >= 1250 && codepage <= 1257 )
852 {
853 return (wxFontEncoding)(wxFONTENCODING_CP1250 + codepage - 1250);
854 }
855
856 if ( codepage == 874 )
857 {
858 return wxFONTENCODING_CP874;
859 }
860
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 }
880 #elif defined(__WXMAC__)
881 CFStringEncoding encoding = 0 ;
882 encoding = CFStringGetSystemEncoding() ;
883 return wxMacGetFontEncFromSystemEnc( encoding ) ;
884 #elif defined(__UNIX_LIKE__) && wxUSE_FONTMAP
885 const wxString encname = GetSystemEncodingName();
886 if ( !encname.empty() )
887 {
888 wxFontEncoding enc = wxFontMapperBase::GetEncodingFromName(encname);
889
890 // on some modern Linux systems (RedHat 8) the default system locale
891 // is UTF8 -- but it isn't supported by wxGTK1 in ANSI build at all so
892 // don't even try to use it in this case
893 #if !wxUSE_UNICODE && \
894 ((defined(__WXGTK__) && !defined(__WXGTK20__)) || defined(__WXMOTIF__))
895 if ( enc == wxFONTENCODING_UTF8 )
896 {
897 // the most similar supported encoding...
898 enc = wxFONTENCODING_ISO8859_1;
899 }
900 #endif // !wxUSE_UNICODE
901
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
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 )
913 {
914 return enc;
915 }
916 //else: return wxFONTENCODING_SYSTEM below
917 }
918 #endif // Win32/Unix
919
920 return wxFONTENCODING_SYSTEM;
921 }
922
923 /* static */
924 void wxLocale::AddLanguage(const wxLanguageInfo& info)
925 {
926 CreateLanguagesDB();
927 ms_languagesDB->Add(info);
928 }
929
930 /* static */
931 const wxLanguageInfo *wxLocale::GetLanguageInfo(int lang)
932 {
933 CreateLanguagesDB();
934
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
940 const size_t count = ms_languagesDB->GetCount();
941 for ( size_t i = 0; i < count; i++ )
942 {
943 if ( ms_languagesDB->Item(i).Language == lang )
944 {
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;
948 }
949 }
950
951 return NULL;
952 }
953
954 /* static */
955 wxString wxLocale::GetLanguageName(int lang)
956 {
957 if ( lang == wxLANGUAGE_DEFAULT || lang == wxLANGUAGE_UNKNOWN )
958 return wxEmptyString;
959
960 const wxLanguageInfo *info = GetLanguageInfo(lang);
961 if ( !info )
962 return wxEmptyString;
963 else
964 return info->Description;
965 }
966
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
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
1000 if ( wxStricmp(locale, info->CanonicalName.BeforeFirst(wxS('_'))) == 0 )
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
1006 // case don't overwrite it because the entry for the default
1007 // country always appears first in ms_languagesDB
1008 if ( !infoRet )
1009 infoRet = info;
1010 }
1011 }
1012
1013 return infoRet;
1014 }
1015
1016 wxString wxLocale::GetSysName() const
1017 {
1018 return wxSetlocale(LC_ALL, NULL);
1019 }
1020
1021 // clean up
1022 wxLocale::~wxLocale()
1023 {
1024 // Restore old translations object.
1025 // See DoCommonInit() for explanation of why this is needed for backward
1026 // compatibility.
1027 if ( wxTranslations::Get() == &m_translations )
1028 {
1029 if ( m_pOldLocale )
1030 wxTranslations::SetNonOwned(&m_pOldLocale->m_translations);
1031 else
1032 wxTranslations::Set(NULL);
1033 }
1034
1035 // restore old locale pointer
1036 wxSetLocale(m_pOldLocale);
1037
1038 wxSetlocale(LC_ALL, m_pszOldLocale);
1039 free(const_cast<char *>(m_pszOldLocale));
1040 }
1041
1042
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);
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 }
1056
1057 #if defined(__WIN32__)
1058 if ( !info->WinLang )
1059 return false;
1060
1061 if ( !::IsValidLocale(info->GetLCID(), LCID_INSTALLED) )
1062 return false;
1063
1064 #elif defined(__UNIX__)
1065
1066 // Test if setting the locale works, then set it back.
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
1075 // restore the original locale
1076 wxSetlocale(LC_ALL, oldLocale);
1077
1078 free(oldLocale);
1079
1080 if ( !available )
1081 return false;
1082 #endif
1083
1084 return true;
1085 }
1086
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
1104 // add a catalog to our linked list
1105 bool wxLocale::AddCatalog(const wxString& szDomain,
1106 wxLanguage msgIdLanguage,
1107 const wxString& msgIdCharset)
1108 {
1109 wxTranslations *t = wxTranslations::Get();
1110 if ( !t )
1111 return false;
1112 #if wxUSE_UNICODE
1113 wxUnusedVar(msgIdCharset);
1114 return t->AddCatalog(szDomain, msgIdLanguage);
1115 #else
1116 return t->AddCatalog(szDomain, msgIdLanguage, msgIdCharset);
1117 #endif
1118 }
1119
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
1137 // ----------------------------------------------------------------------------
1138 // accessors for locale-dependent data
1139 // ----------------------------------------------------------------------------
1140
1141 #if defined(__WINDOWS__) || defined(__WXOSX__)
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;
1160
1161 const char* formatchars =
1162 "dghHmMsSy"
1163 #ifdef __WINDOWS__
1164 "t"
1165 #else
1166 "EawD"
1167 #endif
1168 ;
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();
1180 if ( ch.IsAscii() && strchr(formatchars, ch) )
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;
1203 #ifdef __WINDOWS__
1204 case 3: // ddd
1205 fmtWX += "%a";
1206 break;
1207
1208 case 4: // dddd
1209 fmtWX += "%A";
1210 break;
1211 #endif
1212 default:
1213 wxFAIL_MSG( "too many 'd's" );
1214 }
1215 break;
1216 #ifndef __WINDOWS__
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;
1256
1257 default:
1258 wxFAIL_MSG( "wrong number of 'E's" );
1259 }
1260 break;
1261 #endif
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
1319 fmtWX += "%I";
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" );
1357
1358 break;
1359 #ifndef __WINDOWS__
1360 case 'a':
1361 fmtWX += "%p";
1362 break;
1363 #endif
1364 #ifdef __WINDOWS__
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
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
1390 if ( *p == wxT('%') )
1391 {
1392 // this one needs to be escaped
1393 fmtWX += wxT('%');
1394 }
1395
1396 fmtWX += *p;
1397 }
1398
1399 return fmtWX;
1400 }
1401
1402 } // anonymous namespace
1403
1404 #endif // __WINDOWS__ || __WXOSX__
1405
1406 #if defined(__WINDOWS__)
1407
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
1433 /* static */
1434 wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat)
1435 {
1436 const wxLanguageInfo * const
1437 info = wxGetLocale() ? GetLanguageInfo(wxGetLocale()->GetLanguage())
1438 : NULL;
1439 if ( !info )
1440 {
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 ")
1446 wxS("of using wxLocale and now there is a ")
1447 wxS("mismatch between C/C++ and Windows locale.\n")
1448 wxS("Things are going to break, please only change ")
1449 wxS("locale by creating wxLocale objects to avoid this!") );
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 }
1480 }
1481
1482 const wxUint32 lcid = info->GetLCID();
1483
1484 wxString str;
1485
1486 wxChar buf[256];
1487 buf[0] = wxT('\0');
1488
1489 switch ( index )
1490 {
1491 case wxLOCALE_THOUSANDS_SEP:
1492 if ( ::GetLocaleInfo(lcid, LOCALE_STHOUSAND, buf, WXSIZEOF(buf)) )
1493 str = buf;
1494 break;
1495
1496 case wxLOCALE_DECIMAL_POINT:
1497 if ( ::GetLocaleInfo(lcid,
1498 cat == wxLOCALE_CAT_MONEY
1499 ? LOCALE_SMONDECIMALSEP
1500 : LOCALE_SDECIMAL,
1501 buf,
1502 WXSIZEOF(buf)) )
1503 {
1504 str = buf;
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 }
1520 break;
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 }
1530 break;
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
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)
1539 {
1540 const wxString datefmt = GetInfo(wxLOCALE_SHORT_DATE_FMT);
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 }
1550 break;
1551
1552 default:
1553 wxFAIL_MSG( "unknown wxLocaleInfo" );
1554 }
1555
1556 return str;
1557 }
1558
1559 #elif defined(__WXOSX__)
1560
1561 /* static */
1562 wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory WXUNUSED(cat))
1563 {
1564 CFLocaleRef userLocaleRefRaw;
1565 if ( wxGetLocale() )
1566 {
1567 userLocaleRefRaw = CFLocaleCreate
1568 (
1569 kCFAllocatorDefault,
1570 wxCFStringRef(wxGetLocale()->GetCanonicalName())
1571 );
1572 }
1573 else // no current locale, use the default one
1574 {
1575 userLocaleRefRaw = CFLocaleCopyCurrent();
1576 }
1577
1578 wxCFRef<CFLocaleRef> userLocaleRef(userLocaleRefRaw);
1579
1580 CFStringRef cfstr = 0;
1581 switch ( index )
1582 {
1583 case wxLOCALE_THOUSANDS_SEP:
1584 cfstr = (CFStringRef) CFLocaleGetValue(userLocaleRef, kCFLocaleGroupingSeparator);
1585 break;
1586
1587 case wxLOCALE_DECIMAL_POINT:
1588 cfstr = (CFStringRef) CFLocaleGetValue(userLocaleRef, kCFLocaleDecimalSeparator);
1589 break;
1590
1591 case wxLOCALE_SHORT_DATE_FMT:
1592 case wxLOCALE_LONG_DATE_FMT:
1593 case wxLOCALE_DATE_TIME_FMT:
1594 case wxLOCALE_TIME_FMT:
1595 {
1596 CFDateFormatterStyle dateStyle = kCFDateFormatterNoStyle;
1597 CFDateFormatterStyle timeStyle = kCFDateFormatterNoStyle;
1598 switch (index )
1599 {
1600 case wxLOCALE_SHORT_DATE_FMT:
1601 dateStyle = kCFDateFormatterShortStyle;
1602 break;
1603 case wxLOCALE_LONG_DATE_FMT:
1604 dateStyle = kCFDateFormatterFullStyle;
1605 break;
1606 case wxLOCALE_DATE_TIME_FMT:
1607 dateStyle = kCFDateFormatterFullStyle;
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 ));
1620 wxString format = TranslateFromUnicodeFormat(cfs.AsString());
1621 // we always want full years
1622 format.Replace("%y","%Y");
1623 return format;
1624 }
1625 break;
1626
1627 default:
1628 wxFAIL_MSG( "Unknown locale info" );
1629 return wxString();
1630 }
1631
1632 wxCFStringRef str(wxCFRetain(cfstr));
1633 return str.AsString();
1634 }
1635
1636 #else // !__WINDOWS__ && !__WXOSX__, assume generic POSIX
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
1708 wxUnusedVar(index);
1709
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
1720
1721 /* static */
1722 wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat)
1723 {
1724 lconv * const lc = localeconv();
1725 if ( !lc )
1726 return wxString();
1727
1728 switch ( index )
1729 {
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;
1735
1736 wxFAIL_MSG( "invalid wxLocaleCategory" );
1737 break;
1738
1739
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" );
1747 break;
1748
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
1762 default:
1763 wxFAIL_MSG( "unknown wxLocaleInfo value" );
1764 }
1765
1766 return wxString();
1767 }
1768
1769 #endif // platform
1770
1771 // ----------------------------------------------------------------------------
1772 // global functions and variables
1773 // ----------------------------------------------------------------------------
1774
1775 // retrieve/change current locale
1776 // ------------------------------
1777
1778 // the current locale object
1779 static wxLocale *g_pLocale = NULL;
1780
1781 wxLocale *wxGetLocale()
1782 {
1783 return g_pLocale;
1784 }
1785
1786 wxLocale *wxSetLocale(wxLocale *pLocale)
1787 {
1788 wxLocale *pOld = g_pLocale;
1789 g_pLocale = pLocale;
1790 return pOld;
1791 }
1792
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() {}
1804
1805 bool OnInit()
1806 {
1807 return true;
1808 }
1809
1810 void OnExit()
1811 {
1812 wxLocale::DestroyLanguagesDB();
1813 }
1814 };
1815
1816 IMPLEMENT_DYNAMIC_CLASS(wxLocaleModule, wxModule)
1817
1818 #endif // wxUSE_INTL