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>
7 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // (c) 2010 Vaclav Slavik <vslavik@fastmail.fm>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TRANSLATION_H_
13 #define _WX_TRANSLATION_H_
16 #include "wx/string.h"
20 #include "wx/buffer.h"
21 #include "wx/language.h"
22 #include "wx/hashmap.h"
23 #include "wx/strconv.h"
24 #include "wx/scopedptr.h"
26 // ============================================================================
28 // ============================================================================
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 // gettext() style macros (notice that xgettext should be invoked with
35 // --keyword="_" --keyword="wxPLURAL:1,2" options
36 // to extract the strings from the sources)
37 #ifndef WXINTL_NO_GETTEXT_MACRO
38 #define _(s) wxGetTranslation((s))
39 #define wxPLURAL(sing, plur, n) wxGetTranslation((sing), (plur), n)
42 // another one which just marks the strings for extraction, but doesn't
43 // perform the translation (use -kwxTRANSLATE with xgettext!)
44 #define wxTRANSLATE(str) str
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 class WXDLLIMPEXP_FWD_BASE wxArrayString
;
51 class WXDLLIMPEXP_FWD_BASE wxTranslationsLoader
;
52 class WXDLLIMPEXP_FWD_BASE wxLocale
;
54 class wxPluralFormsCalculator
;
55 wxDECLARE_SCOPED_PTR(wxPluralFormsCalculator
, wxPluralFormsCalculatorPtr
)
57 // ----------------------------------------------------------------------------
58 // wxMsgCatalog corresponds to one loaded message catalog.
59 // ----------------------------------------------------------------------------
61 class WXDLLIMPEXP_BASE wxMsgCatalog
64 // Ctor is protected, because CreateFromXXX functions must be used,
65 // but destruction should be unrestricted
70 // load the catalog from disk or from data; caller is responsible for
71 // deleting them if not NULL
72 static wxMsgCatalog
*CreateFromFile(const wxString
& filename
,
73 const wxString
& domain
);
75 static wxMsgCatalog
*CreateFromData(const wxScopedCharBuffer
& data
,
76 const wxString
& domain
);
78 // get name of the catalog
79 wxString
GetDomain() const { return m_domain
; }
81 // get the translated string: returns NULL if not found
82 const wxString
*GetString(const wxString
& sz
, unsigned n
= UINT_MAX
) const;
85 wxMsgCatalog(const wxString
& domain
)
86 : m_pNext(NULL
), m_domain(domain
)
93 // variable pointing to the next element in a linked list (or NULL)
94 wxMsgCatalog
*m_pNext
;
95 friend class wxTranslations
;
97 wxStringToStringHashMap m_messages
; // all messages in the catalog
98 wxString m_domain
; // name of the domain
101 // the conversion corresponding to this catalog charset if we installed it
106 wxPluralFormsCalculatorPtr m_pluralFormsCalculator
;
109 // ----------------------------------------------------------------------------
110 // wxTranslations: message catalogs
111 // ----------------------------------------------------------------------------
113 // this class allows to get translations for strings
114 class WXDLLIMPEXP_BASE wxTranslations
120 // returns current translations object, may return NULL
121 static wxTranslations
*Get();
122 // sets current translations object (takes ownership; may be NULL)
123 static void Set(wxTranslations
*t
);
125 // changes loader to non-default one; takes ownership of 'loader'
126 void SetLoader(wxTranslationsLoader
*loader
);
128 void SetLanguage(wxLanguage lang
);
129 void SetLanguage(const wxString
& lang
);
131 // get languages available for this app
132 wxArrayString
GetAvailableTranslations(const wxString
& domain
) const;
134 // find best translation language for given domain
135 wxString
GetBestTranslation(const wxString
& domain
, wxLanguage msgIdLanguage
);
136 wxString
GetBestTranslation(const wxString
& domain
,
137 const wxString
& msgIdLanguage
= "en");
139 // add standard wxWidgets catalog ("wxstd")
140 bool AddStdCatalog();
142 // add catalog with given domain name and language, looking it up via
143 // wxTranslationsLoader
144 bool AddCatalog(const wxString
& domain
);
145 bool AddCatalog(const wxString
& domain
, wxLanguage msgIdLanguage
);
147 bool AddCatalog(const wxString
& domain
,
148 wxLanguage msgIdLanguage
,
149 const wxString
& msgIdCharset
);
152 // check if the given catalog is loaded
153 bool IsLoaded(const wxString
& domain
) const;
155 // access to translations
156 const wxString
& GetString(const wxString
& origString
,
157 const wxString
& domain
= wxEmptyString
) const;
158 const wxString
& GetString(const wxString
& origString
,
159 const wxString
& origString2
,
161 const wxString
& domain
= wxEmptyString
) const;
163 wxString
GetHeaderValue(const wxString
& header
,
164 const wxString
& domain
= wxEmptyString
) const;
166 // this is hack to work around a problem with wxGetTranslation() which
167 // returns const wxString& and not wxString, so when it returns untranslated
168 // string, it needs to have a copy of it somewhere
169 static const wxString
& GetUntranslatedString(const wxString
& str
);
172 // perform loading of the catalog via m_loader
173 bool LoadCatalog(const wxString
& domain
, const wxString
& lang
);
175 // find catalog by name in a linked list, return NULL if !found
176 wxMsgCatalog
*FindCatalog(const wxString
& domain
) const;
178 // same as Set(), without taking ownership; only for wxLocale
179 static void SetNonOwned(wxTranslations
*t
);
180 friend class wxLocale
;
184 wxTranslationsLoader
*m_loader
;
186 wxMsgCatalog
*m_pMsgCat
; // pointer to linked list of catalogs
190 // abstraction of translations discovery and loading
191 class WXDLLIMPEXP_BASE wxTranslationsLoader
194 wxTranslationsLoader() {}
195 virtual ~wxTranslationsLoader() {}
197 virtual wxMsgCatalog
*LoadCatalog(const wxString
& domain
,
198 const wxString
& lang
) = 0;
200 virtual wxArrayString
GetAvailableTranslations(const wxString
& domain
) const = 0;
204 // standard wxTranslationsLoader implementation, using filesystem
205 class WXDLLIMPEXP_BASE wxFileTranslationsLoader
206 : public wxTranslationsLoader
209 static void AddCatalogLookupPathPrefix(const wxString
& prefix
);
211 virtual wxMsgCatalog
*LoadCatalog(const wxString
& domain
,
212 const wxString
& lang
);
214 virtual wxArrayString
GetAvailableTranslations(const wxString
& domain
) const;
219 // loads translations from win32 resources
220 class WXDLLIMPEXP_BASE wxResourceTranslationsLoader
221 : public wxTranslationsLoader
224 virtual wxMsgCatalog
*LoadCatalog(const wxString
& domain
,
225 const wxString
& lang
);
227 virtual wxArrayString
GetAvailableTranslations(const wxString
& domain
) const;
230 // returns resource type to use for translations
231 virtual wxString
GetResourceType() const { return "MOFILE"; }
233 // returns module to load resources from
234 virtual WXHINSTANCE
GetModule() const { return 0; }
236 #endif // __WINDOWS__
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 // get the translation of the string in the current locale
244 inline const wxString
& wxGetTranslation(const wxString
& str
,
245 const wxString
& domain
= wxEmptyString
)
247 wxTranslations
*trans
= wxTranslations::Get();
249 return trans
->GetString(str
, domain
);
251 // NB: this function returns reference to a string, so we have to keep
252 // a copy of it somewhere
253 return wxTranslations::GetUntranslatedString(str
);
256 inline const wxString
& wxGetTranslation(const wxString
& str1
,
257 const wxString
& str2
,
259 const wxString
& domain
= wxEmptyString
)
261 wxTranslations
*trans
= wxTranslations::Get();
263 return trans
->GetString(str1
, str2
, n
, domain
);
265 // NB: this function returns reference to a string, so we have to keep
266 // a copy of it somewhere
268 ? wxTranslations::GetUntranslatedString(str1
)
269 : wxTranslations::GetUntranslatedString(str2
);
274 // the macros should still be defined - otherwise compilation would fail
276 #if !defined(WXINTL_NO_GETTEXT_MACRO)
280 #define wxPLURAL(sing, plur, n) ((n) == 1 ? (sing) : (plur))
283 #define wxTRANSLATE(str) str
285 // NB: we use a template here in order to avoid using
286 // wxLocale::GetUntranslatedString() above, which would be required if
287 // we returned const wxString&; this way, the compiler should be able to
288 // optimize wxGetTranslation() away
290 template<typename TString
>
291 inline TString
wxGetTranslation(TString str
)
294 template<typename TString
, typename TDomain
>
295 inline TString
wxGetTranslation(TString str
, TDomain
WXUNUSED(domain
))
298 template<typename TString
, typename TDomain
>
299 inline TString
wxGetTranslation(TString str1
, TString str2
, size_t n
)
300 { return n
== 1 ? str1
: str2
; }
302 template<typename TString
, typename TDomain
>
303 inline TString
wxGetTranslation(TString str1
, TString str2
, size_t n
,
304 TDomain
WXUNUSED(domain
))
305 { return n
== 1 ? str1
: str2
; }
307 #endif // wxUSE_INTL/!wxUSE_INTL
309 // define this one just in case it occurs somewhere (instead of preferred
311 #if !defined(WXINTL_NO_GETTEXT_MACRO)
312 #if !defined(gettext_noop)
313 #define gettext_noop(str) (str)
320 #endif // _WX_TRANSLATION_H_