intl.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: intl.h
  3. // Purpose: interface of wxLocale
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. This is the layout direction stored in wxLanguageInfo and returned by
  9. wxApp::GetLayoutDirection(), wxWindow::GetLayoutDirection(),
  10. wxDC::GetLayoutDirection() for RTL (right-to-left) languages support.
  11. */
  12. enum wxLayoutDirection
  13. {
  14. wxLayout_Default,
  15. wxLayout_LeftToRight,
  16. wxLayout_RightToLeft
  17. };
  18. /**
  19. Encapsulates a ::wxLanguage identifier together with OS-specific information
  20. related to that language.
  21. @beginWxPerlOnly
  22. In wxPerl @c Wx::LanguageInfo has only one method:
  23. - Wx::LanguageInfo->new(language, canonicalName, WinLang, WinSubLang, Description)
  24. @endWxPerlOnly
  25. */
  26. struct wxLanguageInfo
  27. {
  28. /// ::wxLanguage id.
  29. /// It should be greater than @c wxLANGUAGE_USER_DEFINED when defining your own
  30. /// language info structure.
  31. int Language;
  32. /// Canonical name of the language, e.g. @c fr_FR.
  33. wxString CanonicalName;
  34. //@{
  35. /**
  36. Win32 language identifiers (LANG_xxxx, SUBLANG_xxxx).
  37. @onlyfor{wxmsw}
  38. */
  39. wxUint32 WinLang, WinSublang;
  40. //@}
  41. /// Human-readable name of the language.
  42. wxString Description;
  43. /// The layout direction used for this language.
  44. wxLayoutDirection LayoutDirection;
  45. /// Return the LCID corresponding to this language.
  46. /// @onlyfor{wxmsw}
  47. wxUint32 GetLCID() const;
  48. /// Return the locale name corresponding to this language usable with
  49. /// @c setlocale() on the current system.
  50. wxString GetLocaleName() const;
  51. };
  52. /**
  53. The category of locale settings.
  54. @see wxLocale::GetInfo()
  55. */
  56. enum wxLocaleCategory
  57. {
  58. /// Number formatting.
  59. wxLOCALE_CAT_NUMBER,
  60. /// Date/time formatting.
  61. wxLOCALE_CAT_DATE,
  62. /// Monetary values formatting.
  63. wxLOCALE_CAT_MONEY,
  64. /**
  65. Default category for the wxLocaleInfo value.
  66. This category can be used for values which only make sense for a single
  67. category, e.g. wxLOCALE_SHORT_DATE_FMT which can only be used with
  68. wxLOCALE_CAT_DATE. As this is the default value of the second parameter
  69. of wxLocale::GetInfo(), wxLOCALE_CAT_DATE can be omitted when asking
  70. for wxLOCALE_SHORT_DATE_FMT value.
  71. @since 2.9.0
  72. */
  73. wxLOCALE_CAT_DEFAULT
  74. };
  75. /**
  76. The values understood by wxLocale::GetInfo().
  77. Note that for the @c wxLOCALE_*_FMT constants (the date and time formats),
  78. the strings returned by wxLocale::GetInfo() use strftime() or,
  79. equivalently, wxDateTime::Format() format. If the relevant format
  80. couldn't be determined, an empty string is returned -- there is no
  81. fallback value so that the application could determine the best course
  82. of actions itself in such case.
  83. All of these values are used with @c wxLOCALE_CAT_DATE in wxLocale::GetInfo() or,
  84. more typically, with @c wxLOCALE_CAT_DEFAULT as they only apply to a single category.
  85. */
  86. enum wxLocaleInfo
  87. {
  88. /**
  89. The thousands separator.
  90. This value can be used with either wxLOCALE_CAT_NUMBER or
  91. wxLOCALE_CAT_MONEY categories.
  92. */
  93. wxLOCALE_THOUSANDS_SEP,
  94. /**
  95. The character used as decimal point.
  96. This value can be used with either wxLOCALE_CAT_NUMBER or
  97. wxLOCALE_CAT_MONEY categories.
  98. */
  99. wxLOCALE_DECIMAL_POINT,
  100. /**
  101. Short date format.
  102. Notice that short and long date formats may be the same under POSIX
  103. systems currently but may, and typically are, different under MSW or OS X.
  104. @since 2.9.0
  105. */
  106. wxLOCALE_SHORT_DATE_FMT,
  107. /**
  108. Long date format.
  109. @since 2.9.0
  110. */
  111. wxLOCALE_LONG_DATE_FMT,
  112. /**
  113. Date and time format.
  114. @since 2.9.0
  115. */
  116. wxLOCALE_DATE_TIME_FMT,
  117. /**
  118. Time format.
  119. @since 2.9.0
  120. */
  121. wxLOCALE_TIME_FMT
  122. };
  123. /**
  124. @class wxLocale
  125. wxLocale class encapsulates all language-dependent settings and is a
  126. generalization of the C locale concept.
  127. In wxWidgets this class manages current locale. It also initializes and
  128. activates wxTranslations object that manages message catalogs.
  129. For a list of the supported languages, please see ::wxLanguage enum values.
  130. These constants may be used to specify the language in wxLocale::Init and
  131. are returned by wxLocale::GetSystemLanguage.
  132. @beginWxPerlOnly
  133. In wxPerl you can't use the '_' function name, so
  134. the @c Wx::Locale module can export the @c gettext and
  135. @c gettext_noop under any given name.
  136. @code
  137. # this imports gettext ( equivalent to Wx::GetTranslation
  138. # and gettext_noop ( a noop )
  139. # into your module
  140. use Wx::Locale qw(:default);
  141. # ....
  142. # use the functions
  143. print gettext( "Panic!" );
  144. button = Wx::Button-new( window, -1, gettext( "Label" ) );
  145. @endcode
  146. If you need to translate a lot of strings, then adding gettext( ) around
  147. each one is a long task ( that is why _( ) was introduced ), so just choose
  148. a shorter name for gettext:
  149. @code
  150. use Wx::Locale 'gettext' = 't',
  151. 'gettext_noop' = 'gettext_noop';
  152. # ...
  153. # use the functions
  154. print t( "Panic!!" );
  155. # ...
  156. @endcode
  157. @endWxPerlOnly
  158. @library{wxbase}
  159. @category{cfg}
  160. @see @ref overview_i18n, @ref page_samples_internat, wxXLocale, wxTranslations
  161. */
  162. class wxLocale
  163. {
  164. public:
  165. /**
  166. This is the default constructor and it does nothing to initialize the object:
  167. Init() must be used to do that.
  168. */
  169. wxLocale();
  170. /**
  171. See Init() for parameters description.
  172. */
  173. wxLocale(int language, int flags = wxLOCALE_LOAD_DEFAULT);
  174. /**
  175. See Init() for parameters description.
  176. The call of this function has several global side effects which you should
  177. understand: first of all, the application locale is changed - note that this
  178. will affect many of standard C library functions such as printf() or strftime().
  179. Second, this wxLocale object becomes the new current global locale for the
  180. application and so all subsequent calls to ::wxGetTranslation() will try to
  181. translate the messages using the message catalogs for this locale.
  182. */
  183. wxLocale(const wxString& name,
  184. const wxString& shortName = wxEmptyString,
  185. const wxString& locale = wxEmptyString,
  186. bool bLoadDefault = true);
  187. /**
  188. The destructor, like the constructor, also has global side effects: the
  189. previously set locale is restored and so the changes described in
  190. Init() documentation are rolled back.
  191. */
  192. virtual ~wxLocale();
  193. /**
  194. Calls wxTranslations::AddCatalog(const wxString&).
  195. */
  196. bool AddCatalog(const wxString& domain);
  197. /**
  198. Calls wxTranslations::AddCatalog(const wxString&, wxLanguage).
  199. */
  200. bool AddCatalog(const wxString& domain, wxLanguage msgIdLanguage);
  201. /**
  202. Calls wxTranslations::AddCatalog(const wxString&, wxLanguage, const wxString&).
  203. */
  204. bool AddCatalog(const wxString& domain, wxLanguage msgIdLanguage,
  205. const wxString& msgIdCharset);
  206. /**
  207. Calls wxFileTranslationsLoader::AddCatalogLookupPathPrefix().
  208. */
  209. static void AddCatalogLookupPathPrefix(const wxString& prefix);
  210. /**
  211. Adds custom, user-defined language to the database of known languages.
  212. This database is used in conjunction with the first form of Init().
  213. */
  214. static void AddLanguage(const wxLanguageInfo& info);
  215. /**
  216. This function may be used to find the language description structure for the
  217. given locale, specified either as a two letter ISO language code (for example,
  218. "pt"), a language code followed by the country code ("pt_BR") or a full, human
  219. readable, language description ("Portuguese-Brazil").
  220. Returns the information for the given language or @NULL if this language
  221. is unknown. Note that even if the returned pointer is valid, the caller
  222. should @e not delete it.
  223. @see GetLanguageInfo()
  224. */
  225. static const wxLanguageInfo* FindLanguageInfo(const wxString& locale);
  226. /**
  227. Returns the canonical form of current locale name. Canonical form is the
  228. one that is used on UNIX systems: it is a two- or five-letter string in xx or
  229. xx_YY format, where xx is ISO 639 code of language and YY is ISO 3166 code of
  230. the country. Examples are "en", "en_GB", "en_US" or "fr_FR".
  231. This form is internally used when looking up message catalogs.
  232. Compare GetSysName().
  233. */
  234. wxString GetCanonicalName() const;
  235. /**
  236. Calls wxTranslations::GetHeaderValue().
  237. */
  238. wxString GetHeaderValue(const wxString& header,
  239. const wxString& domain = wxEmptyString) const;
  240. /**
  241. Returns the ::wxLanguage constant of current language.
  242. Note that you can call this function only if you used the form of
  243. Init() that takes ::wxLanguage argument.
  244. */
  245. int GetLanguage() const;
  246. /**
  247. Returns a pointer to wxLanguageInfo structure containing information about
  248. the given language or @NULL if this language is unknown. Note that even if
  249. the returned pointer is valid, the caller should @e not delete it.
  250. See AddLanguage() for the wxLanguageInfo description.
  251. As with Init(), @c wxLANGUAGE_DEFAULT has the special meaning if passed
  252. as an argument to this function and in this case the result of
  253. GetSystemLanguage() is used.
  254. */
  255. static const wxLanguageInfo* GetLanguageInfo(int lang);
  256. /**
  257. Returns English name of the given language or empty string if this
  258. language is unknown.
  259. See GetLanguageInfo() for a remark about special meaning of @c wxLANGUAGE_DEFAULT.
  260. */
  261. static wxString GetLanguageName(int lang);
  262. /**
  263. Returns canonical name (see GetCanonicalName()) of the given language
  264. or empty string if this language is unknown.
  265. See GetLanguageInfo() for a remark about special meaning of @c wxLANGUAGE_DEFAULT.
  266. @since 2.9.1
  267. */
  268. static wxString GetLanguageCanonicalName(int lang);
  269. /**
  270. Returns the locale name as passed to the constructor or Init().
  271. This is a full, human-readable name, e.g. "English" or "French".
  272. */
  273. const wxString& GetLocale() const;
  274. /**
  275. Returns the current short name for the locale (as given to the constructor or
  276. the Init() function).
  277. */
  278. const wxString& GetName() const;
  279. /**
  280. Calls wxGetTranslation(const wxString&, const wxString&).
  281. */
  282. virtual const wxString& GetString(const wxString& origString,
  283. const wxString& domain = wxEmptyString) const;
  284. /**
  285. Calls wxGetTranslation(const wxString&, const wxString&, unsigned, const wxString&).
  286. */
  287. virtual const wxString& GetString(const wxString& origString,
  288. const wxString& origString2, unsigned n,
  289. const wxString& domain = wxEmptyString) const;
  290. /**
  291. Returns current platform-specific locale name as passed to setlocale().
  292. Compare GetCanonicalName().
  293. */
  294. wxString GetSysName() const;
  295. /**
  296. Tries to detect the user's default font encoding.
  297. Returns wxFontEncoding() value or @c wxFONTENCODING_SYSTEM if it
  298. couldn't be determined.
  299. */
  300. static wxFontEncoding GetSystemEncoding();
  301. /**
  302. Tries to detect the name of the user's default font encoding.
  303. This string isn't particularly useful for the application as its form is
  304. platform-dependent and so you should probably use GetSystemEncoding() instead.
  305. Returns a user-readable string value or an empty string if it couldn't be
  306. determined.
  307. */
  308. static wxString GetSystemEncodingName();
  309. /**
  310. Tries to detect the user's default locale setting.
  311. Returns the ::wxLanguage value or @c wxLANGUAGE_UNKNOWN if the language-guessing
  312. algorithm failed.
  313. @note This function works with @em locales and returns the user's default
  314. locale. This may be, and usually is, the same as their preferred UI
  315. language, but it's not the same thing. Use wxTranslation to obtain
  316. @em language information.
  317. @see wxTranslations::GetBestTranslation().
  318. */
  319. static int GetSystemLanguage();
  320. /**
  321. Get the values of the given locale-dependent datum.
  322. This function returns the value of the locale-specific option specified
  323. by the given @a index.
  324. @param index
  325. One of the elements of wxLocaleInfo enum.
  326. @param cat
  327. The category to use with the given index or wxLOCALE_CAT_DEFAULT if
  328. the index can only apply to a single category.
  329. @return
  330. The option value or empty string if the function failed.
  331. */
  332. static wxString GetInfo(wxLocaleInfo index,
  333. wxLocaleCategory cat = wxLOCALE_CAT_DEFAULT);
  334. /**
  335. Initializes the wxLocale instance.
  336. The call of this function has several global side effects which you should
  337. understand: first of all, the application locale is changed - note that
  338. this will affect many of standard C library functions such as printf()
  339. or strftime().
  340. Second, this wxLocale object becomes the new current global locale for
  341. the application and so all subsequent calls to wxGetTranslation() will
  342. try to translate the messages using the message catalogs for this locale.
  343. @param language
  344. ::wxLanguage identifier of the locale.
  345. @c wxLANGUAGE_DEFAULT has special meaning -- wxLocale will use system's
  346. default language (see GetSystemLanguage()).
  347. @param flags
  348. Combination of the following:
  349. - wxLOCALE_LOAD_DEFAULT: Load the message catalog for the given locale
  350. containing the translations of standard wxWidgets messages
  351. automatically.
  352. - wxLOCALE_DONT_LOAD_DEFAULT: Negation of wxLOCALE_LOAD_DEFAULT.
  353. @return @true on success or @false if the given locale couldn't be set.
  354. */
  355. bool Init(int language = wxLANGUAGE_DEFAULT,
  356. int flags = wxLOCALE_LOAD_DEFAULT);
  357. /**
  358. @deprecated
  359. This form is deprecated, use the other one unless you know what you are doing.
  360. @param name
  361. The name of the locale. Only used in diagnostic messages.
  362. @param shortName
  363. The standard 2 letter locale abbreviation; it is used as the
  364. directory prefix when looking for the message catalog files.
  365. @param locale
  366. The parameter for the call to setlocale().
  367. Note that it is platform-specific.
  368. @param bLoadDefault
  369. May be set to @false to prevent loading of the message catalog for the
  370. given locale containing the translations of standard wxWidgets messages.
  371. This parameter would be rarely used in normal circumstances.
  372. */
  373. bool Init(const wxString& name, const wxString& shortName = wxEmptyString,
  374. const wxString& locale = wxEmptyString, bool bLoadDefault = true);
  375. /**
  376. Check whether the operating system and/or C run time environment supports
  377. this locale. For example in Windows 2000 and Windows XP, support for many
  378. locales is not installed by default. Returns @true if the locale is
  379. supported.
  380. The argument @a lang is the ::wxLanguage identifier. To obtain this for a
  381. given a two letter ISO language code, use FindLanguageInfo() to obtain its
  382. wxLanguageInfo structure.
  383. See AddLanguage() for the wxLanguageInfo description.
  384. @since 2.7.1.
  385. */
  386. static bool IsAvailable(int lang);
  387. /**
  388. Calls wxTranslations::IsLoaded().
  389. */
  390. bool IsLoaded(const wxString& domain) const;
  391. /**
  392. Returns @true if the locale could be set successfully.
  393. */
  394. bool IsOk() const;
  395. };