Add wxUILocale::GetSystemLocaleId()

This function replaces the existing GetSystemLocale() as it can
represent the locales that don't have any corresponding wxLanguage
values and for which GetSystemLocale() has no choice but to return
wxLANGUAGE_UNKNOWN.
This commit is contained in:
Vadim Zeitlin 2023-02-05 16:40:27 +01:00
parent 9f2a416f81
commit 14714856b3
4 changed files with 35 additions and 9 deletions

View file

@ -158,12 +158,18 @@ public:
// its dtor is not virtual.
~wxUILocale();
// Return the locale ID representing the default system locale, which would
// be set is UseDefault() is called.
static wxLocaleIdent GetSystemLocaleId();
// Try to get user's (or OS's) preferred language setting.
// Return wxLANGUAGE_UNKNOWN if the language-guessing algorithm failed
// Prefer using GetSystemLocaleId() above.
static int GetSystemLanguage();
// Try to get user's (or OS's) default locale setting.
// Return wxLANGUAGE_UNKNOWN if the locale-guessing algorithm failed
// Prefer using GetSystemLocaleId() above.
static int GetSystemLocale();
// Try to retrieve a list of user's (or OS's) preferred UI languages.

View file

@ -289,7 +289,7 @@ public:
by the operating system (for example, Windows 7 and below), the user's
default @em locale will be used.
@see wxTranslations::GetBestTranslation().
@see wxTranslations::GetBestTranslation(), GetSystemLocaleId().
*/
static int GetSystemLanguage();
@ -297,7 +297,8 @@ public:
Tries to detect the user's default locale setting.
Returns the ::wxLanguage value or @c wxLANGUAGE_UNKNOWN if the locale-guessing
algorithm failed.
algorithm failed or if the locale can't be described using solely a
language constant. Consider using GetSystemLocaleId() in this case.
@note This function works with @em locales and returns the user's default
locale. This may be, and usually is, the same as their preferred UI
@ -308,7 +309,19 @@ public:
@see wxTranslations::GetBestTranslation().
*/
static int GetSystemLocale();};
static int GetSystemLocale();
/**
Return the description of the default system locale.
This function can always represent the system locale, even when using
a language and region pair that doesn't correspond to any of the
predefined ::wxLanguage constants, such as e.g. "fr-DE", which means
French language used with German locale settings.
@since 3.3.0
*/
};
/**
Return the format to use for formatting user-visible dates.

View file

@ -632,6 +632,13 @@ wxUILocale::~wxUILocale()
}
/* static */
wxLocaleIdent wxUILocale::GetSystemLocaleId()
{
wxUILocale defaultLocale(wxUILocaleImpl::CreateUserDefault());
return defaultLocale.GetLocaleId();
}
/*static*/
int wxUILocale::GetSystemLanguage()
{
@ -677,11 +684,9 @@ int wxUILocale::GetSystemLanguage()
/*static*/
int wxUILocale::GetSystemLocale()
{
// Create default wxUILocale
wxUILocale defaultLocale(wxUILocaleImpl::CreateUserDefault());
const wxLocaleIdent locId = defaultLocale.GetLocaleId();
const wxLocaleIdent locId = GetSystemLocaleId();
// Find corresponding wxLanguageInfo
// Find wxLanguageInfo corresponding to the default locale.
const wxLanguageInfo* defaultLanguage = wxUILocale::FindLanguageInfo(locId);
// Check if it really corresponds to this locale: we could find it via the

View file

@ -480,9 +480,11 @@ wxString GetLocaleDesc(const char* when)
// ways to change the current locale.
TEST_CASE("wxUILocale::ShowSystem", "[.]")
{
WARN("System locale:\t"
WARN("System locale identifier:\t"
<< wxUILocale::GetSystemLocaleId().GetTag() << "\n"
"System locale as language:\t"
<< GetLangName(wxUILocale::GetSystemLocale()) << "\n"
"System language:\t"
"System language identifier:\t"
<< GetLangName(wxUILocale::GetSystemLanguage()));
WARN(GetLocaleDesc("Before calling any locale functions"));