translation.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: translation.h
  3. // Purpose: wxTranslation class
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. This class allows to get translations for strings.
  9. In wxWidgets this class manages message catalogs which contain the
  10. translations of the strings used to the current language. Unlike wxLocale,
  11. it isn't bound to locale. It can be used either independently of, or in
  12. conjunction with wxLocale. In the latter case, you should initialize
  13. wxLocale (which creates wxTranslations instance) first; in the former, you
  14. need to create a wxTranslations object and Set() it manually.
  15. Only one wxTranslations instance is active at a time; it is set with the
  16. Set() method and obtained using Get().
  17. Unlike wxLocale, wxTranslations' primary mean of identifying language
  18. is by its "canonical name", i.e. ISO 639 code, possibly combined with
  19. ISO 3166 country code and additional modifiers (examples include
  20. "fr", "en_GB" or "ca@valencia"; see wxLocale::GetCanonicalName() for
  21. more information). This allows apps using wxTranslations API to use even
  22. languages not recognized by the operating system or not listed in
  23. wxLanguage enum.
  24. @since 2.9.1
  25. @see wxLocale, wxTranslationsLoader, wxFileTranslationsLoader
  26. */
  27. class wxTranslations
  28. {
  29. public:
  30. /// Constructor
  31. wxTranslations();
  32. /**
  33. Returns current translations object, may return NULL.
  34. You must either call this early in app initialization code, or let
  35. wxLocale do it for you.
  36. */
  37. static wxTranslations *Get();
  38. /**
  39. Sets current translations object.
  40. Deletes previous translation object and takes ownership of @a t.
  41. */
  42. static void Set(wxTranslations *t);
  43. /**
  44. Changes loader use to read catalogs to a non-default one.
  45. Deletes previous loader and takes ownership of @a loader.
  46. @see wxTranslationsLoader, wxFileTranslationsLoader, wxResourceTranslationsLoader
  47. */
  48. void SetLoader(wxTranslationsLoader *loader);
  49. /**
  50. Sets translations language to use.
  51. wxLANGUAGE_DEFAULT has special meaning: best suitable translation,
  52. given user's preference and available translations, will be used.
  53. */
  54. void SetLanguage(wxLanguage lang);
  55. /**
  56. Sets translations language to use.
  57. Empty @a lang string has the same meaning as wxLANGUAGE_DEFAULT in
  58. SetLanguage(wxLanguage): best suitable translation, given user's
  59. preference and available translations, will be used.
  60. */
  61. void SetLanguage(const wxString& lang);
  62. /**
  63. Returns list of all translations of @a domain that were found.
  64. This method can be used e.g. to populate list of application's
  65. translations offered to the user. To do this, pass the app's main
  66. catalog as @a domain.
  67. @see GetBestTranslation()
  68. */
  69. wxArrayString GetAvailableTranslations(const wxString& domain) const;
  70. /**
  71. Returns the best UI language for the @a domain.
  72. The language is determined from the preferred UI language or languages
  73. list the user configured in the OS. Notice that this may or may not
  74. correspond to the default @em locale as obtained from
  75. wxLocale::GetSystemLanguage(); modern operation systems (Windows
  76. Vista+, OS X) have separate language and regional (= locale) settings.
  77. @param domain
  78. The catalog domain to look for.
  79. @param msgIdLanguage
  80. Specifies the language of "msgid" strings in source code
  81. (i.e. arguments to GetString(), wxGetTranslation() and the _() macro).
  82. @return Language code if a suitable match was found, empty string
  83. otherwise.
  84. @since 2.9.5
  85. */
  86. wxString GetBestTranslation(const wxString& domain, wxLanguage msgIdLanguage);
  87. /**
  88. Returns the best UI language for the @a domain.
  89. The language is determined from the preferred UI language or languages
  90. list the user configured in the OS. Notice that this may or may not
  91. correspond to the default @em locale as obtained from
  92. wxLocale::GetSystemLanguage(); modern operation systems (Windows
  93. Vista+, OS X) have separate language and regional (= locale) settings.
  94. @param domain
  95. The catalog domain to look for.
  96. @param msgIdLanguage
  97. Specifies the language of "msgid" strings in source code
  98. (i.e. arguments to GetString(), wxGetTranslation() and the _() macro).
  99. @return Language code if a suitable match was found, empty string
  100. otherwise.
  101. @since 2.9.5
  102. */
  103. wxString GetBestTranslation(const wxString& domain,
  104. const wxString& msgIdLanguage = "en");
  105. /**
  106. Add standard wxWidgets catalogs ("wxstd" and possible port-specific
  107. catalogs).
  108. @return @true if a suitable catalog was found, @false otherwise
  109. @see AddCatalog()
  110. */
  111. bool AddStdCatalog();
  112. /**
  113. Add a catalog for use with the current locale.
  114. By default, it is searched for in standard places (see
  115. wxFileTranslationsLoader), but you may also prepend additional
  116. directories to the search path with
  117. wxFileTranslationsLoader::AddCatalogLookupPathPrefix().
  118. All loaded catalogs will be used for message lookup by GetString() for
  119. the current locale.
  120. In this overload, @c msgid strings are assumed
  121. to be in English and written only using 7-bit ASCII characters.
  122. If you have to deal with non-English strings or 8-bit characters in the
  123. source code, see the instructions in @ref overview_nonenglish.
  124. @return
  125. @true if catalog was successfully loaded, @false otherwise (which might
  126. mean that the catalog is not found or that it isn't in the correct format).
  127. */
  128. bool AddCatalog(const wxString& domain);
  129. /**
  130. Same as AddCatalog(const wxString&), but takes an additional argument,
  131. @a msgIdLanguage.
  132. @param domain
  133. The catalog domain to add.
  134. @param msgIdLanguage
  135. Specifies the language of "msgid" strings in source code
  136. (i.e. arguments to GetString(), wxGetTranslation() and the _() macro).
  137. It is used if AddCatalog() cannot find any catalog for current language:
  138. if the language is same as source code language, then strings from source
  139. code are used instead.
  140. @return
  141. @true if catalog was successfully loaded, @false otherwise (which might
  142. mean that the catalog is not found or that it isn't in the correct format).
  143. */
  144. bool AddCatalog(const wxString& domain, wxLanguage msgIdLanguage);
  145. /**
  146. Same as AddCatalog(const wxString&, wxLanguage), but takes two
  147. additional arguments, @a msgIdLanguage and @a msgIdCharset.
  148. This overload is only available in non-Unicode build.
  149. @param domain
  150. The catalog domain to add.
  151. @param msgIdLanguage
  152. Specifies the language of "msgid" strings in source code
  153. (i.e. arguments to GetString(), wxGetTranslation() and the _() macro).
  154. It is used if AddCatalog() cannot find any catalog for current language:
  155. if the language is same as source code language, then strings from source
  156. code are used instead.
  157. @param msgIdCharset
  158. Lets you specify the charset used for msgids in sources
  159. in case they use 8-bit characters (e.g. German or French strings).
  160. @return
  161. @true if catalog was successfully loaded, @false otherwise (which might
  162. mean that the catalog is not found or that it isn't in the correct format).
  163. */
  164. bool AddCatalog(const wxString& domain,
  165. wxLanguage msgIdLanguage,
  166. const wxString& msgIdCharset);
  167. /**
  168. Check if the given catalog is loaded, and returns @true if it is.
  169. According to GNU gettext tradition, each catalog normally corresponds to
  170. 'domain' which is more or less the application name.
  171. @see AddCatalog()
  172. */
  173. bool IsLoaded(const wxString& domain) const;
  174. /**
  175. Retrieves the translation for a string in all loaded domains unless the @a domain
  176. parameter is specified (and then only this catalog/domain is searched).
  177. Returns @NULL if translation is not available.
  178. This function is thread-safe.
  179. @remarks Domains are searched in the last to first order, i.e. catalogs
  180. added later override those added before.
  181. @since 3.0
  182. */
  183. const wxString *GetTranslatedString(const wxString& origString,
  184. const wxString& domain = wxEmptyString) const;
  185. /**
  186. Retrieves the translation for a string in all loaded domains unless the @a domain
  187. parameter is specified (and then only this catalog/domain is searched).
  188. Returns @NULL if translation is not available.
  189. This form is used when retrieving translation of string that has different
  190. singular and plural form in English or different plural forms in some
  191. other language.
  192. @param origString The singular form of the string to be converted.
  193. @param n The number on which the plural form choice depends on.
  194. (In some languages, there are different plural forms
  195. for e.g. n=2 and n=3 etc., in addition to the singular
  196. form (n=1) being different.)
  197. @param domain The only domain (i.e. message catalog) to search if
  198. specified. By default this parameter is empty,
  199. indicating that all loaded catalogs should be
  200. searched.
  201. See GNU gettext manual for additional information on plural forms handling.
  202. This method is called by the wxGetTranslation() function and _() macro.
  203. This function is thread-safe.
  204. @remarks Domains are searched in the last to first order, i.e. catalogs
  205. added later override those added before.
  206. @since 3.0
  207. */
  208. const wxString *GetTranslatedString(const wxString& origString,
  209. unsigned n,
  210. const wxString& domain = wxEmptyString) const;
  211. /**
  212. Returns the header value for header @a header.
  213. The search for @a header is case sensitive. If an @a domain is passed,
  214. this domain is searched. Else all domains will be searched until a
  215. header has been found.
  216. The return value is the value of the header if found. Else this will be empty.
  217. */
  218. wxString GetHeaderValue(const wxString& header,
  219. const wxString& domain = wxEmptyString) const;
  220. };
  221. /**
  222. Abstraction of translations discovery and loading.
  223. This interface makes it possible to override wxWidgets' default catalogs
  224. loading mechanism and load MO files from locations other than the
  225. filesystem (e.g. embed them in executable).
  226. Implementations must implement the LoadCatalog() method.
  227. @see wxFileTranslationsLoader, wxResourceTranslationsLoader
  228. @since 2.9.1
  229. */
  230. class wxTranslationsLoader
  231. {
  232. public:
  233. /// Trivial default constructor.
  234. wxTranslationsLoader();
  235. /**
  236. Called to load requested catalog.
  237. If the catalog is found, LoadCatalog() should create wxMsgCatalog
  238. instance with its data and return it. The caller will take ownership
  239. of the catalog.
  240. @param domain Domain to load.
  241. @param lang Language to look for. This is "canonical name"
  242. (see wxLocale::GetCanonicalName()), i.e. ISO 639
  243. code, possibly combined with country code or
  244. additional modifiers (e.g. "fr", "en_GB" or
  245. "ca@valencia").
  246. @return Loaded catalog or NULL on failure.
  247. */
  248. virtual wxMsgCatalog *LoadCatalog(const wxString& domain,
  249. const wxString& lang) = 0;
  250. /**
  251. Implements wxTranslations::GetAvailableTranslations().
  252. */
  253. virtual wxArrayString GetAvailableTranslations(const wxString& domain) const = 0;
  254. };
  255. /**
  256. Standard wxTranslationsLoader implementation.
  257. This finds catalogs in the filesystem, using the standard Unix layout.
  258. This is the default unless you change the loader with
  259. wxTranslations::SetLoader().
  260. Catalogs are searched for in standard places (system locales directory,
  261. `LC_PATH` on Unix systems, Resources subdirectory of the application bundle
  262. on OS X, executable's directory on Windows), but you may also prepend
  263. additional directories to the search path with
  264. AddCatalogLookupPathPrefix().
  265. @since 2.9.1
  266. */
  267. class wxFileTranslationsLoader : public wxTranslationsLoader
  268. {
  269. public:
  270. /**
  271. Add a prefix to the catalog lookup path: the message catalog files will
  272. be looked up under prefix/lang/LC_MESSAGES and prefix/lang directories
  273. (in this order).
  274. This only applies to subsequent invocations of
  275. wxTranslations::AddCatalog().
  276. */
  277. static void AddCatalogLookupPathPrefix(const wxString& prefix);
  278. };
  279. /**
  280. This loader makes it possible to load translations from Windows
  281. resources.
  282. If you wish to store translation MO files in resources, you have to
  283. enable this loader before calling wxTranslations::AddCatalog() or
  284. wxLocale::AddCatalog():
  285. @code
  286. wxTranslations::Get()->SetLoader(new wxResourceTranslationsLoader);
  287. @endcode
  288. Translations are stored in resources as compiled MO files, with type
  289. set to "MOFILE" (unless you override GetResourceType()) and name
  290. consisting of the domain, followed by underscore, followed by language
  291. identification. For example, the relevant part of .rc file would look
  292. like this:
  293. @code
  294. myapp_de MOFILE "catalogs/de/myapp.mo"
  295. myapp_fr MOFILE "catalogs/fr/myapp.mo"
  296. myapp_en_GB MOFILE "catalogs/en_GB/myapp.mo"
  297. @endcode
  298. This class is only available on Windows.
  299. @since 2.9.1
  300. */
  301. class wxResourceTranslationsLoader : public wxTranslationsLoader
  302. {
  303. protected:
  304. /**
  305. Returns resource type to use for translations.
  306. Default type is "MOFILE".
  307. */
  308. virtual wxString GetResourceType() const;
  309. /**
  310. Returns handle of the module to load resources from.
  311. By default, the main executable is used.
  312. */
  313. virtual WXHINSTANCE GetModule() const;
  314. };
  315. /**
  316. Represents a loaded translations message catalog.
  317. This class should only be used directly by wxTranslationsLoader
  318. implementations.
  319. @since 2.9.1
  320. */
  321. class wxMsgCatalog
  322. {
  323. public:
  324. /**
  325. Creates catalog loaded from a MO file.
  326. @param filename Path to the MO file to load.
  327. @param domain Catalog's domain. This typically matches
  328. the @a filename.
  329. @return Successfully loaded catalog or NULL on failure.
  330. */
  331. static wxMsgCatalog *CreateFromFile(const wxString& filename,
  332. const wxString& domain);
  333. /**
  334. Creates catalog from MO file data in memory buffer.
  335. @param data Data in MO file format.
  336. @param domain Catalog's domain. This typically matches
  337. the @a filename.
  338. @return Successfully loaded catalog or NULL on failure.
  339. */
  340. static wxMsgCatalog *CreateFromData(const wxScopedCharBuffer& data,
  341. const wxString& domain);
  342. };
  343. // ============================================================================
  344. // Global functions/macros
  345. // ============================================================================
  346. /** @addtogroup group_funcmacro_string */
  347. //@{
  348. /**
  349. This macro is identical to _() but for the plural variant of
  350. wxGetTranslation().
  351. @return A const wxString.
  352. @header{wx/intl.h}
  353. */
  354. #define wxPLURAL(string, plural, n)
  355. /**
  356. This macro doesn't do anything in the program code -- it simply expands to
  357. the value of its argument.
  358. However it does have a purpose which is to mark the literal strings for the
  359. extraction into the message catalog created by @c xgettext program. Usually
  360. this is achieved using _() but that macro not only marks the string for
  361. extraction but also expands into a wxGetTranslation() call which means that
  362. it cannot be used in some situations, notably for static array
  363. initialization.
  364. Here is an example which should make it more clear: suppose that you have a
  365. static array of strings containing the weekday names and which have to be
  366. translated (note that it is a bad example, really, as wxDateTime already
  367. can be used to get the localized week day names already). If you write:
  368. @code
  369. static const char * const weekdays[] = { _("Mon"), ..., _("Sun") };
  370. ...
  371. // use weekdays[n] as usual
  372. @endcode
  373. The code wouldn't compile because the function calls are forbidden in the
  374. array initializer. So instead you should do this:
  375. @code
  376. static const char * const weekdays[] = { wxTRANSLATE("Mon"), ...,
  377. wxTRANSLATE("Sun") };
  378. ...
  379. // use wxGetTranslation(weekdays[n])
  380. @endcode
  381. Note that although the code @b would compile if you simply omit
  382. wxTRANSLATE() in the above, it wouldn't work as expected because there
  383. would be no translations for the weekday names in the program message
  384. catalog and wxGetTranslation() wouldn't find them.
  385. @return A const wxChar*.
  386. @header{wx/intl.h}
  387. */
  388. #define wxTRANSLATE(string)
  389. /**
  390. This function returns the translation of @a string in the current
  391. @c locale(). If the string is not found in any of the loaded message
  392. catalogs (see @ref overview_i18n), the original string is returned. In
  393. debug build, an error message is logged -- this should help to find the
  394. strings which were not yet translated. If @a domain is specified then only
  395. that domain/catalog is searched for a matching string. As this function is
  396. used very often, an alternative (and also common in Unix world) syntax is
  397. provided: the _() macro is defined to do the same thing as
  398. wxGetTranslation().
  399. This function is thread-safe.
  400. @note This function is not suitable for literal strings in Unicode builds
  401. since the literal strings must be enclosed in wxT() macro which makes
  402. them unrecognised by @c xgettext, and so they are not extracted to
  403. the message catalog. Instead, use the _() and wxPLURAL() macro for
  404. all literal strings.
  405. @see wxGetTranslation(const wxString&, const wxString&, unsigned, const wxString&)
  406. @header{wx/intl.h}
  407. */
  408. const wxString& wxGetTranslation(const wxString& string,
  409. const wxString& domain = wxEmptyString);
  410. /**
  411. This is an overloaded version of
  412. wxGetTranslation(const wxString&, const wxString&), please see its
  413. documentation for general information.
  414. This version is used when retrieving translation of string that has
  415. different singular and plural forms in English or different plural forms in
  416. some other language. Like wxGetTranslation(const wxString&,const wxString&),
  417. the @a string parameter must contain the singular form of the string to be
  418. converted and is used as the key for the search in the catalog. The
  419. @a plural parameter is the plural form (in English). The parameter @a n is
  420. used to determine the plural form. If no message catalog is found,
  421. @a string is returned if "n == 1", otherwise @a plural is returned.
  422. See GNU gettext Manual for additional information on plural forms handling:
  423. <http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms>
  424. For a shorter alternative see the wxPLURAL() macro.
  425. This function is thread-safe.
  426. @header{wx/intl.h}
  427. */
  428. const wxString& wxGetTranslation(const wxString& string,
  429. const wxString& plural, unsigned n,
  430. const wxString& domain = wxEmptyString);
  431. /**
  432. Macro to be used around all literal strings that should be translated.
  433. This macro expands into a call to wxGetTranslation(), so it marks the
  434. message for the extraction by @c xgettext just as wxTRANSLATE() does, but
  435. also returns the translation of the string for the current locale during
  436. execution.
  437. This macro is thread-safe.
  438. @header{wx/intl.h}
  439. */
  440. const wxString& _(const wxString& string);
  441. //@}