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"
19 // Make wxLayoutDirection enum available without need for wxUSE_INTL so wxWindow, wxApp
20 // and other classes are not distrubed by wxUSE_INTL
22 enum wxLayoutDirection
31 #include "wx/fontenc.h"
33 // ============================================================================
35 // ============================================================================
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // gettext() style macros (notice that xgettext should be invoked with
42 // --keyword="_" --keyword="wxPLURAL:1,2" options
43 // to extract the strings from the sources)
44 #ifndef WXINTL_NO_GETTEXT_MACRO
45 #define _(s) wxGetTranslation((s))
46 #define wxPLURAL(sing, plur, n) wxGetTranslation((sing), (plur), n)
49 // another one which just marks the strings for extraction, but doesn't
50 // perform the translation (use -kwxTRANSLATE with xgettext!)
51 #define wxTRANSLATE(str) str
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class WXDLLIMPEXP_FWD_BASE wxLocale
;
58 class WXDLLIMPEXP_FWD_BASE wxLanguageInfoArray
;
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
66 // wxLanguage: defines all supported languages
67 // ----------------------------------------------------------------------------
69 // --- --- --- generated code begins here --- --- ---
71 // This enum is generated by misc/languages/genlang.py
72 // When making changes, please put them into misc/languages/langtabl.txt
75 // user's default/preffered language as got from OS:
77 // unknown language, if wxLocale::GetSystemLanguage fails:
86 wxLANGUAGE_ARABIC_ALGERIA
,
87 wxLANGUAGE_ARABIC_BAHRAIN
,
88 wxLANGUAGE_ARABIC_EGYPT
,
89 wxLANGUAGE_ARABIC_IRAQ
,
90 wxLANGUAGE_ARABIC_JORDAN
,
91 wxLANGUAGE_ARABIC_KUWAIT
,
92 wxLANGUAGE_ARABIC_LEBANON
,
93 wxLANGUAGE_ARABIC_LIBYA
,
94 wxLANGUAGE_ARABIC_MOROCCO
,
95 wxLANGUAGE_ARABIC_OMAN
,
96 wxLANGUAGE_ARABIC_QATAR
,
97 wxLANGUAGE_ARABIC_SAUDI_ARABIA
,
98 wxLANGUAGE_ARABIC_SUDAN
,
99 wxLANGUAGE_ARABIC_SYRIA
,
100 wxLANGUAGE_ARABIC_TUNISIA
,
101 wxLANGUAGE_ARABIC_UAE
,
102 wxLANGUAGE_ARABIC_YEMEN
,
107 wxLANGUAGE_AZERI_CYRILLIC
,
108 wxLANGUAGE_AZERI_LATIN
,
111 wxLANGUAGE_BELARUSIAN
,
117 wxLANGUAGE_BULGARIAN
,
119 wxLANGUAGE_CAMBODIAN
,
122 wxLANGUAGE_CHINESE_SIMPLIFIED
,
123 wxLANGUAGE_CHINESE_TRADITIONAL
,
124 wxLANGUAGE_CHINESE_HONGKONG
,
125 wxLANGUAGE_CHINESE_MACAU
,
126 wxLANGUAGE_CHINESE_SINGAPORE
,
127 wxLANGUAGE_CHINESE_TAIWAN
,
133 wxLANGUAGE_DUTCH_BELGIAN
,
135 wxLANGUAGE_ENGLISH_UK
,
136 wxLANGUAGE_ENGLISH_US
,
137 wxLANGUAGE_ENGLISH_AUSTRALIA
,
138 wxLANGUAGE_ENGLISH_BELIZE
,
139 wxLANGUAGE_ENGLISH_BOTSWANA
,
140 wxLANGUAGE_ENGLISH_CANADA
,
141 wxLANGUAGE_ENGLISH_CARIBBEAN
,
142 wxLANGUAGE_ENGLISH_DENMARK
,
143 wxLANGUAGE_ENGLISH_EIRE
,
144 wxLANGUAGE_ENGLISH_JAMAICA
,
145 wxLANGUAGE_ENGLISH_NEW_ZEALAND
,
146 wxLANGUAGE_ENGLISH_PHILIPPINES
,
147 wxLANGUAGE_ENGLISH_SOUTH_AFRICA
,
148 wxLANGUAGE_ENGLISH_TRINIDAD
,
149 wxLANGUAGE_ENGLISH_ZIMBABWE
,
150 wxLANGUAGE_ESPERANTO
,
157 wxLANGUAGE_FRENCH_BELGIAN
,
158 wxLANGUAGE_FRENCH_CANADIAN
,
159 wxLANGUAGE_FRENCH_LUXEMBOURG
,
160 wxLANGUAGE_FRENCH_MONACO
,
161 wxLANGUAGE_FRENCH_SWISS
,
166 wxLANGUAGE_GERMAN_AUSTRIAN
,
167 wxLANGUAGE_GERMAN_BELGIUM
,
168 wxLANGUAGE_GERMAN_LIECHTENSTEIN
,
169 wxLANGUAGE_GERMAN_LUXEMBOURG
,
170 wxLANGUAGE_GERMAN_SWISS
,
172 wxLANGUAGE_GREENLANDIC
,
178 wxLANGUAGE_HUNGARIAN
,
179 wxLANGUAGE_ICELANDIC
,
180 wxLANGUAGE_INDONESIAN
,
181 wxLANGUAGE_INTERLINGUA
,
182 wxLANGUAGE_INTERLINGUE
,
183 wxLANGUAGE_INUKTITUT
,
187 wxLANGUAGE_ITALIAN_SWISS
,
192 wxLANGUAGE_KASHMIRI_INDIA
,
195 wxLANGUAGE_KINYARWANDA
,
205 wxLANGUAGE_LITHUANIAN
,
206 wxLANGUAGE_MACEDONIAN
,
209 wxLANGUAGE_MALAYALAM
,
210 wxLANGUAGE_MALAY_BRUNEI_DARUSSALAM
,
211 wxLANGUAGE_MALAY_MALAYSIA
,
216 wxLANGUAGE_MOLDAVIAN
,
217 wxLANGUAGE_MONGOLIAN
,
220 wxLANGUAGE_NEPALI_INDIA
,
221 wxLANGUAGE_NORWEGIAN_BOKMAL
,
222 wxLANGUAGE_NORWEGIAN_NYNORSK
,
228 wxLANGUAGE_PORTUGUESE
,
229 wxLANGUAGE_PORTUGUESE_BRAZILIAN
,
232 wxLANGUAGE_RHAETO_ROMANCE
,
235 wxLANGUAGE_RUSSIAN_UKRAINE
,
239 wxLANGUAGE_SCOTS_GAELIC
,
241 wxLANGUAGE_SERBIAN_CYRILLIC
,
242 wxLANGUAGE_SERBIAN_LATIN
,
243 wxLANGUAGE_SERBO_CROATIAN
,
248 wxLANGUAGE_SINHALESE
,
251 wxLANGUAGE_SLOVENIAN
,
254 wxLANGUAGE_SPANISH_ARGENTINA
,
255 wxLANGUAGE_SPANISH_BOLIVIA
,
256 wxLANGUAGE_SPANISH_CHILE
,
257 wxLANGUAGE_SPANISH_COLOMBIA
,
258 wxLANGUAGE_SPANISH_COSTA_RICA
,
259 wxLANGUAGE_SPANISH_DOMINICAN_REPUBLIC
,
260 wxLANGUAGE_SPANISH_ECUADOR
,
261 wxLANGUAGE_SPANISH_EL_SALVADOR
,
262 wxLANGUAGE_SPANISH_GUATEMALA
,
263 wxLANGUAGE_SPANISH_HONDURAS
,
264 wxLANGUAGE_SPANISH_MEXICAN
,
265 wxLANGUAGE_SPANISH_MODERN
,
266 wxLANGUAGE_SPANISH_NICARAGUA
,
267 wxLANGUAGE_SPANISH_PANAMA
,
268 wxLANGUAGE_SPANISH_PARAGUAY
,
269 wxLANGUAGE_SPANISH_PERU
,
270 wxLANGUAGE_SPANISH_PUERTO_RICO
,
271 wxLANGUAGE_SPANISH_URUGUAY
,
272 wxLANGUAGE_SPANISH_US
,
273 wxLANGUAGE_SPANISH_VENEZUELA
,
274 wxLANGUAGE_SUNDANESE
,
277 wxLANGUAGE_SWEDISH_FINLAND
,
292 wxLANGUAGE_UKRAINIAN
,
294 wxLANGUAGE_URDU_INDIA
,
295 wxLANGUAGE_URDU_PAKISTAN
,
297 wxLANGUAGE_UZBEK_CYRILLIC
,
298 wxLANGUAGE_UZBEK_LATIN
,
299 wxLANGUAGE_VIETNAMESE
,
309 // for custom, user-defined languages:
310 wxLANGUAGE_USER_DEFINED
313 // --- --- --- generated code ends here --- --- ---
315 // ----------------------------------------------------------------------------
316 // wxLanguageInfo: encapsulates wxLanguage to OS native lang.desc.
317 // translation information
318 // ----------------------------------------------------------------------------
320 struct WXDLLIMPEXP_BASE wxLanguageInfo
322 int Language
; // wxLanguage id
323 wxString CanonicalName
; // Canonical name, e.g. fr_FR
325 wxUint32 WinLang
, // Win32 language identifiers
328 wxString Description
; // human-readable name of the language
329 wxLayoutDirection LayoutDirection
;
332 // return the LCID corresponding to this language
333 wxUint32
GetLCID() const;
337 // ----------------------------------------------------------------------------
338 // wxLocaleCategory: the category of locale settings
339 // ----------------------------------------------------------------------------
341 enum wxLocaleCategory
355 // ----------------------------------------------------------------------------
356 // wxLocaleInfo: the items understood by wxLocale::GetInfo()
357 // ----------------------------------------------------------------------------
361 // the thounsands separator
362 wxLOCALE_THOUSANDS_SEP
,
364 // the character used as decimal point
365 wxLOCALE_DECIMAL_POINT
369 // ----------------------------------------------------------------------------
370 // wxLocale: encapsulates all language dependent settings, including current
371 // message catalogs, date, time and currency formats (TODO) &c
372 // ----------------------------------------------------------------------------
374 enum wxLocaleInitFlags
376 wxLOCALE_LOAD_DEFAULT
= 0x0001, // load wxwin.mo?
377 wxLOCALE_CONV_ENCODING
= 0x0002 // convert encoding on the fly?
380 class WXDLLIMPEXP_BASE wxLocale
386 // call Init() if you use this ctor
387 wxLocale() { DoCommonInit(); }
389 // the ctor has a side effect of changing current locale
390 wxLocale(const wxString
& name
, // name (for messages)
391 const wxString
& shortName
= wxEmptyString
, // dir prefix (for msg files)
392 const wxString
& locale
= wxEmptyString
, // locale (for setlocale)
393 bool bLoadDefault
= true, // preload wxstd.mo?
394 bool bConvertEncoding
= false) // convert Win<->Unix if necessary?
398 Init(name
, shortName
, locale
, bLoadDefault
, bConvertEncoding
);
401 wxLocale(int language
, // wxLanguage id or custom language
402 int flags
= wxLOCALE_LOAD_DEFAULT
| wxLOCALE_CONV_ENCODING
)
406 Init(language
, flags
);
409 // the same as a function (returns true on success)
410 bool Init(const wxString
& name
,
411 const wxString
& shortName
= wxEmptyString
,
412 const wxString
& locale
= wxEmptyString
,
413 bool bLoadDefault
= true,
414 bool bConvertEncoding
= false);
416 // same as second ctor (returns true on success)
417 bool Init(int language
= wxLANGUAGE_DEFAULT
,
418 int flags
= wxLOCALE_LOAD_DEFAULT
| wxLOCALE_CONV_ENCODING
);
420 // restores old locale
423 // Try to get user's (or OS's) preferred language setting.
424 // Return wxLANGUAGE_UNKNOWN if language-guessing algorithm failed
425 static int GetSystemLanguage();
427 // get the encoding used by default for text on this system, returns
428 // wxFONTENCODING_SYSTEM if it couldn't be determined
429 static wxFontEncoding
GetSystemEncoding();
431 // get the string describing the system encoding, return empty string if
432 // couldn't be determined
433 static wxString
GetSystemEncodingName();
435 // get the values of the given locale-dependent datum: the current locale
436 // is used, the US default value is returned if everything else fails
437 static wxString
GetInfo(wxLocaleInfo index
, wxLocaleCategory cat
);
439 // return true if the locale was set successfully
440 bool IsOk() const { return m_pszOldLocale
!= NULL
; }
442 // returns locale name
443 const wxString
& GetLocale() const { return m_strLocale
; }
445 // return current locale wxLanguage value
446 int GetLanguage() const { return m_language
; }
448 // return locale name to be passed to setlocale()
449 wxString
GetSysName() const;
451 // return 'canonical' name, i.e. in the form of xx[_YY], where xx is
452 // language code according to ISO 639 and YY is country name
453 // as specified by ISO 3166.
454 wxString
GetCanonicalName() const { return m_strShort
; }
456 // add a prefix to the catalog lookup path: the message catalog files will be
457 // looked up under prefix/<lang>/LC_MESSAGES, prefix/LC_MESSAGES and prefix
460 // This only applies to subsequent invocations of AddCatalog()!
461 static void AddCatalogLookupPathPrefix(const wxString
& prefix
);
463 // add a catalog: it's searched for in standard places (current directory
464 // first, system one after), but the you may prepend additional directories to
465 // the search path with AddCatalogLookupPathPrefix().
467 // The loaded catalog will be used for message lookup by GetString().
469 // Returns 'true' if it was successfully loaded
470 bool AddCatalog(const wxString
& domain
);
471 bool AddCatalog(const wxString
& domain
,
472 wxLanguage msgIdLanguage
, const wxString
& msgIdCharset
);
474 // check if the given locale is provided by OS and C run time
475 static bool IsAvailable(int lang
);
477 // check if the given catalog is loaded
478 bool IsLoaded(const wxString
& domain
) const;
480 // Retrieve the language info struct for the given language
482 // Returns NULL if no info found, pointer must *not* be deleted by caller
483 static const wxLanguageInfo
*GetLanguageInfo(int lang
);
485 // Returns language name in English or empty string if the language
486 // is not in database
487 static wxString
GetLanguageName(int lang
);
489 // Find the language for the given locale string which may be either a
490 // canonical ISO 2 letter language code ("xx"), a language code followed by
491 // the country code ("xx_XX") or a Windows full language name ("Xxxxx...")
493 // Returns NULL if no info found, pointer must *not* be deleted by caller
494 static const wxLanguageInfo
*FindLanguageInfo(const wxString
& locale
);
496 // Add custom language to the list of known languages.
497 // Notes: 1) wxLanguageInfo contains platform-specific data
498 // 2) must be called before Init to have effect
499 static void AddLanguage(const wxLanguageInfo
& info
);
501 // retrieve the translation for a string in all loaded domains unless
502 // the szDomain parameter is specified (and then only this domain is
504 // n - additional parameter for PluralFormsParser
506 // return original string if translation is not available
507 // (in this case an error message is generated the first time
508 // a string is not found; use wxLogNull to suppress it)
510 // domains are searched in the last to first order, i.e. catalogs
511 // added later override those added before.
512 virtual const wxString
& GetString(const wxString
& origString
,
513 const wxString
& domain
= wxEmptyString
) const;
514 // plural form version of the same:
515 virtual const wxString
& GetString(const wxString
& origString
,
516 const wxString
& origString2
,
518 const wxString
& domain
= wxEmptyString
) const;
520 // this is hack to work around a problem with wxGetTranslation() which
521 // returns const wxString& and not wxString, so when it returns untranslated
522 // string, it needs to have a copy of it somewhere
523 static const wxString
& GetUntranslatedString(const wxString
& str
);
525 // Returns the current short name for the locale
526 const wxString
& GetName() const { return m_strShort
; }
528 // return the contents of .po file header
529 wxString
GetHeaderValue(const wxString
& header
,
530 const wxString
& domain
= wxEmptyString
) const;
532 // These two methods are for internal use only. First one creates
533 // ms_languagesDB if it doesn't already exist, second one destroys
535 static void CreateLanguagesDB();
536 static void DestroyLanguagesDB();
539 // find catalog by name in a linked list, return NULL if !found
540 wxMsgCatalog
*FindCatalog(const wxString
& domain
) const;
542 // copy default table of languages from global static array to
543 // m_langugagesInfo, called by InitLanguagesDB
544 static void InitLanguagesDB();
546 // initialize the member fields to default values
549 wxString m_strLocale
, // this locale name
550 m_strShort
; // short name for the locale
551 int m_language
; // this locale wxLanguage value
553 const char *m_pszOldLocale
; // previous locale from setlocale()
554 wxLocale
*m_pOldLocale
; // previous wxLocale
556 wxMsgCatalog
*m_pMsgCat
; // pointer to linked list of catalogs
558 bool m_bConvertEncoding
;
562 static wxLanguageInfoArray
*ms_languagesDB
;
564 DECLARE_NO_COPY_CLASS(wxLocale
)
567 // ----------------------------------------------------------------------------
569 // ----------------------------------------------------------------------------
571 // get the current locale object (note that it may be NULL!)
572 extern WXDLLIMPEXP_BASE wxLocale
* wxGetLocale();
574 // get the translation of the string in the current locale
575 inline const wxString
& wxGetTranslation(const wxString
& str
,
576 const wxString
& domain
= wxEmptyString
)
578 wxLocale
*pLoc
= wxGetLocale();
580 return pLoc
->GetString(str
, domain
);
582 // NB: this function returns reference to a string, so we have to keep
583 // a copy of it somewhere
584 return wxLocale::GetUntranslatedString(str
);
586 inline const wxString
& wxGetTranslation(const wxString
& str1
,
587 const wxString
& str2
,
589 const wxString
& domain
= wxEmptyString
)
591 wxLocale
*pLoc
= wxGetLocale();
593 return pLoc
->GetString(str1
, str2
, n
, domain
);
595 // NB: this function returns reference to a string, so we have to keep
596 // a copy of it somewhere
598 ? wxLocale::GetUntranslatedString(str1
)
599 : wxLocale::GetUntranslatedString(str2
);
604 // the macros should still be defined - otherwise compilation would fail
606 #if !defined(WXINTL_NO_GETTEXT_MACRO)
610 #define wxPLURAL(sing, plur, n) ((n) == 1 ? (sing) : (plur))
613 #define wxTRANSLATE(str) str
615 // NB: we use a template here in order to avoid using
616 // wxLocale::GetUntranslatedString() above, which would be required if
617 // we returned const wxString&; this way, the compiler should be able to
618 // optimize wxGetTranslation() away
620 template<typename TString
>
621 inline TString
wxGetTranslation(TString str
)
624 template<typename TString
, typename TDomain
>
625 inline TString
wxGetTranslation(TString str
, TDomain
WXUNUSED(domain
))
628 template<typename TString
, typename TDomain
>
629 inline TString
wxGetTranslation(TString str1
, TString str2
, size_t n
)
630 { return n
== 1 ? str1
: str2
; }
632 template<typename TString
, typename TDomain
>
633 inline TString
wxGetTranslation(TString str1
, TString str2
, size_t n
,
634 TDomain
WXUNUSED(domain
))
635 { return n
== 1 ? str1
: str2
; }
637 #endif // wxUSE_INTL/!wxUSE_INTL
639 // define this one just in case it occurs somewhere (instead of preferred
641 #if !defined(WXINTL_NO_GETTEXT_MACRO)
642 #if !defined(gettext_noop)
643 #define gettext_noop(str) (str)
650 #endif // _WX_INTL_H_