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