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