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)
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #include "wx/string.h"
17 #include "wx/translation.h"
19 // Make wxLayoutDirection enum available without need for wxUSE_INTL so wxWindow, wxApp
20 // and other classes are not distrubed by wxUSE_INTL
22 enum wxLayoutDirection
31 #include "wx/fontenc.h"
32 #include "wx/language.h"
34 // ============================================================================
36 // ============================================================================
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 class WXDLLIMPEXP_FWD_BASE wxLocale
;
47 class WXDLLIMPEXP_FWD_BASE wxLanguageInfoArray
;
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
54 // wxLanguageInfo: encapsulates wxLanguage to OS native lang.desc.
55 // translation information
56 // ----------------------------------------------------------------------------
58 struct WXDLLIMPEXP_BASE wxLanguageInfo
60 int Language
; // wxLanguage id
61 wxString CanonicalName
; // Canonical name, e.g. fr_FR
63 wxUint32 WinLang
, // Win32 language identifiers
66 wxString Description
; // human-readable name of the language
67 wxLayoutDirection LayoutDirection
;
70 // return the LCID corresponding to this language
71 wxUint32
GetLCID() const;
74 // return the locale name corresponding to this language usable with
75 // setlocale() on the current system
76 wxString
GetLocaleName() const;
79 // for Unix systems GetLocaleName() is trivial so implement it inline here, for
80 // MSW it's implemented in intl.cpp
82 inline wxString
wxLanguageInfo::GetLocaleName() const { return CanonicalName
; }
83 #endif // !__WINDOWS__
86 // ----------------------------------------------------------------------------
87 // wxLocaleCategory: the category of locale settings
88 // ----------------------------------------------------------------------------
101 // default category for wxLocaleInfo values which only apply to a single
102 // category (e.g. wxLOCALE_SHORT_DATE_FMT)
103 wxLOCALE_CAT_DEFAULT
,
108 // ----------------------------------------------------------------------------
109 // wxLocaleInfo: the items understood by wxLocale::GetInfo()
110 // ----------------------------------------------------------------------------
114 // the thousands separator (for wxLOCALE_CAT_NUMBER or MONEY)
115 wxLOCALE_THOUSANDS_SEP
,
117 // the character used as decimal point (for wxLOCALE_CAT_NUMBER or MONEY)
118 wxLOCALE_DECIMAL_POINT
,
120 // the stftime()-formats used for short/long date and time representations
121 // (under some platforms short and long date formats are the same)
123 // NB: these elements should appear in this order, code in GetInfo() relies
125 wxLOCALE_SHORT_DATE_FMT
,
126 wxLOCALE_LONG_DATE_FMT
,
127 wxLOCALE_DATE_TIME_FMT
,
132 // ----------------------------------------------------------------------------
133 // wxLocale: encapsulates all language dependent settings, including current
134 // message catalogs, date, time and currency formats (TODO) &c
135 // ----------------------------------------------------------------------------
137 enum wxLocaleInitFlags
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
147 class WXDLLIMPEXP_BASE wxLocale
153 // call Init() if you use this ctor
154 wxLocale() { DoCommonInit(); }
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?
168 #if WXWIN_COMPATIBILITY_2_8
169 Init(name
, shortName
, locale
, bLoadDefault
, bConvertEncoding
);
171 Init(name
, shortName
, locale
, bLoadDefault
);
175 wxLocale(int language
, // wxLanguage id or custom language
176 int flags
= wxLOCALE_LOAD_DEFAULT
)
180 Init(language
, flags
);
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
193 // same as second ctor (returns true on success)
194 bool Init(int language
= wxLANGUAGE_DEFAULT
,
195 int flags
= wxLOCALE_LOAD_DEFAULT
);
197 // restores old locale
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();
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();
208 // get the string describing the system encoding, return empty string if
209 // couldn't be determined
210 static wxString
GetSystemEncodingName();
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
);
217 // return true if the locale was set successfully
218 bool IsOk() const { return m_pszOldLocale
!= NULL
; }
220 // returns locale name
221 const wxString
& GetLocale() const { return m_strLocale
; }
223 // return current locale wxLanguage value
224 int GetLanguage() const { return m_language
; }
226 // return locale name to be passed to setlocale()
227 wxString
GetSysName() const;
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.
232 wxString
GetCanonicalName() const { return m_strShort
; }
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
238 // This only applies to subsequent invocations of AddCatalog()!
239 static void AddCatalogLookupPathPrefix(const wxString
& prefix
)
240 { wxFileTranslationsLoader::AddCatalogLookupPathPrefix(prefix
); }
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().
246 // The loaded catalog will be used for message lookup by GetString().
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
);
254 // check if the given locale is provided by OS and C run time
255 static bool IsAvailable(int lang
);
257 // check if the given catalog is loaded
258 bool IsLoaded(const wxString
& domain
) const;
260 // Retrieve the language info struct for the given language
262 // Returns NULL if no info found, pointer must *not* be deleted by caller
263 static const wxLanguageInfo
*GetLanguageInfo(int lang
);
265 // Returns language name in English or empty string if the language
266 // is not in database
267 static wxString
GetLanguageName(int lang
);
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
);
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...")
277 // Returns NULL if no info found, pointer must *not* be deleted by caller
278 static const wxLanguageInfo
*FindLanguageInfo(const wxString
& locale
);
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
);
285 // retrieve the translation for a string in all loaded domains unless
286 // the szDomain parameter is specified (and then only this domain is
288 // n - additional parameter for PluralFormsParser
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)
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
299 return wxGetTranslation(origString
, domain
);
301 // plural form version of the same:
302 const wxString
& GetString(const wxString
& origString
,
303 const wxString
& origString2
,
305 const wxString
& domain
= wxEmptyString
) const
307 return wxGetTranslation(origString
, origString2
, n
, domain
);
310 // this is hack to work around a problem with wxGetTranslation() which
311 // returns const wxString& and not wxString, so when it returns untranslated
312 // string, it needs to have a copy of it somewhere
313 static const wxString
& GetUntranslatedString(const wxString
& str
)
314 { return wxTranslations::GetUntranslatedString(str
); }
316 // Returns the current short name for the locale
317 const wxString
& GetName() const { return m_strShort
; }
319 // return the contents of .po file header
320 wxString
GetHeaderValue(const wxString
& header
,
321 const wxString
& domain
= wxEmptyString
) const;
323 // These two methods are for internal use only. First one creates
324 // ms_languagesDB if it doesn't already exist, second one destroys
326 static void CreateLanguagesDB();
327 static void DestroyLanguagesDB();
330 bool DoInit(const wxString
& name
,
331 const wxString
& shortName
,
332 const wxString
& locale
);
334 // copy default table of languages from global static array to
335 // m_langugagesInfo, called by InitLanguagesDB
336 static void InitLanguagesDB();
338 // initialize the member fields to default values
341 wxString m_strLocale
, // this locale name
342 m_strShort
; // short name for the locale
343 int m_language
; // this locale wxLanguage value
345 const char *m_pszOldLocale
; // previous locale from setlocale()
346 wxLocale
*m_pOldLocale
; // previous wxLocale
350 wxTranslations m_translations
;
352 static wxLanguageInfoArray
*ms_languagesDB
;
354 wxDECLARE_NO_COPY_CLASS(wxLocale
);
357 // ----------------------------------------------------------------------------
359 // ----------------------------------------------------------------------------
361 // get the current locale object (note that it may be NULL!)
362 extern WXDLLIMPEXP_BASE wxLocale
* wxGetLocale();
366 #endif // _WX_INTL_H_