/src/wxwidgets/include/wx/uilocale.h
Line | Count | Source (jump to first uncovered line) |
1 | | /////////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/uilocale.h |
3 | | // Purpose: wxUILocale class declaration. |
4 | | // Author: Vadim Zeitlin |
5 | | // Created: 2021-07-31 |
6 | | // Copyright: (c) 2021 Vadim Zeitlin <vadim@wxwidgets.org> |
7 | | // Licence: wxWindows licence |
8 | | /////////////////////////////////////////////////////////////////////////////// |
9 | | |
10 | | #ifndef _WX_UILOCALE_H_ |
11 | | #define _WX_UILOCALE_H_ |
12 | | |
13 | | #include "wx/defs.h" |
14 | | |
15 | | #if wxUSE_INTL |
16 | | |
17 | | #include "wx/datetime.h" |
18 | | #include "wx/localedefs.h" |
19 | | #include "wx/string.h" |
20 | | #include "wx/vector.h" |
21 | | |
22 | | class wxUILocaleImpl; |
23 | | |
24 | | // Flags for wxUILocale::CompareStrings(). |
25 | | enum |
26 | | { |
27 | | wxCompare_CaseSensitive = 0, |
28 | | wxCompare_CaseInsensitive = 1 |
29 | | }; |
30 | | |
31 | | // Flags for wxLocaleIdent::RemoveLikelySubtags(). |
32 | | enum |
33 | | { |
34 | | wxSubtags_FavourRegion = 0, |
35 | | wxSubtags_FavourScript = 1 |
36 | | }; |
37 | | |
38 | | // ---------------------------------------------------------------------------- |
39 | | // wxLocaleIdent: allows to fully identify a locale under all platforms |
40 | | // ---------------------------------------------------------------------------- |
41 | | |
42 | | class WXDLLIMPEXP_BASE wxLocaleIdent |
43 | | { |
44 | | public: |
45 | | // Create the object from BCP 47-like language tag: the string must contain |
46 | | // at least the language part (2 or 3 ASCII letters) and may contain script |
47 | | // and region separated by dashes. |
48 | | static wxLocaleIdent FromTag(const wxString& tag); |
49 | | |
50 | | // Default ctor creates an empty, invalid identifier. |
51 | 0 | wxLocaleIdent() = default; |
52 | | |
53 | | // Set language |
54 | | wxLocaleIdent& Language(const wxString& language); |
55 | | |
56 | | // Set region |
57 | | wxLocaleIdent& Region(const wxString& region); |
58 | | |
59 | | // Set script (not supported and ignored under Unix) |
60 | | wxLocaleIdent& Script(const wxString& script); |
61 | | |
62 | | // Set charset (only supported under Unix) |
63 | | wxLocaleIdent& Charset(const wxString& charset); |
64 | | |
65 | | // Set modifier (only supported under Unix) |
66 | | wxLocaleIdent& Modifier(const wxString& modifier); |
67 | | |
68 | | // Set extension (only supported under Windows) |
69 | | wxLocaleIdent& Extension(const wxString& extension); |
70 | | |
71 | | // Set sort order (only supported under Windows) |
72 | | wxLocaleIdent& SortOrder(const wxString& sortorder); |
73 | | |
74 | | // Accessors for the individual fields. |
75 | 0 | const wxString& GetLanguage() const { return m_language; } |
76 | 0 | const wxString& GetRegion() const { return m_region; } |
77 | 0 | const wxString& GetScript() const { return m_script; } |
78 | 0 | const wxString& GetCharset() const { return m_charset; } |
79 | 0 | const wxString& GetModifier() const { return m_modifier; } |
80 | 0 | const wxString& GetExtension() const { return m_extension; } |
81 | 0 | const wxString& GetSortorder() const { return m_sortorder; } |
82 | | |
83 | | // Construct platform dependent name |
84 | | wxString GetName() const; |
85 | | |
86 | | // Get the language tag: for the objects created with FromTag() returns the |
87 | | // string passed to it directly, otherwise reconstructs this string from |
88 | | // the components. |
89 | | wxString GetTag(wxLocaleTagType tagType = wxLOCALE_TAGTYPE_DEFAULT) const; |
90 | | |
91 | | // Empty locale identifier is invalid. at least Language() must be called. |
92 | | bool IsEmpty() const |
93 | 0 | { |
94 | 0 | return m_language.empty(); |
95 | 0 | } |
96 | | |
97 | | // Methods for internal use only |
98 | | // Find the best match between desired and supported languages/locales. |
99 | | static wxString GetBestMatch(const wxVector<wxString>& desired, const wxVector<wxString>& supported); |
100 | | static wxString GetBestMatch(const wxString& desired, const wxVector<wxString>& supported); |
101 | | |
102 | | private: |
103 | | wxString m_tag; |
104 | | |
105 | | wxString m_language; |
106 | | wxString m_region; |
107 | | wxString m_script; |
108 | | wxString m_charset; |
109 | | wxString m_modifier; |
110 | | wxString m_extension; |
111 | | wxString m_sortorder; |
112 | | |
113 | | // Add likely subtags to a given locale identifier. |
114 | | static wxLocaleIdent AddLikelySubtags(const wxLocaleIdent& localeIdent); |
115 | | |
116 | | // Remove likely subtags from a given locale identifier, favor region. |
117 | | static wxLocaleIdent RemoveLikelySubtags(const wxLocaleIdent& localeIdent, int subtagsFavour = wxSubtags_FavourRegion); |
118 | | }; |
119 | | |
120 | | // ---------------------------------------------------------------------------- |
121 | | // wxUILocale allows to use the default UI locale and get information about it |
122 | | // ---------------------------------------------------------------------------- |
123 | | |
124 | | class WXDLLIMPEXP_BASE wxUILocale |
125 | | { |
126 | | public: |
127 | | // Configure the UI to use the default user locale. |
128 | | static bool UseDefault(); |
129 | | |
130 | | // Use the locale corresponding to the given POSIX locale, e.g. "de_DE.UTF-8". |
131 | | // |
132 | | // This is a compatibility function used by wxWidgets itself, don't use it |
133 | | // in the new code. |
134 | | static bool UseLocaleName(const wxString& localeName); |
135 | | |
136 | | // Return true if the locale was set by calling either UseDefault() or |
137 | | // UseLocaleName(). |
138 | | static bool IsSet(); |
139 | | |
140 | | // Get the object corresponding to the currently used locale, which is |
141 | | // always valid: if IsSet() is false, "C" locale is returned. |
142 | | static const wxUILocale& GetCurrent(); |
143 | | |
144 | | // A helper just to avoid writing wxUILocale(wxLocaleIdent::FromTag(...)). |
145 | | static wxUILocale FromTag(const wxString& tag) |
146 | 0 | { |
147 | 0 | return wxUILocale(wxLocaleIdent::FromTag(tag)); |
148 | 0 | } |
149 | | |
150 | | // Create the object corresponding to the given locale. |
151 | | explicit wxUILocale(const wxLocaleIdent& localeId); |
152 | | |
153 | | // Objects of this class can be (cheaply) copied. |
154 | | wxUILocale(const wxUILocale& loc); |
155 | | wxUILocale& operator=(const wxUILocale& loc); |
156 | | |
157 | | // Check if the locale is actually supported by the current system: if it's |
158 | | // not supported, the other functions will behave as for the "C" locale. |
159 | | bool IsSupported() const; |
160 | | |
161 | | // Get the platform-dependent name of the current locale. |
162 | | wxString GetName() const; |
163 | | |
164 | | // Get the locale id from which the current locale was instantiated. |
165 | | wxLocaleIdent GetLocaleId() const; |
166 | | |
167 | | // Query the locale for the specified information. |
168 | | wxString GetInfo(wxLocaleInfo index, |
169 | | wxLocaleCategory cat = wxLOCALE_CAT_DEFAULT) const; |
170 | | |
171 | | // Query the locale for the specified localized name. |
172 | | wxString GetLocalizedName(wxLocaleName name, wxLocaleForm form) const; |
173 | | |
174 | | #if wxUSE_DATETIME |
175 | | // Get the full (default) or abbreviated localized month name |
176 | | // returns empty string on error |
177 | | wxString GetMonthName(wxDateTime::Month month, |
178 | | wxDateTime::NameForm form = {}) const; |
179 | | |
180 | | // Get the full (default) or abbreviated localized weekday name |
181 | | // returns empty string on error |
182 | | wxString GetWeekDayName(wxDateTime::WeekDay weekday, |
183 | | wxDateTime::NameForm form = {}) const; |
184 | | #endif // wxUSE_DATETIME |
185 | | |
186 | | // Query the layout direction of the current locale. |
187 | | wxLayoutDirection GetLayoutDirection() const; |
188 | | |
189 | | // Compares two strings in the order defined by this locale. |
190 | | int CompareStrings(const wxString& lhs, const wxString& rhs, |
191 | | int flags = wxCompare_CaseSensitive) const; |
192 | | |
193 | | // Note that this class is not supposed to be used polymorphically, hence |
194 | | // its dtor is not virtual. |
195 | | ~wxUILocale(); |
196 | | |
197 | | // Return the locale ID representing the default system locale, which would |
198 | | // be set is UseDefault() is called. |
199 | | static wxLocaleIdent GetSystemLocaleId(); |
200 | | |
201 | | // Try to get user's (or OS's) preferred language setting. |
202 | | // Return wxLANGUAGE_UNKNOWN if the language-guessing algorithm failed |
203 | | // Prefer using GetSystemLocaleId() above. |
204 | | static int GetSystemLanguage(); |
205 | | |
206 | | // Try to get user's (or OS's) default locale setting. |
207 | | // Return wxLANGUAGE_UNKNOWN if the locale-guessing algorithm failed |
208 | | // Prefer using GetSystemLocaleId() above. |
209 | | static int GetSystemLocale(); |
210 | | |
211 | | // Try to retrieve a list of user's (or OS's) preferred UI languages. |
212 | | // Return empty list if language-guessing algorithm failed |
213 | | static wxVector<wxString> GetPreferredUILanguages(); |
214 | | |
215 | | // Retrieve the language info struct for the given language |
216 | | // |
217 | | // Returns nullptr if no info found, pointer must *not* be deleted by caller |
218 | | static const wxLanguageInfo* GetLanguageInfo(int lang); |
219 | | |
220 | | // Returns language name in English or empty string if the language |
221 | | // is not in database |
222 | | static wxString GetLanguageName(int lang); |
223 | | |
224 | | // Returns ISO code ("canonical name") of language or empty string if the |
225 | | // language is not in database |
226 | | static wxString GetLanguageCanonicalName(int lang); |
227 | | |
228 | | // Find the language for the given locale string which may be either a |
229 | | // canonical ISO 2 letter language code ("xx"), a language code followed by |
230 | | // the country code ("xx_XX") or a Windows full language name ("Xxxxx...") |
231 | | // |
232 | | // Returns nullptr if no info found, pointer must *not* be deleted by caller |
233 | | static const wxLanguageInfo* FindLanguageInfo(const wxString& locale); |
234 | | |
235 | | // Find the language for the given locale string which may be either a |
236 | | // canonical ISO 2 letter language code ("xx"), a language code followed by |
237 | | // the country code ("xx_XX") or a Windows full language name ("Xxxxx...") |
238 | | // |
239 | | // Returns nullptr if no info found, pointer must *not* be deleted by caller |
240 | | static const wxLanguageInfo* FindLanguageInfo(const wxLocaleIdent& locId); |
241 | | |
242 | | // Add custom language to the list of known languages. |
243 | | // Notes: 1) wxLanguageInfo contains platform-specific data |
244 | | // 2) must be called before Init to have effect |
245 | | static void AddLanguage(const wxLanguageInfo& info); |
246 | | |
247 | | private: |
248 | | // This ctor is private and exists only for implementation reasons. |
249 | | // It takes ownership of the provided pointer. |
250 | 2 | explicit wxUILocale(wxUILocaleImpl* impl = nullptr) : m_impl(impl) { } |
251 | | |
252 | | static wxUILocale ms_current; |
253 | | |
254 | | wxUILocaleImpl* m_impl; |
255 | | }; |
256 | | |
257 | | inline wxString wxGetUIDateFormat() |
258 | 0 | { |
259 | 0 | return wxUILocale::GetCurrent().GetInfo(wxLOCALE_SHORT_DATE_FMT); |
260 | 0 | } |
261 | | |
262 | | #else // !wxUSE_INTL |
263 | | |
264 | | inline wxString wxGetUIDateFormat() |
265 | | { |
266 | | return wxString(wxS("%x")); |
267 | | } |
268 | | |
269 | | #endif // wxUSE_INTL/!wxUSE_INTL |
270 | | |
271 | | #endif // _WX_UILOCALE_H_ |