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