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