]> git.saurik.com Git - wxWidgets.git/blob - include/wx/intl.h
Don't return a dangling pointer from wxDateTime::ParseXXX(wxCStrData).
[wxWidgets.git] / include / wx / intl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/intl.h
3 // Purpose: Internationalization and localisation for wxWidgets
4 // Author: Vadim Zeitlin
5 // Modified by: Michael N. Filippov <michael@idisys.iae.nsk.su>
6 // (2003/09/30 - plural forms support)
7 // Created: 29/01/98
8 // RCS-ID: $Id$
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_INTL_H_
14 #define _WX_INTL_H_
15
16 #include "wx/defs.h"
17 #include "wx/string.h"
18 #include "wx/translation.h"
19
20 // Make wxLayoutDirection enum available without need for wxUSE_INTL so wxWindow, wxApp
21 // and other classes are not distrubed by wxUSE_INTL
22
23 enum wxLayoutDirection
24 {
25 wxLayout_Default,
26 wxLayout_LeftToRight,
27 wxLayout_RightToLeft
28 };
29
30 #if wxUSE_INTL
31
32 #include "wx/fontenc.h"
33 #include "wx/language.h"
34
35 // ============================================================================
36 // global decls
37 // ============================================================================
38
39 // ----------------------------------------------------------------------------
40 // macros
41 // ----------------------------------------------------------------------------
42
43 // ----------------------------------------------------------------------------
44 // forward decls
45 // ----------------------------------------------------------------------------
46
47 class WXDLLIMPEXP_FWD_BASE wxLocale;
48 class WXDLLIMPEXP_FWD_BASE wxLanguageInfoArray;
49
50 // ============================================================================
51 // locale support
52 // ============================================================================
53
54 // ----------------------------------------------------------------------------
55 // wxLanguageInfo: encapsulates wxLanguage to OS native lang.desc.
56 // translation information
57 // ----------------------------------------------------------------------------
58
59 struct WXDLLIMPEXP_BASE wxLanguageInfo
60 {
61 int Language; // wxLanguage id
62 wxString CanonicalName; // Canonical name, e.g. fr_FR
63 #ifdef __WINDOWS__
64 wxUint32 WinLang, // Win32 language identifiers
65 WinSublang;
66 #endif // __WINDOWS__
67 wxString Description; // human-readable name of the language
68 wxLayoutDirection LayoutDirection;
69
70 #ifdef __WINDOWS__
71 // return the LCID corresponding to this language
72 wxUint32 GetLCID() const;
73 #endif // __WINDOWS__
74
75 // return the locale name corresponding to this language usable with
76 // setlocale() on the current system
77 wxString GetLocaleName() const;
78 };
79
80 // for Unix systems GetLocaleName() is trivial so implement it inline here, for
81 // MSW it's implemented in intl.cpp
82 #ifndef __WINDOWS__
83 inline wxString wxLanguageInfo::GetLocaleName() const { return CanonicalName; }
84 #endif // !__WINDOWS__
85
86
87 // ----------------------------------------------------------------------------
88 // wxLocaleCategory: the category of locale settings
89 // ----------------------------------------------------------------------------
90
91 enum wxLocaleCategory
92 {
93 // (any) numbers
94 wxLOCALE_CAT_NUMBER,
95
96 // date/time
97 wxLOCALE_CAT_DATE,
98
99 // monetary value
100 wxLOCALE_CAT_MONEY,
101
102 // default category for wxLocaleInfo values which only apply to a single
103 // category (e.g. wxLOCALE_SHORT_DATE_FMT)
104 wxLOCALE_CAT_DEFAULT,
105
106 wxLOCALE_CAT_MAX
107 };
108
109 // ----------------------------------------------------------------------------
110 // wxLocaleInfo: the items understood by wxLocale::GetInfo()
111 // ----------------------------------------------------------------------------
112
113 enum wxLocaleInfo
114 {
115 // the thousands separator (for wxLOCALE_CAT_NUMBER or MONEY)
116 wxLOCALE_THOUSANDS_SEP,
117
118 // the character used as decimal point (for wxLOCALE_CAT_NUMBER or MONEY)
119 wxLOCALE_DECIMAL_POINT,
120
121 // the stftime()-formats used for short/long date and time representations
122 // (under some platforms short and long date formats are the same)
123 //
124 // NB: these elements should appear in this order, code in GetInfo() relies
125 // on it
126 wxLOCALE_SHORT_DATE_FMT,
127 wxLOCALE_LONG_DATE_FMT,
128 wxLOCALE_DATE_TIME_FMT,
129 wxLOCALE_TIME_FMT
130
131 };
132
133 // ----------------------------------------------------------------------------
134 // wxLocale: encapsulates all language dependent settings, including current
135 // message catalogs, date, time and currency formats (TODO) &c
136 // ----------------------------------------------------------------------------
137
138 enum wxLocaleInitFlags
139 {
140 wxLOCALE_DONT_LOAD_DEFAULT = 0x0000, // don't load wxwin.mo
141 wxLOCALE_LOAD_DEFAULT = 0x0001 // load wxwin.mo?
142 #if WXWIN_COMPATIBILITY_2_8
143 ,wxLOCALE_CONV_ENCODING = 0x0002 // no longer used, simply remove
144 // it from the existing code
145 #endif
146 };
147
148 class WXDLLIMPEXP_BASE wxLocale
149 {
150 public:
151 // ctor & dtor
152 // -----------
153
154 // call Init() if you use this ctor
155 wxLocale() { DoCommonInit(); }
156
157 // the ctor has a side effect of changing current locale
158 wxLocale(const wxString& name, // name (for messages)
159 const wxString& shortName = wxEmptyString, // dir prefix (for msg files)
160 const wxString& locale = wxEmptyString, // locale (for setlocale)
161 bool bLoadDefault = true // preload wxstd.mo?
162 #if WXWIN_COMPATIBILITY_2_8
163 ,bool bConvertEncoding = true // convert Win<->Unix if necessary?
164 #endif
165 )
166 {
167 DoCommonInit();
168
169 #if WXWIN_COMPATIBILITY_2_8
170 Init(name, shortName, locale, bLoadDefault, bConvertEncoding);
171 #else
172 Init(name, shortName, locale, bLoadDefault);
173 #endif
174 }
175
176 wxLocale(int language, // wxLanguage id or custom language
177 int flags = wxLOCALE_LOAD_DEFAULT)
178 {
179 DoCommonInit();
180
181 Init(language, flags);
182 }
183
184 // the same as a function (returns true on success)
185 bool Init(const wxString& name,
186 const wxString& shortName = wxEmptyString,
187 const wxString& locale = wxEmptyString,
188 bool bLoadDefault = true
189 #if WXWIN_COMPATIBILITY_2_8
190 ,bool bConvertEncoding = true
191 #endif
192 );
193
194 // same as second ctor (returns true on success)
195 bool Init(int language = wxLANGUAGE_DEFAULT,
196 int flags = wxLOCALE_LOAD_DEFAULT);
197
198 // restores old locale
199 virtual ~wxLocale();
200
201 // Try to get user's (or OS's) preferred language setting.
202 // Return wxLANGUAGE_UNKNOWN if language-guessing algorithm failed
203 static int GetSystemLanguage();
204
205 // get the encoding used by default for text on this system, returns
206 // wxFONTENCODING_SYSTEM if it couldn't be determined
207 static wxFontEncoding GetSystemEncoding();
208
209 // get the string describing the system encoding, return empty string if
210 // couldn't be determined
211 static wxString GetSystemEncodingName();
212
213 // get the values of the given locale-dependent datum: the current locale
214 // is used, the US default value is returned if everything else fails
215 static wxString GetInfo(wxLocaleInfo index,
216 wxLocaleCategory cat = wxLOCALE_CAT_DEFAULT);
217
218 // return true if the locale was set successfully
219 bool IsOk() const { return m_pszOldLocale != NULL; }
220
221 // returns locale name
222 const wxString& GetLocale() const { return m_strLocale; }
223
224 // return current locale wxLanguage value
225 int GetLanguage() const { return m_language; }
226
227 // return locale name to be passed to setlocale()
228 wxString GetSysName() const;
229
230 // return 'canonical' name, i.e. in the form of xx[_YY], where xx is
231 // language code according to ISO 639 and YY is country name
232 // as specified by ISO 3166.
233 wxString GetCanonicalName() const { return m_strShort; }
234
235 // add a prefix to the catalog lookup path: the message catalog files will be
236 // looked up under prefix/<lang>/LC_MESSAGES, prefix/LC_MESSAGES and prefix
237 // (in this order).
238 //
239 // This only applies to subsequent invocations of AddCatalog()!
240 static void AddCatalogLookupPathPrefix(const wxString& prefix)
241 { wxFileTranslationsLoader::AddCatalogLookupPathPrefix(prefix); }
242
243 // add a catalog: it's searched for in standard places (current directory
244 // first, system one after), but the you may prepend additional directories to
245 // the search path with AddCatalogLookupPathPrefix().
246 //
247 // The loaded catalog will be used for message lookup by GetString().
248 //
249 // Returns 'true' if it was successfully loaded
250 bool AddCatalog(const wxString& domain);
251 bool AddCatalog(const wxString& domain, wxLanguage msgIdLanguage);
252 bool AddCatalog(const wxString& domain,
253 wxLanguage msgIdLanguage, const wxString& msgIdCharset);
254
255 // check if the given locale is provided by OS and C run time
256 static bool IsAvailable(int lang);
257
258 // check if the given catalog is loaded
259 bool IsLoaded(const wxString& domain) const;
260
261 // Retrieve the language info struct for the given language
262 //
263 // Returns NULL if no info found, pointer must *not* be deleted by caller
264 static const wxLanguageInfo *GetLanguageInfo(int lang);
265
266 // Returns language name in English or empty string if the language
267 // is not in database
268 static wxString GetLanguageName(int lang);
269
270 // Returns ISO code ("canonical name") of language or empty string if the
271 // language is not in database
272 static wxString GetLanguageCanonicalName(int lang);
273
274 // Find the language for the given locale string which may be either a
275 // canonical ISO 2 letter language code ("xx"), a language code followed by
276 // the country code ("xx_XX") or a Windows full language name ("Xxxxx...")
277 //
278 // Returns NULL if no info found, pointer must *not* be deleted by caller
279 static const wxLanguageInfo *FindLanguageInfo(const wxString& locale);
280
281 // Add custom language to the list of known languages.
282 // Notes: 1) wxLanguageInfo contains platform-specific data
283 // 2) must be called before Init to have effect
284 static void AddLanguage(const wxLanguageInfo& info);
285
286 // retrieve the translation for a string in all loaded domains unless
287 // the szDomain parameter is specified (and then only this domain is
288 // searched)
289 // n - additional parameter for PluralFormsParser
290 //
291 // return original string if translation is not available
292 // (in this case an error message is generated the first time
293 // a string is not found; use wxLogNull to suppress it)
294 //
295 // domains are searched in the last to first order, i.e. catalogs
296 // added later override those added before.
297 const wxString& GetString(const wxString& origString,
298 const wxString& domain = wxEmptyString) const
299 {
300 return wxGetTranslation(origString, domain);
301 }
302 // plural form version of the same:
303 const wxString& GetString(const wxString& origString,
304 const wxString& origString2,
305 unsigned n,
306 const wxString& domain = wxEmptyString) const
307 {
308 return wxGetTranslation(origString, origString2, n, domain);
309 }
310
311 // this is hack to work around a problem with wxGetTranslation() which
312 // returns const wxString& and not wxString, so when it returns untranslated
313 // string, it needs to have a copy of it somewhere
314 static const wxString& GetUntranslatedString(const wxString& str)
315 { return wxTranslations::GetUntranslatedString(str); }
316
317 // Returns the current short name for the locale
318 const wxString& GetName() const { return m_strShort; }
319
320 // return the contents of .po file header
321 wxString GetHeaderValue(const wxString& header,
322 const wxString& domain = wxEmptyString) const;
323
324 // These two methods are for internal use only. First one creates
325 // ms_languagesDB if it doesn't already exist, second one destroys
326 // it.
327 static void CreateLanguagesDB();
328 static void DestroyLanguagesDB();
329
330 private:
331 bool DoInit(const wxString& name,
332 const wxString& shortName,
333 const wxString& locale);
334
335 // copy default table of languages from global static array to
336 // m_langugagesInfo, called by InitLanguagesDB
337 static void InitLanguagesDB();
338
339 // initialize the member fields to default values
340 void DoCommonInit();
341
342 wxString m_strLocale, // this locale name
343 m_strShort; // short name for the locale
344 int m_language; // this locale wxLanguage value
345
346 const char *m_pszOldLocale; // previous locale from setlocale()
347 wxLocale *m_pOldLocale; // previous wxLocale
348
349 bool m_initialized;
350
351 wxTranslations m_translations;
352
353 static wxLanguageInfoArray *ms_languagesDB;
354
355 wxDECLARE_NO_COPY_CLASS(wxLocale);
356 };
357
358 // ----------------------------------------------------------------------------
359 // global functions
360 // ----------------------------------------------------------------------------
361
362 // get the current locale object (note that it may be NULL!)
363 extern WXDLLIMPEXP_BASE wxLocale* wxGetLocale();
364
365 #endif // wxUSE_INTL
366
367 #endif // _WX_INTL_H_