Coverage Report

Created: 2026-05-24 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wxwidgets/include/wx/uilocale.h
Line
Count
Source
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
    // Check if the locale was instantiated via UseDefault()
162
0
    bool IsDefault() const { return m_isDefault; }
163
164
    // Get the platform-dependent name of the current locale.
165
    wxString GetName() const;
166
167
    // Get the locale id from which the current locale was instantiated.
168
    wxLocaleIdent GetLocaleId() const;
169
170
    // Query the locale for the specified information.
171
    wxString GetInfo(wxLocaleInfo index,
172
                     wxLocaleCategory cat = wxLOCALE_CAT_DEFAULT) const;
173
174
    // Query the locale for the specified localized name.
175
    wxString GetLocalizedName(wxLocaleName name, wxLocaleForm form) const;
176
177
#if wxUSE_DATETIME
178
    // Get the full (default) or abbreviated localized month name
179
    // returns empty string on error
180
    wxString GetMonthName(wxDateTime::Month month,
181
                          wxDateTime::NameForm form = {}) const;
182
183
    // Get the full (default) or abbreviated localized weekday name
184
    // returns empty string on error
185
    wxString GetWeekDayName(wxDateTime::WeekDay weekday,
186
                            wxDateTime::NameForm form = {}) const;
187
#endif // wxUSE_DATETIME
188
189
    // Query the layout direction of the current locale.
190
    wxLayoutDirection GetLayoutDirection() const;
191
192
    // Query infos about number formatting of the current locale
193
    wxLocaleNumberFormatting GetNumberFormatting() const;
194
195
    // Query the curreny symbol of the current locale
196
    wxString GetCurrencySymbol() const;
197
198
    // Query the currency code of the current locale
199
    wxString GetCurrencyCode() const;
200
201
    // Query the currency symbol position of the current locale
202
    wxCurrencySymbolPosition GetCurrencySymbolPosition() const;
203
204
    // Query the currency infos of the current locale
205
    wxLocaleCurrencyInfo GetCurrencyInfo() const;
206
207
    // Query whether the current locale uses the metric system
208
    wxMeasurementSystem UsesMetricSystem() const;
209
210
    // Guess from the region whether the current locale uses the metric system
211
    static wxMeasurementSystem GuessMetricSystemFromRegion(const wxLocaleIdent& idLocale);
212
213
    // Compares two strings in the order defined by this locale.
214
    int CompareStrings(const wxString& lhs, const wxString& rhs,
215
                       int flags = wxCompare_CaseSensitive) const;
216
217
    // Note that this class is not supposed to be used polymorphically, hence
218
    // its dtor is not virtual.
219
    ~wxUILocale();
220
221
    // Return the locale ID representing the default system locale, which would
222
    // be set is UseDefault() is called.
223
    static wxLocaleIdent GetSystemLocaleId();
224
225
    // Try to get user's (or OS's) preferred language setting.
226
    // Return wxLANGUAGE_UNKNOWN if the language-guessing algorithm failed
227
    // Prefer using GetSystemLocaleId() above.
228
    static int GetSystemLanguage();
229
230
    // Try to get user's (or OS's) default locale setting.
231
    // Return wxLANGUAGE_UNKNOWN if the locale-guessing algorithm failed
232
    // Prefer using GetSystemLocaleId() above.
233
    static int GetSystemLocale();
234
235
    // Try to retrieve a list of user's (or OS's) preferred UI languages.
236
    // Return empty list if language-guessing algorithm failed
237
    static wxVector<wxString> GetPreferredUILanguages();
238
239
    // Retrieve the language info struct for the given language
240
    //
241
    // Returns nullptr if no info found, pointer must *not* be deleted by caller
242
    static const wxLanguageInfo* GetLanguageInfo(int lang);
243
244
    // Returns language name in English or empty string if the language
245
    // is not in database
246
    static wxString GetLanguageName(int lang);
247
248
    // Returns ISO code ("canonical name") of language or empty string if the
249
    // language is not in database
250
    static wxString GetLanguageCanonicalName(int lang);
251
252
    // Find the language for the given locale string which may be either a
253
    // canonical ISO 2 letter language code ("xx"), a language code followed by
254
    // the country code ("xx_XX") or a Windows full language name ("Xxxxx...")
255
    //
256
    // Returns nullptr if no info found, pointer must *not* be deleted by caller
257
    static const wxLanguageInfo* FindLanguageInfo(const wxString& locale);
258
259
    // Find the language for the given locale string which may be either a
260
    // canonical ISO 2 letter language code ("xx"), a language code followed by
261
    // the country code ("xx_XX") or a Windows full language name ("Xxxxx...")
262
    //
263
    // Returns nullptr if no info found, pointer must *not* be deleted by caller
264
    static const wxLanguageInfo* FindLanguageInfo(const wxLocaleIdent& locId);
265
266
    // Add custom language to the list of known languages.
267
    // Notes: 1) wxLanguageInfo contains platform-specific data
268
    //        2) must be called before Init to have effect
269
    static void AddLanguage(const wxLanguageInfo& info);
270
271
private:
272
    // This ctor is private and exists only for implementation reasons.
273
    // It takes ownership of the provided pointer.
274
2
    explicit wxUILocale(wxUILocaleImpl* impl = nullptr) : m_impl(impl) { }
275
276
    static wxUILocale ms_current;
277
278
    wxUILocaleImpl* m_impl;
279
    bool m_isDefault = false;
280
};
281
282
inline wxString wxGetUIDateFormat()
283
0
{
284
0
    return wxUILocale::GetCurrent().GetInfo(wxLOCALE_SHORT_DATE_FMT);
285
0
}
286
287
#else // !wxUSE_INTL
288
289
inline wxString wxGetUIDateFormat()
290
{
291
    return wxString(wxS("%x"));
292
}
293
294
#endif // wxUSE_INTL/!wxUSE_INTL
295
296
#endif // _WX_UILOCALE_H_