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 or empty string if this locale is not
76     // supported
77     wxString GetLocaleName() const;
78 
79     // Call setlocale() and return non-null value if it works for this language.
80     //
81     // This function is mostly for internal use, as changing locale involves
82     // more than just calling setlocale() on some platforms, use wxLocale to
83     // do everything that needs to be done instead of calling this method.
84     const char* TrySetLocale() const;
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
wxLocale()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     // Same as GetInfo() but uses current locale at the OS level to retrieve
219     // the information. Normally it should be the same as the one used by
220     // GetInfo() but there are two exceptions: the most important one is that
221     // if no locale had been set, GetInfo() would fall back to "C" locale,
222     // while this one uses the default OS locale. Another, more rare, one is
223     // that some locales might not supported by the OS.
224     //
225     // Currently this is the same as GetInfo() under non-MSW platforms.
226     static wxString GetOSInfo(wxLocaleInfo index,
227                               wxLocaleCategory cat = wxLOCALE_CAT_DEFAULT);
228 
229     // return true if the locale was set successfully
IsOk()230     bool IsOk() const { return m_pszOldLocale != NULL; }
231 
232     // returns locale name
GetLocale()233     const wxString& GetLocale() const { return m_strLocale; }
234 
235     // return current locale wxLanguage value
GetLanguage()236     int GetLanguage() const { return m_language; }
237 
238     // return locale name to be passed to setlocale()
239     wxString GetSysName() const;
240 
241     // return 'canonical' name, i.e. in the form of xx[_YY], where xx is
242     // language code according to ISO 639 and YY is country name
243     // as specified by ISO 3166.
GetCanonicalName()244     wxString GetCanonicalName() const { return m_strShort; }
245 
246     // add a prefix to the catalog lookup path: the message catalog files will be
247     // looked up under prefix/<lang>/LC_MESSAGES, prefix/LC_MESSAGES and prefix
248     // (in this order).
249     //
250     // This only applies to subsequent invocations of AddCatalog()!
AddCatalogLookupPathPrefix(const wxString & prefix)251     static void AddCatalogLookupPathPrefix(const wxString& prefix)
252         { wxFileTranslationsLoader::AddCatalogLookupPathPrefix(prefix); }
253 
254     // add a catalog: it's searched for in standard places (current directory
255     // first, system one after), but the you may prepend additional directories to
256     // the search path with AddCatalogLookupPathPrefix().
257     //
258     // The loaded catalog will be used for message lookup by GetString().
259     //
260     // Returns 'true' if it was successfully loaded
261     bool AddCatalog(const wxString& domain);
262     bool AddCatalog(const wxString& domain, wxLanguage msgIdLanguage);
263     bool AddCatalog(const wxString& domain,
264                     wxLanguage msgIdLanguage, const wxString& msgIdCharset);
265 
266     // check if the given locale is provided by OS and C run time
267     static bool IsAvailable(int lang);
268 
269     // check if the given catalog is loaded
270     bool IsLoaded(const wxString& domain) const;
271 
272     // Retrieve the language info struct for the given language
273     //
274     // Returns NULL if no info found, pointer must *not* be deleted by caller
275     static const wxLanguageInfo *GetLanguageInfo(int lang);
276 
277     // Returns language name in English or empty string if the language
278     // is not in database
279     static wxString GetLanguageName(int lang);
280 
281     // Returns ISO code ("canonical name") of language or empty string if the
282     // language is not in database
283     static wxString GetLanguageCanonicalName(int lang);
284 
285     // Find the language for the given locale string which may be either a
286     // canonical ISO 2 letter language code ("xx"), a language code followed by
287     // the country code ("xx_XX") or a Windows full language name ("Xxxxx...")
288     //
289     // Returns NULL if no info found, pointer must *not* be deleted by caller
290     static const wxLanguageInfo *FindLanguageInfo(const wxString& locale);
291 
292     // Add custom language to the list of known languages.
293     // Notes: 1) wxLanguageInfo contains platform-specific data
294     //        2) must be called before Init to have effect
295     static void AddLanguage(const wxLanguageInfo& info);
296 
297     // retrieve the translation for a string in all loaded domains unless
298     // the szDomain parameter is specified (and then only this domain is
299     // searched)
300     // n - additional parameter for PluralFormsParser
301     //
302     // return original string if translation is not available
303     // (in this case an error message is generated the first time
304     //  a string is not found; use wxLogNull to suppress it)
305     //
306     // domains are searched in the last to first order, i.e. catalogs
307     // added later override those added before.
308     const wxString& GetString(const wxString& origString,
309                               const wxString& domain = wxEmptyString) const
310     {
311         return wxGetTranslation(origString, domain);
312     }
313     // plural form version of the same:
314     const wxString& GetString(const wxString& origString,
315                               const wxString& origString2,
316                               unsigned n,
317                               const wxString& domain = wxEmptyString) const
318     {
319         return wxGetTranslation(origString, origString2, n, domain);
320     }
321 
322     // Returns the current short name for the locale
GetName()323     const wxString& GetName() const { return m_strShort; }
324 
325     // return the contents of .po file header
326     wxString GetHeaderValue(const wxString& header,
327                             const wxString& domain = wxEmptyString) const;
328 
329     // These two methods are for internal use only. First one creates
330     // ms_languagesDB if it doesn't already exist, second one destroys
331     // it.
332     static void CreateLanguagesDB();
333     static void DestroyLanguagesDB();
334 
335 private:
336     // This method is trivial and just updates the corresponding member
337     // variables without doing anything else.
338     void DoInit(const wxString& name,
339                 const wxString& shortName,
340                 int language);
341 
342     // copy default table of languages from global static array to
343     // m_langugagesInfo, called by InitLanguagesDB
344     static void InitLanguagesDB();
345 
346     // initialize the member fields to default values
347     void DoCommonInit();
348 
349     // After trying to set locale, call this method to give the appropriate
350     // error if it couldn't be set (success == false) and to load the
351     // translations for the given language, if necessary.
352     //
353     // The return value is the same as "success" parameter.
354     bool DoCommonPostInit(bool success,
355                           const wxString& name,
356                           const wxString& shortName,
357                           bool bLoadDefault);
358 
359 
360     wxString       m_strLocale,       // this locale name
361                    m_strShort;        // short name for the locale
362     int            m_language;        // this locale wxLanguage value
363 
364     const char  *m_pszOldLocale;      // previous locale from setlocale()
365     wxLocale      *m_pOldLocale;      // previous wxLocale
366 
367     bool           m_initialized;
368 
369     wxTranslations m_translations;
370 
371     static wxLanguageInfoArray *ms_languagesDB;
372 
373     wxDECLARE_NO_COPY_CLASS(wxLocale);
374 };
375 
376 // ----------------------------------------------------------------------------
377 // global functions
378 // ----------------------------------------------------------------------------
379 
380 // get the current locale object (note that it may be NULL!)
381 extern WXDLLIMPEXP_BASE wxLocale* wxGetLocale();
382 
383 #endif // wxUSE_INTL
384 
385 #endif // _WX_INTL_H_
386