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