intl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/intl.h
  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)
  7. // Created: 29/01/98
  8. // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_INTL_H_
  12. #define _WX_INTL_H_
  13. #include "wx/defs.h"
  14. #include "wx/string.h"
  15. #include "wx/translation.h"
  16. // Make wxLayoutDirection enum available without need for wxUSE_INTL so wxWindow, wxApp
  17. // and other classes are not distrubed by wxUSE_INTL
  18. enum wxLayoutDirection
  19. {
  20. wxLayout_Default,
  21. wxLayout_LeftToRight,
  22. wxLayout_RightToLeft
  23. };
  24. #if wxUSE_INTL
  25. #include "wx/fontenc.h"
  26. #include "wx/language.h"
  27. // ============================================================================
  28. // global decls
  29. // ============================================================================
  30. // ----------------------------------------------------------------------------
  31. // macros
  32. // ----------------------------------------------------------------------------
  33. // ----------------------------------------------------------------------------
  34. // forward decls
  35. // ----------------------------------------------------------------------------
  36. class WXDLLIMPEXP_FWD_BASE wxLocale;
  37. class WXDLLIMPEXP_FWD_BASE wxLanguageInfoArray;
  38. // ============================================================================
  39. // locale support
  40. // ============================================================================
  41. // ----------------------------------------------------------------------------
  42. // wxLanguageInfo: encapsulates wxLanguage to OS native lang.desc.
  43. // translation information
  44. // ----------------------------------------------------------------------------
  45. struct WXDLLIMPEXP_BASE wxLanguageInfo
  46. {
  47. int Language; // wxLanguage id
  48. wxString CanonicalName; // Canonical name, e.g. fr_FR
  49. #ifdef __WINDOWS__
  50. wxUint32 WinLang, // Win32 language identifiers
  51. WinSublang;
  52. #endif // __WINDOWS__
  53. wxString Description; // human-readable name of the language
  54. wxLayoutDirection LayoutDirection;
  55. #ifdef __WINDOWS__
  56. // return the LCID corresponding to this language
  57. wxUint32 GetLCID() const;
  58. #endif // __WINDOWS__
  59. // return the locale name corresponding to this language usable with
  60. // setlocale() on the current system
  61. wxString GetLocaleName() const;
  62. };
  63. // for Unix systems GetLocaleName() is trivial so implement it inline here, for
  64. // MSW it's implemented in intl.cpp
  65. #ifndef __WINDOWS__
  66. inline wxString wxLanguageInfo::GetLocaleName() const { return CanonicalName; }
  67. #endif // !__WINDOWS__
  68. // ----------------------------------------------------------------------------
  69. // wxLocaleCategory: the category of locale settings
  70. // ----------------------------------------------------------------------------
  71. enum wxLocaleCategory
  72. {
  73. // (any) numbers
  74. wxLOCALE_CAT_NUMBER,
  75. // date/time
  76. wxLOCALE_CAT_DATE,
  77. // monetary value
  78. wxLOCALE_CAT_MONEY,
  79. // default category for wxLocaleInfo values which only apply to a single
  80. // category (e.g. wxLOCALE_SHORT_DATE_FMT)
  81. wxLOCALE_CAT_DEFAULT,
  82. wxLOCALE_CAT_MAX
  83. };
  84. // ----------------------------------------------------------------------------
  85. // wxLocaleInfo: the items understood by wxLocale::GetInfo()
  86. // ----------------------------------------------------------------------------
  87. enum wxLocaleInfo
  88. {
  89. // the thousands separator (for wxLOCALE_CAT_NUMBER or MONEY)
  90. wxLOCALE_THOUSANDS_SEP,
  91. // the character used as decimal point (for wxLOCALE_CAT_NUMBER or MONEY)
  92. wxLOCALE_DECIMAL_POINT,
  93. // the stftime()-formats used for short/long date and time representations
  94. // (under some platforms short and long date formats are the same)
  95. //
  96. // NB: these elements should appear in this order, code in GetInfo() relies
  97. // on it
  98. wxLOCALE_SHORT_DATE_FMT,
  99. wxLOCALE_LONG_DATE_FMT,
  100. wxLOCALE_DATE_TIME_FMT,
  101. wxLOCALE_TIME_FMT
  102. };
  103. // ----------------------------------------------------------------------------
  104. // wxLocale: encapsulates all language dependent settings, including current
  105. // message catalogs, date, time and currency formats (TODO) &c
  106. // ----------------------------------------------------------------------------
  107. enum wxLocaleInitFlags
  108. {
  109. wxLOCALE_DONT_LOAD_DEFAULT = 0x0000, // don't load wxwin.mo
  110. wxLOCALE_LOAD_DEFAULT = 0x0001 // load wxwin.mo?
  111. #if WXWIN_COMPATIBILITY_2_8
  112. ,wxLOCALE_CONV_ENCODING = 0x0002 // no longer used, simply remove
  113. // it from the existing code
  114. #endif
  115. };
  116. class WXDLLIMPEXP_BASE wxLocale
  117. {
  118. public:
  119. // ctor & dtor
  120. // -----------
  121. // call Init() if you use this ctor
  122. wxLocale() { DoCommonInit(); }
  123. // the ctor has a side effect of changing current locale
  124. wxLocale(const wxString& name, // name (for messages)
  125. const wxString& shortName = wxEmptyString, // dir prefix (for msg files)
  126. const wxString& locale = wxEmptyString, // locale (for setlocale)
  127. bool bLoadDefault = true // preload wxstd.mo?
  128. #if WXWIN_COMPATIBILITY_2_8
  129. ,bool bConvertEncoding = true // convert Win<->Unix if necessary?
  130. #endif
  131. )
  132. {
  133. DoCommonInit();
  134. #if WXWIN_COMPATIBILITY_2_8
  135. Init(name, shortName, locale, bLoadDefault, bConvertEncoding);
  136. #else
  137. Init(name, shortName, locale, bLoadDefault);
  138. #endif
  139. }
  140. wxLocale(int language, // wxLanguage id or custom language
  141. int flags = wxLOCALE_LOAD_DEFAULT)
  142. {
  143. DoCommonInit();
  144. Init(language, flags);
  145. }
  146. // the same as a function (returns true on success)
  147. bool Init(const wxString& name,
  148. const wxString& shortName = wxEmptyString,
  149. const wxString& locale = wxEmptyString,
  150. bool bLoadDefault = true
  151. #if WXWIN_COMPATIBILITY_2_8
  152. ,bool bConvertEncoding = true
  153. #endif
  154. );
  155. // same as second ctor (returns true on success)
  156. bool Init(int language = wxLANGUAGE_DEFAULT,
  157. int flags = wxLOCALE_LOAD_DEFAULT);
  158. // restores old locale
  159. virtual ~wxLocale();
  160. // Try to get user's (or OS's) preferred language setting.
  161. // Return wxLANGUAGE_UNKNOWN if language-guessing algorithm failed
  162. static int GetSystemLanguage();
  163. // get the encoding used by default for text on this system, returns
  164. // wxFONTENCODING_SYSTEM if it couldn't be determined
  165. static wxFontEncoding GetSystemEncoding();
  166. // get the string describing the system encoding, return empty string if
  167. // couldn't be determined
  168. static wxString GetSystemEncodingName();
  169. // get the values of the given locale-dependent datum: the current locale
  170. // is used, the US default value is returned if everything else fails
  171. static wxString GetInfo(wxLocaleInfo index,
  172. wxLocaleCategory cat = wxLOCALE_CAT_DEFAULT);
  173. // return true if the locale was set successfully
  174. bool IsOk() const { return m_pszOldLocale != NULL; }
  175. // returns locale name
  176. const wxString& GetLocale() const { return m_strLocale; }
  177. // return current locale wxLanguage value
  178. int GetLanguage() const { return m_language; }
  179. // return locale name to be passed to setlocale()
  180. wxString GetSysName() const;
  181. // return 'canonical' name, i.e. in the form of xx[_YY], where xx is
  182. // language code according to ISO 639 and YY is country name
  183. // as specified by ISO 3166.
  184. wxString GetCanonicalName() const { return m_strShort; }
  185. // add a prefix to the catalog lookup path: the message catalog files will be
  186. // looked up under prefix/<lang>/LC_MESSAGES, prefix/LC_MESSAGES and prefix
  187. // (in this order).
  188. //
  189. // This only applies to subsequent invocations of AddCatalog()!
  190. static void AddCatalogLookupPathPrefix(const wxString& prefix)
  191. { wxFileTranslationsLoader::AddCatalogLookupPathPrefix(prefix); }
  192. // add a catalog: it's searched for in standard places (current directory
  193. // first, system one after), but the you may prepend additional directories to
  194. // the search path with AddCatalogLookupPathPrefix().
  195. //
  196. // The loaded catalog will be used for message lookup by GetString().
  197. //
  198. // Returns 'true' if it was successfully loaded
  199. bool AddCatalog(const wxString& domain);
  200. bool AddCatalog(const wxString& domain, wxLanguage msgIdLanguage);
  201. bool AddCatalog(const wxString& domain,
  202. wxLanguage msgIdLanguage, const wxString& msgIdCharset);
  203. // check if the given locale is provided by OS and C run time
  204. static bool IsAvailable(int lang);
  205. // check if the given catalog is loaded
  206. bool IsLoaded(const wxString& domain) const;
  207. // Retrieve the language info struct for the given language
  208. //
  209. // Returns NULL if no info found, pointer must *not* be deleted by caller
  210. static const wxLanguageInfo *GetLanguageInfo(int lang);
  211. // Returns language name in English or empty string if the language
  212. // is not in database
  213. static wxString GetLanguageName(int lang);
  214. // Returns ISO code ("canonical name") of language or empty string if the
  215. // language is not in database
  216. static wxString GetLanguageCanonicalName(int lang);
  217. // Find the language for the given locale string which may be either a
  218. // canonical ISO 2 letter language code ("xx"), a language code followed by
  219. // the country code ("xx_XX") or a Windows full language name ("Xxxxx...")
  220. //
  221. // Returns NULL if no info found, pointer must *not* be deleted by caller
  222. static const wxLanguageInfo *FindLanguageInfo(const wxString& locale);
  223. // Add custom language to the list of known languages.
  224. // Notes: 1) wxLanguageInfo contains platform-specific data
  225. // 2) must be called before Init to have effect
  226. static void AddLanguage(const wxLanguageInfo& info);
  227. // retrieve the translation for a string in all loaded domains unless
  228. // the szDomain parameter is specified (and then only this domain is
  229. // searched)
  230. // n - additional parameter for PluralFormsParser
  231. //
  232. // return original string if translation is not available
  233. // (in this case an error message is generated the first time
  234. // a string is not found; use wxLogNull to suppress it)
  235. //
  236. // domains are searched in the last to first order, i.e. catalogs
  237. // added later override those added before.
  238. const wxString& GetString(const wxString& origString,
  239. const wxString& domain = wxEmptyString) const
  240. {
  241. return wxGetTranslation(origString, domain);
  242. }
  243. // plural form version of the same:
  244. const wxString& GetString(const wxString& origString,
  245. const wxString& origString2,
  246. unsigned n,
  247. const wxString& domain = wxEmptyString) const
  248. {
  249. return wxGetTranslation(origString, origString2, n, domain);
  250. }
  251. // Returns the current short name for the locale
  252. const wxString& GetName() const { return m_strShort; }
  253. // return the contents of .po file header
  254. wxString GetHeaderValue(const wxString& header,
  255. const wxString& domain = wxEmptyString) const;
  256. // These two methods are for internal use only. First one creates
  257. // ms_languagesDB if it doesn't already exist, second one destroys
  258. // it.
  259. static void CreateLanguagesDB();
  260. static void DestroyLanguagesDB();
  261. private:
  262. bool DoInit(const wxString& name,
  263. const wxString& shortName,
  264. const wxString& locale);
  265. // copy default table of languages from global static array to
  266. // m_langugagesInfo, called by InitLanguagesDB
  267. static void InitLanguagesDB();
  268. // initialize the member fields to default values
  269. void DoCommonInit();
  270. wxString m_strLocale, // this locale name
  271. m_strShort; // short name for the locale
  272. int m_language; // this locale wxLanguage value
  273. const char *m_pszOldLocale; // previous locale from setlocale()
  274. wxLocale *m_pOldLocale; // previous wxLocale
  275. bool m_initialized;
  276. wxTranslations m_translations;
  277. static wxLanguageInfoArray *ms_languagesDB;
  278. wxDECLARE_NO_COPY_CLASS(wxLocale);
  279. };
  280. // ----------------------------------------------------------------------------
  281. // global functions
  282. // ----------------------------------------------------------------------------
  283. // get the current locale object (note that it may be NULL!)
  284. extern WXDLLIMPEXP_BASE wxLocale* wxGetLocale();
  285. #endif // wxUSE_INTL
  286. #endif // _WX_INTL_H_