-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
intl.cpp
2091 lines (1768 loc) · 60.5 KB
/
intl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////////
// Name: src/common/intl.cpp
// Purpose: Internationalization and localisation for wxWidgets
// Author: Vadim Zeitlin
// Modified by: Michael N. Filippov <michael@idisys.iae.nsk.su>
// (2003/09/30 - PluralForms support)
// Created: 29/01/98
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declaration
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#if wxUSE_INTL
#ifndef WX_PRECOMP
#include "wx/dynarray.h"
#include "wx/string.h"
#include "wx/intl.h"
#include "wx/log.h"
#include "wx/utils.h"
#include "wx/app.h"
#include "wx/hashmap.h"
#include "wx/module.h"
#endif // WX_PRECOMP
#include <locale.h>
// standard headers
#include <ctype.h>
#include <stdlib.h>
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
#ifdef __WIN32__
#include "wx/dynlib.h"
#include "wx/msw/private.h"
#ifndef LOCALE_SNAME
#define LOCALE_SNAME 0x5c
#endif
#endif
#include "wx/file.h"
#include "wx/filename.h"
#include "wx/tokenzr.h"
#include "wx/fontmap.h"
#include "wx/scopedptr.h"
#include "wx/apptrait.h"
#include "wx/stdpaths.h"
#include "wx/hashset.h"
#if defined(__WXOSX__)
#include "wx/osx/core/cfref.h"
#include "wx/osx/core/cfstring.h"
#include <CoreFoundation/CFLocale.h>
#include <CoreFoundation/CFDateFormatter.h>
#include <CoreFoundation/CFNumberFormatter.h>
#include <CoreFoundation/CFString.h>
#endif
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
#define TRACE_I18N wxS("i18n")
// ============================================================================
// implementation
// ============================================================================
// ----------------------------------------------------------------------------
// global functions
// ----------------------------------------------------------------------------
static wxLocale *wxSetLocale(wxLocale *pLocale);
namespace
{
// get just the language part ("en" in "en_GB")
inline wxString ExtractLang(const wxString& langFull)
{
return langFull.BeforeFirst('_');
}
// helper functions of GetSystemLanguage()
#ifdef __UNIX__
// get everything else (including the leading '_')
inline wxString ExtractNotLang(const wxString& langFull)
{
size_t pos = langFull.find('_');
if ( pos != wxString::npos )
return langFull.substr(pos);
else
return wxString();
}
#endif // __UNIX__
} // anonymous namespace
// ----------------------------------------------------------------------------
// wxLanguageInfo
// ----------------------------------------------------------------------------
#ifdef __WINDOWS__
// helper used by wxLanguageInfo::GetLocaleName() and elsewhere to determine
// whether the locale is Unicode-only (it is if this function returns empty
// string)
static wxString wxGetANSICodePageForLocale(LCID lcid)
{
wxString cp;
wxChar buffer[16];
if ( ::GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE,
buffer, WXSIZEOF(buffer)) > 0 )
{
if ( buffer[0] != wxT('0') || buffer[1] != wxT('\0') )
cp = buffer;
//else: this locale doesn't use ANSI code page
}
return cp;
}
wxUint32 wxLanguageInfo::GetLCID() const
{
return MAKELCID(MAKELANGID(WinLang, WinSublang), SORT_DEFAULT);
}
const char* wxLanguageInfo::TrySetLocale() const
{
wxString locale;
const LCID lcid = GetLCID();
wxChar buffer[256];
buffer[0] = wxT('\0');
// Prefer to use the new (Vista and later) locale names instead of locale
// identifiers if supported, both at the OS level (LOCALE_SNAME) and by the
// CRT (check by calling setlocale()).
if ( wxGetWinVersion() >= wxWinVersion_Vista )
{
if ( ::GetLocaleInfo(lcid, LOCALE_SNAME, buffer, WXSIZEOF(buffer)) )
{
locale = buffer;
}
else
{
wxLogLastError(wxT("GetLocaleInfo(LOCALE_SNAME)"));
}
const char* const retloc = wxSetlocale(LC_ALL, locale);
if ( retloc )
return retloc;
//else: fall back to LOCALE_SENGLANGUAGE
}
if ( !::GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE, buffer, WXSIZEOF(buffer)) )
{
wxLogLastError(wxT("GetLocaleInfo(LOCALE_SENGLANGUAGE)"));
return NULL;
}
locale = buffer;
if ( ::GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY,
buffer, WXSIZEOF(buffer)) > 0 )
{
locale << wxT('_') << buffer;
}
const wxString cp = wxGetANSICodePageForLocale(lcid);
if ( !cp.empty() )
{
locale << wxT('.') << cp;
}
return wxSetlocale(LC_ALL, locale);
}
#else // !__WINDOWS__
const char* wxLanguageInfo::TrySetLocale() const
{
return wxSetlocale(LC_ALL, CanonicalName);
}
#endif // __WINDOWS__/!__WINDOWS__
wxString wxLanguageInfo::GetLocaleName() const
{
const char* const orig = wxSetlocale(LC_ALL, NULL);
const char* const ret = TrySetLocale();
wxString retval;
if ( ret )
{
// Note that we must copy the returned value before calling setlocale()
// again as the string "ret" points to can (and, at least under Linux
// with glibc, actually always will) be changed by this call.
retval = ret;
wxSetlocale(LC_ALL, orig);
}
return retval;
}
// ----------------------------------------------------------------------------
// wxLocale
// ----------------------------------------------------------------------------
#include "wx/arrimpl.cpp"
WX_DECLARE_USER_EXPORTED_OBJARRAY(wxLanguageInfo, wxLanguageInfoArray, WXDLLIMPEXP_BASE);
WX_DEFINE_OBJARRAY(wxLanguageInfoArray)
wxLanguageInfoArray *wxLocale::ms_languagesDB = NULL;
/*static*/ void wxLocale::CreateLanguagesDB()
{
if (ms_languagesDB == NULL)
{
ms_languagesDB = new wxLanguageInfoArray;
InitLanguagesDB();
}
}
/*static*/ void wxLocale::DestroyLanguagesDB()
{
wxDELETE(ms_languagesDB);
}
void wxLocale::DoCommonInit()
{
// Store the current locale in order to be able to restore it in the dtor.
m_pszOldLocale = wxSetlocale(LC_ALL, NULL);
if ( m_pszOldLocale )
m_pszOldLocale = wxStrdup(m_pszOldLocale);
m_pOldLocale = wxSetLocale(this);
// Set translations object, but only if the user didn't do so yet.
// This is to preserve compatibility with wx-2.8 where wxLocale was
// the only API for translations. wxLocale works as a stack, with
// latest-created one being the active one:
// wxLocale loc_fr(wxLANGUAGE_FRENCH);
// // _() returns French
// {
// wxLocale loc_cs(wxLANGUAGE_CZECH);
// // _() returns Czech
// }
// // _() returns French again
wxTranslations *oldTrans = wxTranslations::Get();
if ( !oldTrans ||
(m_pOldLocale && oldTrans == &m_pOldLocale->m_translations) )
{
wxTranslations::SetNonOwned(&m_translations);
}
m_language = wxLANGUAGE_UNKNOWN;
m_initialized = false;
}
// NB: this function has (desired) side effect of changing current locale
bool wxLocale::Init(const wxString& name,
const wxString& shortName,
const wxString& locale,
bool bLoadDefault
#if WXWIN_COMPATIBILITY_2_8
,bool WXUNUSED_UNLESS_DEBUG(bConvertEncoding)
#endif
)
{
#if WXWIN_COMPATIBILITY_2_8
wxASSERT_MSG( bConvertEncoding,
wxS("wxLocale::Init with bConvertEncoding=false is no longer supported, add charset to your catalogs") );
#endif
// change current locale (default: same as long name)
wxString szLocale(locale);
if ( szLocale.empty() )
{
// the argument to setlocale()
szLocale = shortName;
wxCHECK_MSG( !szLocale.empty(), false,
wxS("no locale to set in wxLocale::Init()") );
}
if ( const wxLanguageInfo* langInfo = FindLanguageInfo(szLocale) )
{
// Prefer to use Init(wxLanguage) overload if possible as it will
// correctly set our m_language and also set the locale correctly under
// MSW, where just calling wxSetLocale() as we do below is not enough.
//
// However don't do it if the parameters are incompatible with this
// language, e.g. if we are called with something like ("French", "de")
// to use French locale but German translations: this seems unlikely to
// happen but, in principle, it could.
if ( langInfo->CanonicalName.StartsWith(shortName) )
{
return Init(langInfo->Language,
bLoadDefault ? wxLOCALE_LOAD_DEFAULT : 0);
}
}
// the short name will be used to look for catalog files as well,
// so we need something here
wxString strShort(shortName);
if ( strShort.empty() ) {
// FIXME I don't know how these 2 letter abbreviations are formed,
// this wild guess is surely wrong
if ( !szLocale.empty() )
{
strShort += (wxChar)wxTolower(szLocale[0]);
if ( szLocale.length() > 1 )
strShort += (wxChar)wxTolower(szLocale[1]);
}
}
DoInit(name, strShort, wxLANGUAGE_UNKNOWN);
const bool ret = wxSetlocale(LC_ALL, szLocale) != NULL;
return DoCommonPostInit(ret, szLocale, shortName, bLoadDefault);
}
void wxLocale::DoInit(const wxString& name,
const wxString& shortName,
int language)
{
wxASSERT_MSG( !m_initialized,
wxS("you can't call wxLocale::Init more than once") );
m_initialized = true;
m_strLocale = name;
m_strShort = shortName;
m_language = language;
}
bool wxLocale::DoCommonPostInit(bool success,
const wxString& name,
const wxString& shortName,
bool bLoadDefault)
{
if ( !success )
{
wxLogWarning(_("Cannot set locale to language \"%s\"."), name);
// As we failed to change locale, there is no need to restore the
// previous one: it's still valid.
free(const_cast<char *>(m_pszOldLocale));
m_pszOldLocale = NULL;
// continue nevertheless and try to load at least the translations for
// this language
}
wxTranslations *t = wxTranslations::Get();
if ( t )
{
t->SetLanguage(shortName);
if ( bLoadDefault )
t->AddStdCatalog();
}
return success;
}
#if defined(__UNIX__) && wxUSE_UNICODE && !defined(__WXMAC__)
static const char *wxSetlocaleTryUTF8(int c, const wxString& lc)
{
const char *l = NULL;
// NB: We prefer to set UTF-8 locale if it's possible and only fall back to
// non-UTF-8 locale if it fails
if ( !lc.empty() )
{
wxString buf(lc);
wxString buf2;
buf2 = buf + wxS(".UTF-8");
l = wxSetlocale(c, buf2);
if ( !l )
{
buf2 = buf + wxS(".utf-8");
l = wxSetlocale(c, buf2);
}
if ( !l )
{
buf2 = buf + wxS(".UTF8");
l = wxSetlocale(c, buf2);
}
if ( !l )
{
buf2 = buf + wxS(".utf8");
l = wxSetlocale(c, buf2);
}
}
// if we can't set UTF-8 locale, try non-UTF-8 one:
if ( !l )
l = wxSetlocale(c, lc);
return l;
}
#else
#define wxSetlocaleTryUTF8(c, lc) wxSetlocale(c, lc)
#endif
bool wxLocale::Init(int language, int flags)
{
#if WXWIN_COMPATIBILITY_2_8
wxASSERT_MSG( !(flags & wxLOCALE_CONV_ENCODING),
wxS("wxLOCALE_CONV_ENCODING is no longer supported, add charset to your catalogs") );
#endif
int lang = language;
if (lang == wxLANGUAGE_DEFAULT)
{
// auto detect the language
lang = GetSystemLanguage();
}
// We failed to detect system language, so we will use English:
if (lang == wxLANGUAGE_UNKNOWN)
{
return false;
}
const wxLanguageInfo *info = GetLanguageInfo(lang);
// Unknown language:
if (info == NULL)
{
wxLogError(wxS("Unknown language %i."), lang);
return false;
}
const wxString& name = info->Description;
DoInit(name, info->CanonicalName, lang);
// Set the locale:
#if defined(__UNIX__) && !defined(__WXMAC__)
const wxString& locale = info->CanonicalName;
const char *retloc = wxSetlocaleTryUTF8(LC_ALL, locale);
const wxString langOnly = ExtractLang(locale);
if ( !retloc )
{
// Some C libraries don't like xx_YY form and require xx only
retloc = wxSetlocaleTryUTF8(LC_ALL, langOnly);
}
#if wxUSE_FONTMAP
// some systems (e.g. FreeBSD and HP-UX) don't have xx_YY aliases but
// require the full xx_YY.encoding form, so try using UTF-8 because this is
// the only thing we can do generically
//
// TODO: add encodings applicable to each language to the lang DB and try
// them all in turn here
if ( !retloc )
{
const wxChar **names =
wxFontMapperBase::GetAllEncodingNames(wxFONTENCODING_UTF8);
while ( *names )
{
retloc = wxSetlocale(LC_ALL, locale + wxS('.') + *names++);
if ( retloc )
break;
}
}
#endif // wxUSE_FONTMAP
if ( !retloc )
{
// Some C libraries (namely glibc) still use old ISO 639,
// so will translate the abbrev for them
wxString localeAlt;
if ( langOnly == wxS("he") )
localeAlt = wxS("iw") + ExtractNotLang(locale);
else if ( langOnly == wxS("id") )
localeAlt = wxS("in") + ExtractNotLang(locale);
else if ( langOnly == wxS("yi") )
localeAlt = wxS("ji") + ExtractNotLang(locale);
else if ( langOnly == wxS("nb") )
localeAlt = wxS("no_NO");
else if ( langOnly == wxS("nn") )
localeAlt = wxS("no_NY");
if ( !localeAlt.empty() )
{
retloc = wxSetlocaleTryUTF8(LC_ALL, localeAlt);
if ( !retloc )
retloc = wxSetlocaleTryUTF8(LC_ALL, ExtractLang(localeAlt));
}
}
#ifdef __AIX__
// at least in AIX 5.2 libc is buggy and the string returned from
// setlocale(LC_ALL) can't be passed back to it because it returns 6
// strings (one for each locale category), i.e. for C locale we get back
// "C C C C C C"
//
// this contradicts IBM own docs but this is not of much help, so just work
// around it in the crudest possible manner
char* p = const_cast<char*>(wxStrchr(retloc, ' '));
if ( p )
*p = '\0';
#endif // __AIX__
#elif defined(__WIN32__)
const char *retloc = "C";
if ( info->WinLang == 0 )
{
wxLogWarning(wxS("Locale '%s' not supported by OS."), name);
// retloc already set to "C"
}
else // language supported by Windows
{
const wxUint32 lcid = info->GetLCID();
// change locale used by Windows functions
::SetThreadLocale(lcid);
// SetThreadUILanguage() is available on XP, but with unclear
// behavior, so avoid calling it there.
if ( wxGetWinVersion() >= wxWinVersion_Vista )
{
wxLoadedDLL dllKernel32(wxS("kernel32.dll"));
typedef LANGID(WINAPI *SetThreadUILanguage_t)(LANGID);
SetThreadUILanguage_t pfnSetThreadUILanguage = NULL;
wxDL_INIT_FUNC(pfn, SetThreadUILanguage, dllKernel32);
if (pfnSetThreadUILanguage)
pfnSetThreadUILanguage(LANGIDFROMLCID(lcid));
}
// and also call setlocale() to change locale used by the CRT
retloc = info->TrySetLocale();
}
#if wxUSE_UNICODE && (defined(__VISUALC__) || defined(__MINGW32__))
// VC++ setlocale() (also used by Mingw) can't set locale to languages that
// can only be written using Unicode, therefore wxSetlocale() call fails
// for such languages but we don't want to report it as an error -- so that
// at least message catalogs can be used.
if ( !retloc )
{
if ( wxGetANSICodePageForLocale(LOCALE_USER_DEFAULT).empty() )
{
// we set the locale to a Unicode-only language, don't treat the
// inability of CRT to use it as an error
retloc = "C";
}
}
#endif // CRT not handling Unicode-only languages
#elif defined(__WXMAC__)
const wxString& locale = info->CanonicalName;
const char *retloc = wxSetlocale(LC_ALL, locale);
if ( !retloc )
{
// Some C libraries don't like xx_YY form and require xx only
retloc = wxSetlocale(LC_ALL, ExtractLang(locale));
}
#else
wxUnusedVar(flags);
return false;
#define WX_NO_LOCALE_SUPPORT
#endif
#ifndef WX_NO_LOCALE_SUPPORT
// NB: don't use 'lang' here, 'language'
return DoCommonPostInit
(
retloc != NULL,
name,
language == wxLANGUAGE_DEFAULT
? wxString()
: info->CanonicalName,
flags & wxLOCALE_LOAD_DEFAULT
);
#endif // !WX_NO_LOCALE_SUPPORT
}
namespace
{
#ifndef __WXOSX__
// Small helper function: get the value of the given environment variable and
// return true only if the variable was found and has non-empty value.
inline bool wxGetNonEmptyEnvVar(const wxString& name, wxString* value)
{
return wxGetEnv(name, value) && !value->empty();
}
#endif
} // anonymous namespace
/*static*/ int wxLocale::GetSystemLanguage()
{
CreateLanguagesDB();
// init i to avoid compiler warning
size_t i = 0,
count = ms_languagesDB->GetCount();
#if defined(__UNIX__)
// first get the string identifying the language from the environment
wxString langFull;
#ifdef __WXOSX__
wxCFRef<CFLocaleRef> userLocaleRef(CFLocaleCopyCurrent());
// because the locale identifier (kCFLocaleIdentifier) is formatted a little bit differently, eg
// az_Cyrl_AZ@calendar=buddhist;currency=JPY we just recreate the base info as expected by wx here
wxCFStringRef str(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleLanguageCode)));
langFull = str.AsString()+"_";
str.reset(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleCountryCode)));
langFull += str.AsString();
#else
if (!wxGetNonEmptyEnvVar(wxS("LC_ALL"), &langFull) &&
!wxGetNonEmptyEnvVar(wxS("LC_MESSAGES"), &langFull) &&
!wxGetNonEmptyEnvVar(wxS("LANG"), &langFull))
{
// no language specified, treat it as English
return wxLANGUAGE_ENGLISH_US;
}
#endif
// the language string has the following form
//
// lang[_LANG][.encoding][@modifier]
//
// (see environ(5) in the Open Unix specification)
//
// where lang is the primary language, LANG is a sublang/territory,
// encoding is the charset to use and modifier "allows the user to select
// a specific instance of localization data within a single category"
//
// for example, the following strings are valid:
// fr
// fr_FR
// de_DE.iso88591
// de_DE@euro
// de_DE.iso88591@euro
// for now we don't use the encoding, although we probably should (doing
// translations of the msg catalogs on the fly as required) (TODO)
//
// we need the modified for languages like Valencian: ca_ES@valencia
// though, remember it
wxString modifier;
size_t posModifier = langFull.find_first_of(wxS("@"));
if ( posModifier != wxString::npos )
modifier = langFull.Mid(posModifier);
size_t posEndLang = langFull.find_first_of(wxS("@."));
if ( posEndLang != wxString::npos )
{
langFull.Truncate(posEndLang);
}
if ( langFull == wxS("C") || langFull == wxS("POSIX") )
{
// default C locale is English too
return wxLANGUAGE_ENGLISH_US;
}
// do we have just the language (or sublang too)?
const bool justLang = langFull.find('_') == wxString::npos;
// 0. Make sure the lang is according to latest ISO 639
// (this is necessary because glibc uses iw and in instead
// of he and id respectively).
// the language itself (second part is the dialect/sublang)
wxString langOrig = ExtractLang(langFull);
wxString lang;
if ( langOrig == wxS("iw"))
lang = wxS("he");
else if (langOrig == wxS("in"))
lang = wxS("id");
else if (langOrig == wxS("ji"))
lang = wxS("yi");
else if (langOrig == wxS("no_NO"))
lang = wxS("nb_NO");
else if (langOrig == wxS("no_NY"))
lang = wxS("nn_NO");
else if (langOrig == wxS("no"))
lang = wxS("nb_NO");
else
lang = langOrig;
// did we change it?
if ( lang != langOrig )
{
langFull = lang + ExtractNotLang(langFull);
}
// 1. Try to find the language either as is:
// a) With modifier if set
if ( !modifier.empty() )
{
wxString langFullWithModifier = langFull + modifier;
for ( i = 0; i < count; i++ )
{
if ( ms_languagesDB->Item(i).CanonicalName == langFullWithModifier )
break;
}
}
// b) Without modifier
if ( modifier.empty() || i == count )
{
for ( i = 0; i < count; i++ )
{
if ( ms_languagesDB->Item(i).CanonicalName == langFull )
break;
}
}
// 2. If langFull is of the form xx_YY, try to find xx:
if ( i == count && !justLang )
{
for ( i = 0; i < count; i++ )
{
if ( ms_languagesDB->Item(i).CanonicalName == lang )
{
break;
}
}
}
// 3. If langFull is of the form xx, try to find any xx_YY record:
if ( i == count && justLang )
{
for ( i = 0; i < count; i++ )
{
if ( ExtractLang(ms_languagesDB->Item(i).CanonicalName)
== langFull )
{
break;
}
}
}
if ( i == count )
{
// In addition to the format above, we also can have full language
// names in LANG env var - for example, SuSE is known to use
// LANG="german" - so check for use of non-standard format and try to
// find the name in verbose description.
for ( i = 0; i < count; i++ )
{
if (ms_languagesDB->Item(i).Description.CmpNoCase(langFull) == 0)
{
break;
}
}
}
#elif defined(__WIN32__)
LCID lcid = GetUserDefaultLCID();
if ( lcid != 0 )
{
wxUint32 lang = PRIMARYLANGID(LANGIDFROMLCID(lcid));
wxUint32 sublang = SUBLANGID(LANGIDFROMLCID(lcid));
for ( i = 0; i < count; i++ )
{
if (ms_languagesDB->Item(i).WinLang == lang &&
ms_languagesDB->Item(i).WinSublang == sublang)
{
break;
}
}
}
//else: leave wxlang == wxLANGUAGE_UNKNOWN
#endif // Unix/Win32
if ( i < count )
{
// we did find a matching entry, use it
return ms_languagesDB->Item(i).Language;
}
// no info about this language in the database
return wxLANGUAGE_UNKNOWN;
}
// ----------------------------------------------------------------------------
// encoding stuff
// ----------------------------------------------------------------------------
// this is a bit strange as under Windows we get the encoding name using its
// numeric value and under Unix we do it the other way round, but this just
// reflects the way different systems provide the encoding info
/* static */
wxString wxLocale::GetSystemEncodingName()
{
wxString encname;
#if defined(__WIN32__)
// FIXME: what is the error return value for GetACP()?
const UINT codepage = ::GetACP();
switch ( codepage )
{
case 65001:
encname = "UTF-8";
break;
default:
encname.Printf(wxS("windows-%u"), codepage);
}
#elif defined(__WXMAC__)
encname = wxCFStringRef::AsString(
CFStringGetNameOfEncoding(CFStringGetSystemEncoding())
);
#elif defined(__UNIX_LIKE__)
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
// GNU libc provides current character set this way (this conforms
// to Unix98)
char *oldLocale = strdup(setlocale(LC_CTYPE, NULL));
setlocale(LC_CTYPE, "");
encname = wxString::FromAscii(nl_langinfo(CODESET));
setlocale(LC_CTYPE, oldLocale);
free(oldLocale);
if (encname.empty())
#endif // HAVE_LANGINFO_H
{
// if we can't get at the character set directly, try to see if it's in
// the environment variables (in most cases this won't work, but I was
// out of ideas)
char *lang = getenv( "LC_ALL");
char *dot = lang ? strchr(lang, '.') : NULL;
if (!dot)
{
lang = getenv( "LC_CTYPE" );
if ( lang )
dot = strchr(lang, '.' );
}
if (!dot)
{
lang = getenv( "LANG");
if ( lang )
dot = strchr(lang, '.');
}
if ( dot )
{
encname = wxString::FromAscii( dot+1 );
}
}
#endif // Win32/Unix
return encname;
}
/* static */
wxFontEncoding wxLocale::GetSystemEncoding()
{
#if defined(__WIN32__)
const UINT codepage = ::GetACP();
switch ( codepage )
{
case 1250:
case 1251:
case 1252:
case 1253:
case 1254:
case 1255:
case 1256:
case 1257:
case 1258:
return (wxFontEncoding)(wxFONTENCODING_CP1250 + codepage - 1250);
case 1361:
return wxFONTENCODING_CP1361;
case 874:
return wxFONTENCODING_CP874;
case 932:
return wxFONTENCODING_CP932;
case 936:
return wxFONTENCODING_CP936;
case 949:
return wxFONTENCODING_CP949;
case 950:
return wxFONTENCODING_CP950;
case 65001:
return wxFONTENCODING_UTF8;
}
#elif defined(__WXMAC__)
CFStringEncoding encoding = 0 ;
encoding = CFStringGetSystemEncoding() ;
return wxMacGetFontEncFromSystemEnc( encoding ) ;
#elif defined(__UNIX_LIKE__) && wxUSE_FONTMAP
const wxString encname = GetSystemEncodingName();
if ( !encname.empty() )
{
wxFontEncoding enc = wxFontMapperBase::GetEncodingFromName(encname);
// on some modern Linux systems (RedHat 8) the default system locale
// is UTF8 -- but it isn't supported by wxGTK1 in ANSI build at all so
// don't even try to use it in this case
#if !wxUSE_UNICODE && \
((defined(__WXGTK__) && !defined(__WXGTK20__)) || defined(__WXMOTIF__))
if ( enc == wxFONTENCODING_UTF8 )
{
// the most similar supported encoding...
enc = wxFONTENCODING_ISO8859_1;
}
#endif // !wxUSE_UNICODE
// GetEncodingFromName() returns wxFONTENCODING_DEFAULT for C locale
// (a.k.a. US-ASCII) which is arguably a bug but keep it like this for
// backwards compatibility and just take care to not return
// wxFONTENCODING_DEFAULT from here as this surely doesn't make sense
if ( enc == wxFONTENCODING_DEFAULT )
{
// we don't have wxFONTENCODING_ASCII, so use the closest one
return wxFONTENCODING_ISO8859_1;
}
if ( enc != wxFONTENCODING_MAX )
{
return enc;
}
//else: return wxFONTENCODING_SYSTEM below
}
#endif // Win32/Unix
return wxFONTENCODING_SYSTEM;
}
/* static */
void wxLocale::AddLanguage(const wxLanguageInfo& info)
{
CreateLanguagesDB();
ms_languagesDB->Add(info);
}
/* static */
const wxLanguageInfo *wxLocale::GetLanguageInfo(int lang)
{
CreateLanguagesDB();
// calling GetLanguageInfo(wxLANGUAGE_DEFAULT) is a natural thing to do, so
// make it work
if ( lang == wxLANGUAGE_DEFAULT )
lang = GetSystemLanguage();
const size_t count = ms_languagesDB->GetCount();
for ( size_t i = 0; i < count; i++ )
{
if ( ms_languagesDB->Item(i).Language == lang )
{
// We need to create a temporary here in order to make this work with BCC in final build mode
wxLanguageInfo *ptr = &ms_languagesDB->Item(i);
return ptr;
}
}
return NULL;
}
/* static */