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