1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Internationalization and localisation for wxWidgets
4 // Author: Vadim Zeitlin
5 // Modified by: Michael N. Filippov <michael@idisys.iae.nsk.su>
6 // (2003/09/30 - plural forms support)
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
17 #include "wx/string.h"
21 #include "wx/fontenc.h"
23 // ============================================================================
25 // ============================================================================
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 // gettext() style macros (notice that xgettext should be invoked with
32 // --keyword="_" --keyword="wxPLURAL:1,2" options
33 // to extract the strings from the sources)
34 #ifndef WXINTL_NO_GETTEXT_MACRO
35 #define _(s) wxGetTranslation(_T(s))
36 #define wxPLURAL(sing, plur, n) wxGetTranslation(_T(sing), _T(plur), n)
39 // another one which just marks the strings for extraction, but doesn't
40 // perform the translation (use -kwxTRANSLATE with xgettext!)
41 #define wxTRANSLATE(str) _T(str)
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 class WXDLLIMPEXP_BASE wxLocale
;
48 class WXDLLIMPEXP_BASE wxLanguageInfoArray
;
51 // ============================================================================
53 // ============================================================================
55 // ----------------------------------------------------------------------------
56 // wxLanguage: defines all supported languages
57 // ----------------------------------------------------------------------------
59 // --- --- --- generated code begins here --- --- ---
61 // This enum is generated by misc/languages/genlang.py
62 // When making changes, please put them into misc/languages/langtabl.txt
65 // user's default/preffered language as got from OS:
67 // unknown language, if wxLocale::GetSystemLanguage fails:
76 wxLANGUAGE_ARABIC_ALGERIA
,
77 wxLANGUAGE_ARABIC_BAHRAIN
,
78 wxLANGUAGE_ARABIC_EGYPT
,
79 wxLANGUAGE_ARABIC_IRAQ
,
80 wxLANGUAGE_ARABIC_JORDAN
,
81 wxLANGUAGE_ARABIC_KUWAIT
,
82 wxLANGUAGE_ARABIC_LEBANON
,
83 wxLANGUAGE_ARABIC_LIBYA
,
84 wxLANGUAGE_ARABIC_MOROCCO
,
85 wxLANGUAGE_ARABIC_OMAN
,
86 wxLANGUAGE_ARABIC_QATAR
,
87 wxLANGUAGE_ARABIC_SAUDI_ARABIA
,
88 wxLANGUAGE_ARABIC_SUDAN
,
89 wxLANGUAGE_ARABIC_SYRIA
,
90 wxLANGUAGE_ARABIC_TUNISIA
,
91 wxLANGUAGE_ARABIC_UAE
,
92 wxLANGUAGE_ARABIC_YEMEN
,
97 wxLANGUAGE_AZERI_CYRILLIC
,
98 wxLANGUAGE_AZERI_LATIN
,
101 wxLANGUAGE_BELARUSIAN
,
107 wxLANGUAGE_BULGARIAN
,
109 wxLANGUAGE_CAMBODIAN
,
112 wxLANGUAGE_CHINESE_SIMPLIFIED
,
113 wxLANGUAGE_CHINESE_TRADITIONAL
,
114 wxLANGUAGE_CHINESE_HONGKONG
,
115 wxLANGUAGE_CHINESE_MACAU
,
116 wxLANGUAGE_CHINESE_SINGAPORE
,
117 wxLANGUAGE_CHINESE_TAIWAN
,
123 wxLANGUAGE_DUTCH_BELGIAN
,
125 wxLANGUAGE_ENGLISH_UK
,
126 wxLANGUAGE_ENGLISH_US
,
127 wxLANGUAGE_ENGLISH_AUSTRALIA
,
128 wxLANGUAGE_ENGLISH_BELIZE
,
129 wxLANGUAGE_ENGLISH_BOTSWANA
,
130 wxLANGUAGE_ENGLISH_CANADA
,
131 wxLANGUAGE_ENGLISH_CARIBBEAN
,
132 wxLANGUAGE_ENGLISH_DENMARK
,
133 wxLANGUAGE_ENGLISH_EIRE
,
134 wxLANGUAGE_ENGLISH_JAMAICA
,
135 wxLANGUAGE_ENGLISH_NEW_ZEALAND
,
136 wxLANGUAGE_ENGLISH_PHILIPPINES
,
137 wxLANGUAGE_ENGLISH_SOUTH_AFRICA
,
138 wxLANGUAGE_ENGLISH_TRINIDAD
,
139 wxLANGUAGE_ENGLISH_ZIMBABWE
,
140 wxLANGUAGE_ESPERANTO
,
147 wxLANGUAGE_FRENCH_BELGIAN
,
148 wxLANGUAGE_FRENCH_CANADIAN
,
149 wxLANGUAGE_FRENCH_LUXEMBOURG
,
150 wxLANGUAGE_FRENCH_MONACO
,
151 wxLANGUAGE_FRENCH_SWISS
,
156 wxLANGUAGE_GERMAN_AUSTRIAN
,
157 wxLANGUAGE_GERMAN_BELGIUM
,
158 wxLANGUAGE_GERMAN_LIECHTENSTEIN
,
159 wxLANGUAGE_GERMAN_LUXEMBOURG
,
160 wxLANGUAGE_GERMAN_SWISS
,
162 wxLANGUAGE_GREENLANDIC
,
168 wxLANGUAGE_HUNGARIAN
,
169 wxLANGUAGE_ICELANDIC
,
170 wxLANGUAGE_INDONESIAN
,
171 wxLANGUAGE_INTERLINGUA
,
172 wxLANGUAGE_INTERLINGUE
,
173 wxLANGUAGE_INUKTITUT
,
177 wxLANGUAGE_ITALIAN_SWISS
,
182 wxLANGUAGE_KASHMIRI_INDIA
,
185 wxLANGUAGE_KINYARWANDA
,
195 wxLANGUAGE_LITHUANIAN
,
196 wxLANGUAGE_MACEDONIAN
,
199 wxLANGUAGE_MALAYALAM
,
200 wxLANGUAGE_MALAY_BRUNEI_DARUSSALAM
,
201 wxLANGUAGE_MALAY_MALAYSIA
,
206 wxLANGUAGE_MOLDAVIAN
,
207 wxLANGUAGE_MONGOLIAN
,
210 wxLANGUAGE_NEPALI_INDIA
,
211 wxLANGUAGE_NORWEGIAN_BOKMAL
,
212 wxLANGUAGE_NORWEGIAN_NYNORSK
,
218 wxLANGUAGE_PORTUGUESE
,
219 wxLANGUAGE_PORTUGUESE_BRAZILIAN
,
222 wxLANGUAGE_RHAETO_ROMANCE
,
225 wxLANGUAGE_RUSSIAN_UKRAINE
,
229 wxLANGUAGE_SCOTS_GAELIC
,
231 wxLANGUAGE_SERBIAN_CYRILLIC
,
232 wxLANGUAGE_SERBIAN_LATIN
,
233 wxLANGUAGE_SERBO_CROATIAN
,
238 wxLANGUAGE_SINHALESE
,
241 wxLANGUAGE_SLOVENIAN
,
244 wxLANGUAGE_SPANISH_ARGENTINA
,
245 wxLANGUAGE_SPANISH_BOLIVIA
,
246 wxLANGUAGE_SPANISH_CHILE
,
247 wxLANGUAGE_SPANISH_COLOMBIA
,
248 wxLANGUAGE_SPANISH_COSTA_RICA
,
249 wxLANGUAGE_SPANISH_DOMINICAN_REPUBLIC
,
250 wxLANGUAGE_SPANISH_ECUADOR
,
251 wxLANGUAGE_SPANISH_EL_SALVADOR
,
252 wxLANGUAGE_SPANISH_GUATEMALA
,
253 wxLANGUAGE_SPANISH_HONDURAS
,
254 wxLANGUAGE_SPANISH_MEXICAN
,
255 wxLANGUAGE_SPANISH_MODERN
,
256 wxLANGUAGE_SPANISH_NICARAGUA
,
257 wxLANGUAGE_SPANISH_PANAMA
,
258 wxLANGUAGE_SPANISH_PARAGUAY
,
259 wxLANGUAGE_SPANISH_PERU
,
260 wxLANGUAGE_SPANISH_PUERTO_RICO
,
261 wxLANGUAGE_SPANISH_URUGUAY
,
262 wxLANGUAGE_SPANISH_US
,
263 wxLANGUAGE_SPANISH_VENEZUELA
,
264 wxLANGUAGE_SUNDANESE
,
267 wxLANGUAGE_SWEDISH_FINLAND
,
282 wxLANGUAGE_UKRAINIAN
,
284 wxLANGUAGE_URDU_INDIA
,
285 wxLANGUAGE_URDU_PAKISTAN
,
287 wxLANGUAGE_UZBEK_CYRILLIC
,
288 wxLANGUAGE_UZBEK_LATIN
,
289 wxLANGUAGE_VIETNAMESE
,
299 // for custom, user-defined languages:
300 wxLANGUAGE_USER_DEFINED
303 // --- --- --- generated code ends here --- --- ---
305 enum wxLayoutDirection
308 wxLayout_LeftToRight
,
312 // ----------------------------------------------------------------------------
313 // wxLanguageInfo: encapsulates wxLanguage to OS native lang.desc.
314 // translation information
315 // ----------------------------------------------------------------------------
317 struct WXDLLIMPEXP_BASE wxLanguageInfo
319 int Language
; // wxLanguage id
320 wxString CanonicalName
; // Canonical name, e.g. fr_FR
322 wxUint32 WinLang
, // Win32 language identifiers
325 wxString Description
; // human-readable name of the language
326 wxLayoutDirection LayoutDirection
;
329 // ----------------------------------------------------------------------------
330 // wxLocaleCategory: the category of locale settings
331 // ----------------------------------------------------------------------------
333 enum wxLocaleCategory
347 // ----------------------------------------------------------------------------
348 // wxLocaleInfo: the items understood by wxLocale::GetInfo()
349 // ----------------------------------------------------------------------------
353 // the thounsands separator
354 wxLOCALE_THOUSANDS_SEP
,
356 // the character used as decimal point
357 wxLOCALE_DECIMAL_POINT
361 // ----------------------------------------------------------------------------
362 // wxLocale: encapsulates all language dependent settings, including current
363 // message catalogs, date, time and currency formats (TODO) &c
364 // ----------------------------------------------------------------------------
366 enum wxLocaleInitFlags
368 wxLOCALE_LOAD_DEFAULT
= 0x0001, // load wxwin.mo?
369 wxLOCALE_CONV_ENCODING
= 0x0002 // convert encoding on the fly?
372 class WXDLLIMPEXP_BASE wxLocale
378 // call Init() if you use this ctor
379 wxLocale() { DoCommonInit(); }
381 // the ctor has a side effect of changing current locale
382 wxLocale(const wxChar
*szName
, // name (for messages)
383 const wxChar
*szShort
= (const wxChar
*) NULL
, // dir prefix (for msg files)
384 const wxChar
*szLocale
= (const wxChar
*) NULL
, // locale (for setlocale)
385 bool bLoadDefault
= true, // preload wxstd.mo?
386 bool bConvertEncoding
= false) // convert Win<->Unix if necessary?
390 Init(szName
, szShort
, szLocale
, bLoadDefault
, bConvertEncoding
);
393 wxLocale(int language
, // wxLanguage id or custom language
394 int flags
= wxLOCALE_LOAD_DEFAULT
| wxLOCALE_CONV_ENCODING
)
398 Init(language
, flags
);
401 // the same as a function (returns true on success)
402 bool Init(const wxChar
*szName
,
403 const wxChar
*szShort
= (const wxChar
*) NULL
,
404 const wxChar
*szLocale
= (const wxChar
*) NULL
,
405 bool bLoadDefault
= true,
406 bool bConvertEncoding
= false);
408 // same as second ctor (returns true on success)
409 bool Init(int language
= wxLANGUAGE_DEFAULT
,
410 int flags
= wxLOCALE_LOAD_DEFAULT
| wxLOCALE_CONV_ENCODING
);
412 // restores old locale
415 // Try to get user's (or OS's) preferred language setting.
416 // Return wxLANGUAGE_UNKNOWN if language-guessing algorithm failed
417 static int GetSystemLanguage();
419 // get the encoding used by default for text on this system, returns
420 // wxFONTENCODING_SYSTEM if it couldn't be determined
421 static wxFontEncoding
GetSystemEncoding();
423 // get the string describing the system encoding, return empty string if
424 // couldn't be determined
425 static wxString
GetSystemEncodingName();
427 // get the values of the given locale-dependent datum: the current locale
428 // is used, the US default value is returned if everything else fails
429 static wxString
GetInfo(wxLocaleInfo index
, wxLocaleCategory cat
);
431 // return true if the locale was set successfully
432 bool IsOk() const { return m_pszOldLocale
!= NULL
; }
434 // returns locale name
435 const wxChar
*GetLocale() const { return m_strLocale
; }
437 // return current locale wxLanguage value
438 int GetLanguage() const { return m_language
; }
440 // return locale name to be passed to setlocale()
441 wxString
GetSysName() const;
443 // return 'canonical' name, i.e. in the form of xx[_YY], where xx is
444 // language code according to ISO 639 and YY is country name
445 // as specified by ISO 3166.
446 wxString
GetCanonicalName() const { return m_strShort
; }
448 // add a prefix to the catalog lookup path: the message catalog files will be
449 // looked up under prefix/<lang>/LC_MESSAGES, prefix/LC_MESSAGES and prefix
452 // This only applies to subsequent invocations of AddCatalog()!
453 static void AddCatalogLookupPathPrefix(const wxString
& prefix
);
455 // add a catalog: it's searched for in standard places (current directory
456 // first, system one after), but the you may prepend additional directories to
457 // the search path with AddCatalogLookupPathPrefix().
459 // The loaded catalog will be used for message lookup by GetString().
461 // Returns 'true' if it was successfully loaded
462 bool AddCatalog(const wxChar
*szDomain
);
463 bool AddCatalog(const wxChar
*szDomain
,
464 wxLanguage msgIdLanguage
, const wxChar
*msgIdCharset
);
466 // check if the given catalog is loaded
467 bool IsLoaded(const wxChar
*szDomain
) const;
469 // Retrieve the language info struct for the given language
471 // Returns NULL if no info found, pointer must *not* be deleted by caller
472 static const wxLanguageInfo
*GetLanguageInfo(int lang
);
474 // Returns language name in English or empty string if the language
475 // is not in database
476 static wxString
GetLanguageName(int lang
);
478 // Find the language for the given locale string which may be either a
479 // canonical ISO 2 letter language code ("xx"), a language code followed by
480 // the country code ("xx_XX") or a Windows full language name ("Xxxxx...")
482 // Returns NULL if no info found, pointer must *not* be deleted by caller
483 static const wxLanguageInfo
*FindLanguageInfo(const wxString
& locale
);
485 // Add custom language to the list of known languages.
486 // Notes: 1) wxLanguageInfo contains platform-specific data
487 // 2) must be called before Init to have effect
488 static void AddLanguage(const wxLanguageInfo
& info
);
490 // retrieve the translation for a string in all loaded domains unless
491 // the szDomain parameter is specified (and then only this domain is
493 // n - additional parameter for PluralFormsParser
495 // return original string if translation is not available
496 // (in this case an error message is generated the first time
497 // a string is not found; use wxLogNull to suppress it)
499 // domains are searched in the last to first order, i.e. catalogs
500 // added later override those added before.
501 virtual const wxChar
*GetString(const wxChar
*szOrigString
,
502 const wxChar
*szDomain
= NULL
) const;
503 // plural form version of the same:
504 virtual const wxChar
*GetString(const wxChar
*szOrigString
,
505 const wxChar
*szOrigString2
,
507 const wxChar
*szDomain
= NULL
) const;
509 // Returns the current short name for the locale
510 const wxString
& GetName() const { return m_strShort
; }
512 // return the contents of .po file header
513 wxString
GetHeaderValue( const wxChar
* szHeader
,
514 const wxChar
* szDomain
= NULL
) const;
516 // These two methods are for internal use only. First one creates
517 // ms_languagesDB if it doesn't already exist, second one destroys
519 static void CreateLanguagesDB();
520 static void DestroyLanguagesDB();
523 // find catalog by name in a linked list, return NULL if !found
524 wxMsgCatalog
*FindCatalog(const wxChar
*szDomain
) const;
526 // copy default table of languages from global static array to
527 // m_langugagesInfo, called by InitLanguagesDB
528 static void InitLanguagesDB();
530 // initialize the member fields to default values
533 wxString m_strLocale
, // this locale name
534 m_strShort
; // short name for the locale
535 int m_language
; // this locale wxLanguage value
537 const wxChar
*m_pszOldLocale
; // previous locale from setlocale()
538 wxLocale
*m_pOldLocale
; // previous wxLocale
540 wxMsgCatalog
*m_pMsgCat
; // pointer to linked list of catalogs
542 bool m_bConvertEncoding
;
546 static wxLanguageInfoArray
*ms_languagesDB
;
548 DECLARE_NO_COPY_CLASS(wxLocale
)
551 // ----------------------------------------------------------------------------
553 // ----------------------------------------------------------------------------
555 // get the current locale object (note that it may be NULL!)
556 extern WXDLLIMPEXP_BASE wxLocale
* wxGetLocale();
558 // get the translation of the string in the current locale
559 inline const wxChar
*wxGetTranslation(const wxChar
*sz
, const wxChar
* domain
=NULL
)
561 wxLocale
*pLoc
= wxGetLocale();
563 return pLoc
->GetString(sz
, domain
);
567 inline const wxChar
*wxGetTranslation(const wxChar
*sz1
, const wxChar
*sz2
,
568 size_t n
, const wxChar
* domain
=NULL
)
570 wxLocale
*pLoc
= wxGetLocale();
572 return pLoc
->GetString(sz1
, sz2
, n
, domain
);
574 return n
== 1 ? sz1
: sz2
;
579 // the macros should still be defined - otherwise compilation would fail
581 #if !defined(WXINTL_NO_GETTEXT_MACRO)
587 #define wxTRANSLATE(str) _T(str)
589 // Note: use of 'inline' here can cause this symbol not to be found when compiled with gcc
590 //const wxChar *wxGetTranslation(const wxChar *sz);
591 #define wxGetTranslation(sz) (sz)
593 #endif // wxUSE_INTL/!wxUSE_INTL
595 // define this one just in case it occurs somewhere (instead of preferred
597 #if !defined(WXINTL_NO_GETTEXT_MACRO)
598 #if !defined(gettext_noop)
599 #define gettext_noop(str) _T(str)
606 #endif // _WX_INTL_H_