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>
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // (c) 2010 Vaclav Slavik <vslavik@fastmail.fm>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_TRANSLATION_H_
14 #define _WX_TRANSLATION_H_
17 #include "wx/string.h"
21 #include "wx/language.h"
24 #include "wx/hashmap.h"
27 // ============================================================================
29 // ============================================================================
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
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)
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
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 class WXDLLIMPEXP_FWD_BASE wxTranslationsLoader
;
52 class WXDLLIMPEXP_FWD_BASE wxLocale
;
55 // ----------------------------------------------------------------------------
56 // wxTranslations: message catalogs
57 // ----------------------------------------------------------------------------
59 // this class allows to get translations for strings
60 class WXDLLIMPEXP_BASE wxTranslations
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
);
71 // changes loader to non-default one; takes ownership of 'loader'
72 void SetLoader(wxTranslationsLoader
*loader
);
74 void SetLanguage(wxLanguage lang
);
75 void SetLanguage(const wxString
& lang
);
77 // add standard wxWidgets catalog ("wxstd")
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
);
85 bool AddCatalog(const wxString
& domain
,
86 wxLanguage msgIdLanguage
,
87 const wxString
& msgIdCharset
);
90 // check if the given catalog is loaded
91 bool IsLoaded(const wxString
& domain
) const;
93 // load catalog data directly from file
94 bool LoadCatalogFile(const wxString
& filename
,
95 const wxString
& domain
= wxEmptyString
);
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
,
103 const wxString
& domain
= wxEmptyString
) const;
105 wxString
GetHeaderValue(const wxString
& header
,
106 const wxString
& domain
= wxEmptyString
) const;
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
);
114 // find best translation for given domain
115 wxString
ChooseLanguageForDomain(const wxString
& domain
,
116 const wxString
& msgIdLang
);
118 // find catalog by name in a linked list, return NULL if !found
119 wxMsgCatalog
*FindCatalog(const wxString
& domain
) const;
121 // same as Set(), without taking ownership; only for wxLocale
122 static void SetNonOwned(wxTranslations
*t
);
123 friend class wxLocale
;
127 wxTranslationsLoader
*m_loader
;
129 wxMsgCatalog
*m_pMsgCat
; // pointer to linked list of catalogs
132 wxStringToStringHashMap m_msgIdCharset
;
137 // abstraction of translations discovery and loading
138 class WXDLLIMPEXP_BASE wxTranslationsLoader
141 wxTranslationsLoader() {}
142 virtual ~wxTranslationsLoader() {}
144 virtual bool LoadCatalog(wxTranslations
*translations
,
145 const wxString
& domain
, const wxString
& lang
) = 0;
148 // standard wxTranslationsLoader implementation, using filesystem
149 class WXDLLIMPEXP_BASE wxFileTranslationsLoader
150 : public wxTranslationsLoader
153 static void AddCatalogLookupPathPrefix(const wxString
& prefix
);
155 virtual bool LoadCatalog(wxTranslations
*translations
,
156 const wxString
& domain
, const wxString
& lang
);
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 // get the translation of the string in the current locale
165 inline const wxString
& wxGetTranslation(const wxString
& str
,
166 const wxString
& domain
= wxEmptyString
)
168 wxTranslations
*trans
= wxTranslations::Get();
170 return trans
->GetString(str
, domain
);
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
);
177 inline const wxString
& wxGetTranslation(const wxString
& str1
,
178 const wxString
& str2
,
180 const wxString
& domain
= wxEmptyString
)
182 wxTranslations
*trans
= wxTranslations::Get();
184 return trans
->GetString(str1
, str2
, n
, domain
);
186 // NB: this function returns reference to a string, so we have to keep
187 // a copy of it somewhere
189 ? wxTranslations::GetUntranslatedString(str1
)
190 : wxTranslations::GetUntranslatedString(str2
);
195 // the macros should still be defined - otherwise compilation would fail
197 #if !defined(WXINTL_NO_GETTEXT_MACRO)
201 #define wxPLURAL(sing, plur, n) ((n) == 1 ? (sing) : (plur))
204 #define wxTRANSLATE(str) str
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
211 template<typename TString
>
212 inline TString
wxGetTranslation(TString str
)
215 template<typename TString
, typename TDomain
>
216 inline TString
wxGetTranslation(TString str
, TDomain
WXUNUSED(domain
))
219 template<typename TString
, typename TDomain
>
220 inline TString
wxGetTranslation(TString str1
, TString str2
, size_t n
)
221 { return n
== 1 ? str1
: str2
; }
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
; }
228 #endif // wxUSE_INTL/!wxUSE_INTL
230 // define this one just in case it occurs somewhere (instead of preferred
232 #if !defined(WXINTL_NO_GETTEXT_MACRO)
233 #if !defined(gettext_noop)
234 #define gettext_noop(str) (str)
241 #endif // _WX_TRANSLATION_H_