1 /////////////////////////////////////////////////////////////////////////////
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)
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
17 #include "wx/string.h"
18 #include "wx/translation.h"
20 // Make wxLayoutDirection enum available without need for wxUSE_INTL so wxWindow, wxApp
21 // and other classes are not distrubed by wxUSE_INTL
23 enum wxLayoutDirection
32 #include "wx/fontenc.h"
33 #include "wx/language.h"
35 // ============================================================================
37 // ============================================================================
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 class WXDLLIMPEXP_FWD_BASE wxLocale
;
48 class WXDLLIMPEXP_FWD_BASE wxLanguageInfoArray
;
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
55 // wxLanguageInfo: encapsulates wxLanguage to OS native lang.desc.
56 // translation information
57 // ----------------------------------------------------------------------------
59 struct WXDLLIMPEXP_BASE wxLanguageInfo
61 int Language
; // wxLanguage id
62 wxString CanonicalName
; // Canonical name, e.g. fr_FR
64 wxUint32 WinLang
, // Win32 language identifiers
67 wxString Description
; // human-readable name of the language
68 wxLayoutDirection LayoutDirection
;
71 // return the LCID corresponding to this language
72 wxUint32
GetLCID() const;
75 // return the locale name corresponding to this language usable with
76 // setlocale() on the current system
77 wxString
GetLocaleName() const;
80 // for Unix systems GetLocaleName() is trivial so implement it inline here, for
81 // MSW it's implemented in intl.cpp
83 inline wxString
wxLanguageInfo::GetLocaleName() const { return CanonicalName
; }
84 #endif // !__WINDOWS__
87 // ----------------------------------------------------------------------------
88 // wxLocaleCategory: the category of locale settings
89 // ----------------------------------------------------------------------------
102 // default category for wxLocaleInfo values which only apply to a single
103 // category (e.g. wxLOCALE_SHORT_DATE_FMT)
104 wxLOCALE_CAT_DEFAULT
,
109 // ----------------------------------------------------------------------------
110 // wxLocaleInfo: the items understood by wxLocale::GetInfo()
111 // ----------------------------------------------------------------------------
115 // the thousands separator (for wxLOCALE_CAT_NUMBER or MONEY)
116 wxLOCALE_THOUSANDS_SEP
,
118 // the character used as decimal point (for wxLOCALE_CAT_NUMBER or MONEY)
119 wxLOCALE_DECIMAL_POINT
,
121 // the stftime()-formats used for short/long date and time representations
122 // (under some platforms short and long date formats are the same)
124 // NB: these elements should appear in this order, code in GetInfo() relies
126 wxLOCALE_SHORT_DATE_FMT
,
127 wxLOCALE_LONG_DATE_FMT
,
128 wxLOCALE_DATE_TIME_FMT
,
133 // ----------------------------------------------------------------------------
134 // wxLocale: encapsulates all language dependent settings, including current
135 // message catalogs, date, time and currency formats (TODO) &c
136 // ----------------------------------------------------------------------------
138 enum wxLocaleInitFlags
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
148 class WXDLLIMPEXP_BASE wxLocale
154 // call Init() if you use this ctor
155 wxLocale() { DoCommonInit(); }
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?
169 #if WXWIN_COMPATIBILITY_2_8
170 Init(name
, shortName
, locale
, bLoadDefault
, bConvertEncoding
);
172 Init(name
, shortName
, locale
, bLoadDefault
);
176 wxLocale(int language
, // wxLanguage id or custom language
177 int flags
= wxLOCALE_LOAD_DEFAULT
)
181 Init(language
, flags
);
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
194 // same as second ctor (returns true on success)
195 bool Init(int language
= wxLANGUAGE_DEFAULT
,
196 int flags
= wxLOCALE_LOAD_DEFAULT
);
198 // restores old locale
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();
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();
209 // get the string describing the system encoding, return empty string if
210 // couldn't be determined
211 static wxString
GetSystemEncodingName();
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
);
218 // return true if the locale was set successfully
219 bool IsOk() const { return m_pszOldLocale
!= NULL
; }
221 // returns locale name
222 const wxString
& GetLocale() const { return m_strLocale
; }
224 // return current locale wxLanguage value
225 int GetLanguage() const { return m_language
; }
227 // return locale name to be passed to setlocale()
228 wxString
GetSysName() const;
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
; }
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
239 // This only applies to subsequent invocations of AddCatalog()!
240 static void AddCatalogLookupPathPrefix(const wxString
& prefix
)
241 { wxFileTranslationsLoader::AddCatalogLookupPathPrefix(prefix
); }
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().
247 // The loaded catalog will be used for message lookup by GetString().
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
);
255 // check if the given locale is provided by OS and C run time
256 static bool IsAvailable(int lang
);
258 // check if the given catalog is loaded
259 bool IsLoaded(const wxString
& domain
) const;
261 // Retrieve the language info struct for the given language
263 // Returns NULL if no info found, pointer must *not* be deleted by caller
264 static const wxLanguageInfo
*GetLanguageInfo(int lang
);
266 // Returns language name in English or empty string if the language
267 // is not in database
268 static wxString
GetLanguageName(int lang
);
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
);
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...")
278 // Returns NULL if no info found, pointer must *not* be deleted by caller
279 static const wxLanguageInfo
*FindLanguageInfo(const wxString
& locale
);
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
);
286 // retrieve the translation for a string in all loaded domains unless
287 // the szDomain parameter is specified (and then only this domain is
289 // n - additional parameter for PluralFormsParser
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)
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
300 return wxGetTranslation(origString
, domain
);
302 // plural form version of the same:
303 const wxString
& GetString(const wxString
& origString
,
304 const wxString
& origString2
,
306 const wxString
& domain
= wxEmptyString
) const
308 return wxGetTranslation(origString
, origString2
, n
, domain
);
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
); }
317 // Returns the current short name for the locale
318 const wxString
& GetName() const { return m_strShort
; }
320 // return the contents of .po file header
321 wxString
GetHeaderValue(const wxString
& header
,
322 const wxString
& domain
= wxEmptyString
) const;
324 // These two methods are for internal use only. First one creates
325 // ms_languagesDB if it doesn't already exist, second one destroys
327 static void CreateLanguagesDB();
328 static void DestroyLanguagesDB();
331 bool DoInit(const wxString
& name
,
332 const wxString
& shortName
,
333 const wxString
& locale
);
335 // copy default table of languages from global static array to
336 // m_langugagesInfo, called by InitLanguagesDB
337 static void InitLanguagesDB();
339 // initialize the member fields to default values
342 wxString m_strLocale
, // this locale name
343 m_strShort
; // short name for the locale
344 int m_language
; // this locale wxLanguage value
346 const char *m_pszOldLocale
; // previous locale from setlocale()
347 wxLocale
*m_pOldLocale
; // previous wxLocale
351 wxTranslations m_translations
;
353 static wxLanguageInfoArray
*ms_languagesDB
;
355 wxDECLARE_NO_COPY_CLASS(wxLocale
);
358 // ----------------------------------------------------------------------------
360 // ----------------------------------------------------------------------------
362 // get the current locale object (note that it may be NULL!)
363 extern WXDLLIMPEXP_BASE wxLocale
* wxGetLocale();
367 #endif // _WX_INTL_H_