PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Externals/wxWidgets3/include/wx/translation.h

https://gitlab.com/Hexexpeck/dolphin-emulator
C Header | 329 lines | 180 code | 70 blank | 79 comment | 6 complexity | 94a8bb77a2b1254de46549b0ba8a084f MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/translation.h
  3. // Purpose: Internationalization and localisation for wxWidgets
  4. // Author: Vadim Zeitlin, Vaclav Slavik,
  5. // Michael N. Filippov <michael@idisys.iae.nsk.su>
  6. // Created: 2010-04-23
  7. // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  8. // (c) 2010 Vaclav Slavik <vslavik@fastmail.fm>
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_TRANSLATION_H_
  12. #define _WX_TRANSLATION_H_
  13. #include "wx/defs.h"
  14. #include "wx/string.h"
  15. #if wxUSE_INTL
  16. #include "wx/buffer.h"
  17. #include "wx/language.h"
  18. #include "wx/hashmap.h"
  19. #include "wx/strconv.h"
  20. #include "wx/scopedptr.h"
  21. // ============================================================================
  22. // global decls
  23. // ============================================================================
  24. // ----------------------------------------------------------------------------
  25. // macros
  26. // ----------------------------------------------------------------------------
  27. // gettext() style macros (notice that xgettext should be invoked with
  28. // --keyword="_" --keyword="wxPLURAL:1,2" options
  29. // to extract the strings from the sources)
  30. #ifndef WXINTL_NO_GETTEXT_MACRO
  31. #define _(s) wxGetTranslation((s))
  32. #define wxPLURAL(sing, plur, n) wxGetTranslation((sing), (plur), n)
  33. #endif
  34. // another one which just marks the strings for extraction, but doesn't
  35. // perform the translation (use -kwxTRANSLATE with xgettext!)
  36. #define wxTRANSLATE(str) str
  37. // ----------------------------------------------------------------------------
  38. // forward decls
  39. // ----------------------------------------------------------------------------
  40. class WXDLLIMPEXP_FWD_BASE wxArrayString;
  41. class WXDLLIMPEXP_FWD_BASE wxTranslationsLoader;
  42. class WXDLLIMPEXP_FWD_BASE wxLocale;
  43. class wxPluralFormsCalculator;
  44. wxDECLARE_SCOPED_PTR(wxPluralFormsCalculator, wxPluralFormsCalculatorPtr)
  45. // ----------------------------------------------------------------------------
  46. // wxMsgCatalog corresponds to one loaded message catalog.
  47. // ----------------------------------------------------------------------------
  48. class WXDLLIMPEXP_BASE wxMsgCatalog
  49. {
  50. public:
  51. // Ctor is protected, because CreateFromXXX functions must be used,
  52. // but destruction should be unrestricted
  53. #if !wxUSE_UNICODE
  54. ~wxMsgCatalog();
  55. #endif
  56. // load the catalog from disk or from data; caller is responsible for
  57. // deleting them if not NULL
  58. static wxMsgCatalog *CreateFromFile(const wxString& filename,
  59. const wxString& domain);
  60. static wxMsgCatalog *CreateFromData(const wxScopedCharBuffer& data,
  61. const wxString& domain);
  62. // get name of the catalog
  63. wxString GetDomain() const { return m_domain; }
  64. // get the translated string: returns NULL if not found
  65. const wxString *GetString(const wxString& sz, unsigned n = UINT_MAX) const;
  66. protected:
  67. wxMsgCatalog(const wxString& domain)
  68. : m_pNext(NULL), m_domain(domain)
  69. #if !wxUSE_UNICODE
  70. , m_conv(NULL)
  71. #endif
  72. {}
  73. private:
  74. // variable pointing to the next element in a linked list (or NULL)
  75. wxMsgCatalog *m_pNext;
  76. friend class wxTranslations;
  77. wxStringToStringHashMap m_messages; // all messages in the catalog
  78. wxString m_domain; // name of the domain
  79. #if !wxUSE_UNICODE
  80. // the conversion corresponding to this catalog charset if we installed it
  81. // as the global one
  82. wxCSConv *m_conv;
  83. #endif
  84. wxPluralFormsCalculatorPtr m_pluralFormsCalculator;
  85. };
  86. // ----------------------------------------------------------------------------
  87. // wxTranslations: message catalogs
  88. // ----------------------------------------------------------------------------
  89. // this class allows to get translations for strings
  90. class WXDLLIMPEXP_BASE wxTranslations
  91. {
  92. public:
  93. wxTranslations();
  94. ~wxTranslations();
  95. // returns current translations object, may return NULL
  96. static wxTranslations *Get();
  97. // sets current translations object (takes ownership; may be NULL)
  98. static void Set(wxTranslations *t);
  99. // changes loader to non-default one; takes ownership of 'loader'
  100. void SetLoader(wxTranslationsLoader *loader);
  101. void SetLanguage(wxLanguage lang);
  102. void SetLanguage(const wxString& lang);
  103. // get languages available for this app
  104. wxArrayString GetAvailableTranslations(const wxString& domain) const;
  105. // find best translation language for given domain
  106. wxString GetBestTranslation(const wxString& domain, wxLanguage msgIdLanguage);
  107. wxString GetBestTranslation(const wxString& domain,
  108. const wxString& msgIdLanguage = "en");
  109. // add standard wxWidgets catalog ("wxstd")
  110. bool AddStdCatalog();
  111. // add catalog with given domain name and language, looking it up via
  112. // wxTranslationsLoader
  113. bool AddCatalog(const wxString& domain);
  114. bool AddCatalog(const wxString& domain, wxLanguage msgIdLanguage);
  115. #if !wxUSE_UNICODE
  116. bool AddCatalog(const wxString& domain,
  117. wxLanguage msgIdLanguage,
  118. const wxString& msgIdCharset);
  119. #endif
  120. // check if the given catalog is loaded
  121. bool IsLoaded(const wxString& domain) const;
  122. // access to translations
  123. const wxString *GetTranslatedString(const wxString& origString,
  124. const wxString& domain = wxEmptyString) const;
  125. const wxString *GetTranslatedString(const wxString& origString,
  126. unsigned n,
  127. const wxString& domain = wxEmptyString) const;
  128. wxString GetHeaderValue(const wxString& header,
  129. const wxString& domain = wxEmptyString) const;
  130. // this is hack to work around a problem with wxGetTranslation() which
  131. // returns const wxString& and not wxString, so when it returns untranslated
  132. // string, it needs to have a copy of it somewhere
  133. static const wxString& GetUntranslatedString(const wxString& str);
  134. private:
  135. // perform loading of the catalog via m_loader
  136. bool LoadCatalog(const wxString& domain, const wxString& lang, const wxString& msgIdLang);
  137. // find catalog by name in a linked list, return NULL if !found
  138. wxMsgCatalog *FindCatalog(const wxString& domain) const;
  139. // same as Set(), without taking ownership; only for wxLocale
  140. static void SetNonOwned(wxTranslations *t);
  141. friend class wxLocale;
  142. private:
  143. wxString m_lang;
  144. wxTranslationsLoader *m_loader;
  145. wxMsgCatalog *m_pMsgCat; // pointer to linked list of catalogs
  146. // In addition to keeping all the catalogs in the linked list, we also
  147. // store them in a hash map indexed by the domain name to allow finding
  148. // them by name efficiently.
  149. WX_DECLARE_HASH_MAP(wxString, wxMsgCatalog *, wxStringHash, wxStringEqual, wxMsgCatalogMap);
  150. wxMsgCatalogMap m_catalogMap;
  151. };
  152. // abstraction of translations discovery and loading
  153. class WXDLLIMPEXP_BASE wxTranslationsLoader
  154. {
  155. public:
  156. wxTranslationsLoader() {}
  157. virtual ~wxTranslationsLoader() {}
  158. virtual wxMsgCatalog *LoadCatalog(const wxString& domain,
  159. const wxString& lang) = 0;
  160. virtual wxArrayString GetAvailableTranslations(const wxString& domain) const = 0;
  161. };
  162. // standard wxTranslationsLoader implementation, using filesystem
  163. class WXDLLIMPEXP_BASE wxFileTranslationsLoader
  164. : public wxTranslationsLoader
  165. {
  166. public:
  167. static void AddCatalogLookupPathPrefix(const wxString& prefix);
  168. virtual wxMsgCatalog *LoadCatalog(const wxString& domain,
  169. const wxString& lang) wxOVERRIDE;
  170. virtual wxArrayString GetAvailableTranslations(const wxString& domain) const wxOVERRIDE;
  171. };
  172. #ifdef __WINDOWS__
  173. // loads translations from win32 resources
  174. class WXDLLIMPEXP_BASE wxResourceTranslationsLoader
  175. : public wxTranslationsLoader
  176. {
  177. public:
  178. virtual wxMsgCatalog *LoadCatalog(const wxString& domain,
  179. const wxString& lang);
  180. virtual wxArrayString GetAvailableTranslations(const wxString& domain) const;
  181. protected:
  182. // returns resource type to use for translations
  183. virtual wxString GetResourceType() const { return "MOFILE"; }
  184. // returns module to load resources from
  185. virtual WXHINSTANCE GetModule() const { return 0; }
  186. };
  187. #endif // __WINDOWS__
  188. // ----------------------------------------------------------------------------
  189. // global functions
  190. // ----------------------------------------------------------------------------
  191. // get the translation of the string in the current locale
  192. inline const wxString& wxGetTranslation(const wxString& str,
  193. const wxString& domain = wxString())
  194. {
  195. wxTranslations *trans = wxTranslations::Get();
  196. const wxString *transStr = trans ? trans->GetTranslatedString(str, domain)
  197. : NULL;
  198. if ( transStr )
  199. return *transStr;
  200. else
  201. // NB: this function returns reference to a string, so we have to keep
  202. // a copy of it somewhere
  203. return wxTranslations::GetUntranslatedString(str);
  204. }
  205. inline const wxString& wxGetTranslation(const wxString& str1,
  206. const wxString& str2,
  207. unsigned n,
  208. const wxString& domain = wxString())
  209. {
  210. wxTranslations *trans = wxTranslations::Get();
  211. const wxString *transStr = trans ? trans->GetTranslatedString(str1, n, domain)
  212. : NULL;
  213. if ( transStr )
  214. return *transStr;
  215. else
  216. // NB: this function returns reference to a string, so we have to keep
  217. // a copy of it somewhere
  218. return n == 1
  219. ? wxTranslations::GetUntranslatedString(str1)
  220. : wxTranslations::GetUntranslatedString(str2);
  221. }
  222. #else // !wxUSE_INTL
  223. // the macros should still be defined - otherwise compilation would fail
  224. #if !defined(WXINTL_NO_GETTEXT_MACRO)
  225. #if !defined(_)
  226. #define _(s) (s)
  227. #endif
  228. #define wxPLURAL(sing, plur, n) ((n) == 1 ? (sing) : (plur))
  229. #endif
  230. #define wxTRANSLATE(str) str
  231. // NB: we use a template here in order to avoid using
  232. // wxLocale::GetUntranslatedString() above, which would be required if
  233. // we returned const wxString&; this way, the compiler should be able to
  234. // optimize wxGetTranslation() away
  235. template<typename TString>
  236. inline TString wxGetTranslation(TString str)
  237. { return str; }
  238. template<typename TString, typename TDomain>
  239. inline TString wxGetTranslation(TString str, TDomain WXUNUSED(domain))
  240. { return str; }
  241. template<typename TString, typename TDomain>
  242. inline TString wxGetTranslation(TString str1, TString str2, size_t n)
  243. { return n == 1 ? str1 : str2; }
  244. template<typename TString, typename TDomain>
  245. inline TString wxGetTranslation(TString str1, TString str2, size_t n,
  246. TDomain WXUNUSED(domain))
  247. { return n == 1 ? str1 : str2; }
  248. #endif // wxUSE_INTL/!wxUSE_INTL
  249. // define this one just in case it occurs somewhere (instead of preferred
  250. // wxTRANSLATE) too
  251. #if !defined(WXINTL_NO_GETTEXT_MACRO)
  252. #if !defined(gettext_noop)
  253. #define gettext_noop(str) (str)
  254. #endif
  255. #if !defined(N_)
  256. #define N_(s) (s)
  257. #endif
  258. #endif
  259. #endif // _WX_TRANSLATION_H_