]>
Commit | Line | Data |
---|---|---|
ea144923 VS |
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 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // (c) 2010 Vaclav Slavik <vslavik@fastmail.fm> | |
10 | // Licence: wxWindows licence | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | #ifndef _WX_TRANSLATION_H_ | |
14 | #define _WX_TRANSLATION_H_ | |
15 | ||
16 | #include "wx/defs.h" | |
17 | #include "wx/string.h" | |
18 | ||
19 | #if wxUSE_INTL | |
20 | ||
21 | #include "wx/language.h" | |
22 | ||
23 | #if !wxUSE_UNICODE | |
24 | #include "wx/hashmap.h" | |
25 | #endif | |
26 | ||
27 | // ============================================================================ | |
28 | // global decls | |
29 | // ============================================================================ | |
30 | ||
31 | // ---------------------------------------------------------------------------- | |
32 | // macros | |
33 | // ---------------------------------------------------------------------------- | |
34 | ||
35 | // gettext() style macros (notice that xgettext should be invoked with | |
36 | // --keyword="_" --keyword="wxPLURAL:1,2" options | |
37 | // to extract the strings from the sources) | |
38 | #ifndef WXINTL_NO_GETTEXT_MACRO | |
39 | #define _(s) wxGetTranslation((s)) | |
40 | #define wxPLURAL(sing, plur, n) wxGetTranslation((sing), (plur), n) | |
41 | #endif | |
42 | ||
43 | // another one which just marks the strings for extraction, but doesn't | |
44 | // perform the translation (use -kwxTRANSLATE with xgettext!) | |
45 | #define wxTRANSLATE(str) str | |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // forward decls | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | class WXDLLIMPEXP_FWD_BASE wxTranslationsLoader; | |
52 | class WXDLLIMPEXP_FWD_BASE wxLocale; | |
53 | class wxMsgCatalog; | |
54 | ||
55 | // ---------------------------------------------------------------------------- | |
56 | // wxTranslations: message catalogs | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
59 | // this class allows to get translations for strings | |
60 | class WXDLLIMPEXP_BASE wxTranslations | |
61 | { | |
62 | public: | |
63 | wxTranslations(); | |
64 | ~wxTranslations(); | |
65 | ||
66 | // returns current translations object, may return NULL | |
67 | static wxTranslations *Get(); | |
68 | // sets current translations object (takes ownership; may be NULL) | |
69 | static void Set(wxTranslations *t); | |
70 | ||
71 | // changes loader to non-default one; takes ownership of 'loader' | |
72 | void SetLoader(wxTranslationsLoader *loader); | |
73 | ||
74 | void SetLanguage(wxLanguage lang); | |
75 | void SetLanguage(const wxString& lang); | |
76 | ||
77 | // add standard wxWidgets catalog ("wxstd") | |
78 | bool AddStdCatalog(); | |
79 | ||
80 | // add catalog with given domain name and language, looking it up via | |
81 | // wxTranslationsLoader | |
82 | bool AddCatalog(const wxString& domain); | |
83 | bool AddCatalog(const wxString& domain, wxLanguage msgIdLanguage); | |
84 | #if !wxUSE_UNICODE | |
85 | bool AddCatalog(const wxString& domain, | |
86 | wxLanguage msgIdLanguage, | |
87 | const wxString& msgIdCharset); | |
88 | #endif | |
89 | ||
90 | // check if the given catalog is loaded | |
91 | bool IsLoaded(const wxString& domain) const; | |
92 | ||
93 | // load catalog data directly from file | |
94 | bool LoadCatalogFile(const wxString& filename, | |
95 | const wxString& domain = wxEmptyString); | |
96 | ||
97 | // access to translations | |
98 | const wxString& GetString(const wxString& origString, | |
99 | const wxString& domain = wxEmptyString) const; | |
100 | const wxString& GetString(const wxString& origString, | |
101 | const wxString& origString2, | |
102 | size_t n, | |
103 | const wxString& domain = wxEmptyString) const; | |
104 | ||
105 | wxString GetHeaderValue(const wxString& header, | |
106 | const wxString& domain = wxEmptyString) const; | |
107 | ||
108 | // this is hack to work around a problem with wxGetTranslation() which | |
109 | // returns const wxString& and not wxString, so when it returns untranslated | |
110 | // string, it needs to have a copy of it somewhere | |
111 | static const wxString& GetUntranslatedString(const wxString& str); | |
112 | ||
113 | private: | |
114 | // find best translation for given domain | |
115 | wxString ChooseLanguageForDomain(const wxString& domain, | |
116 | const wxString& msgIdLang); | |
117 | ||
118 | // find catalog by name in a linked list, return NULL if !found | |
119 | wxMsgCatalog *FindCatalog(const wxString& domain) const; | |
120 | ||
121 | // same as Set(), without taking ownership; only for wxLocale | |
122 | static void SetNonOwned(wxTranslations *t); | |
123 | friend class wxLocale; | |
124 | ||
125 | private: | |
126 | wxString m_lang; | |
127 | wxTranslationsLoader *m_loader; | |
128 | ||
129 | wxMsgCatalog *m_pMsgCat; // pointer to linked list of catalogs | |
130 | ||
131 | #if !wxUSE_UNICODE | |
132 | wxStringToStringHashMap m_msgIdCharset; | |
133 | #endif | |
134 | }; | |
135 | ||
136 | ||
137 | // abstraction of translations discovery and loading | |
138 | class WXDLLIMPEXP_BASE wxTranslationsLoader | |
139 | { | |
140 | public: | |
141 | wxTranslationsLoader() {} | |
142 | virtual ~wxTranslationsLoader() {} | |
143 | ||
144 | virtual bool LoadCatalog(wxTranslations *translations, | |
145 | const wxString& domain, const wxString& lang) = 0; | |
146 | }; | |
147 | ||
148 | // standard wxTranslationsLoader implementation, using filesystem | |
149 | class WXDLLIMPEXP_BASE wxFileTranslationsLoader | |
150 | : public wxTranslationsLoader | |
151 | { | |
152 | public: | |
153 | static void AddCatalogLookupPathPrefix(const wxString& prefix); | |
154 | ||
155 | virtual bool LoadCatalog(wxTranslations *translations, | |
156 | const wxString& domain, const wxString& lang); | |
157 | }; | |
158 | ||
159 | ||
160 | // ---------------------------------------------------------------------------- | |
161 | // global functions | |
162 | // ---------------------------------------------------------------------------- | |
163 | ||
164 | // get the translation of the string in the current locale | |
165 | inline const wxString& wxGetTranslation(const wxString& str, | |
166 | const wxString& domain = wxEmptyString) | |
167 | { | |
168 | wxTranslations *trans = wxTranslations::Get(); | |
169 | if ( trans ) | |
170 | return trans->GetString(str, domain); | |
171 | else | |
172 | // NB: this function returns reference to a string, so we have to keep | |
173 | // a copy of it somewhere | |
174 | return wxTranslations::GetUntranslatedString(str); | |
175 | } | |
176 | ||
177 | inline const wxString& wxGetTranslation(const wxString& str1, | |
178 | const wxString& str2, | |
179 | size_t n, | |
180 | const wxString& domain = wxEmptyString) | |
181 | { | |
182 | wxTranslations *trans = wxTranslations::Get(); | |
183 | if ( trans ) | |
184 | return trans->GetString(str1, str2, n, domain); | |
185 | else | |
186 | // NB: this function returns reference to a string, so we have to keep | |
187 | // a copy of it somewhere | |
188 | return n == 1 | |
189 | ? wxTranslations::GetUntranslatedString(str1) | |
190 | : wxTranslations::GetUntranslatedString(str2); | |
191 | } | |
192 | ||
193 | #else // !wxUSE_INTL | |
194 | ||
195 | // the macros should still be defined - otherwise compilation would fail | |
196 | ||
197 | #if !defined(WXINTL_NO_GETTEXT_MACRO) | |
198 | #if !defined(_) | |
199 | #define _(s) (s) | |
200 | #endif | |
201 | #define wxPLURAL(sing, plur, n) ((n) == 1 ? (sing) : (plur)) | |
202 | #endif | |
203 | ||
204 | #define wxTRANSLATE(str) str | |
205 | ||
206 | // NB: we use a template here in order to avoid using | |
207 | // wxLocale::GetUntranslatedString() above, which would be required if | |
208 | // we returned const wxString&; this way, the compiler should be able to | |
209 | // optimize wxGetTranslation() away | |
210 | ||
211 | template<typename TString> | |
212 | inline TString wxGetTranslation(TString str) | |
213 | { return str; } | |
214 | ||
215 | template<typename TString, typename TDomain> | |
216 | inline TString wxGetTranslation(TString str, TDomain WXUNUSED(domain)) | |
217 | { return str; } | |
218 | ||
219 | template<typename TString, typename TDomain> | |
220 | inline TString wxGetTranslation(TString str1, TString str2, size_t n) | |
221 | { return n == 1 ? str1 : str2; } | |
222 | ||
223 | template<typename TString, typename TDomain> | |
224 | inline TString wxGetTranslation(TString str1, TString str2, size_t n, | |
225 | TDomain WXUNUSED(domain)) | |
226 | { return n == 1 ? str1 : str2; } | |
227 | ||
228 | #endif // wxUSE_INTL/!wxUSE_INTL | |
229 | ||
230 | // define this one just in case it occurs somewhere (instead of preferred | |
231 | // wxTRANSLATE) too | |
232 | #if !defined(WXINTL_NO_GETTEXT_MACRO) | |
233 | #if !defined(gettext_noop) | |
234 | #define gettext_noop(str) (str) | |
235 | #endif | |
236 | #if !defined(N_) | |
237 | #define N_(s) (s) | |
238 | #endif | |
239 | #endif | |
240 | ||
241 | #endif // _WX_TRANSLATION_H_ |