1

I wanted to get the system language (not the input language, but the overall system language) in the C++/WinRT app, but can't find a way for doing such. I googled and found that in Unity this can be done using System.Globalization.CultureInfo.CurrentCulture structure, which is not available in C++ code. Anybody's familiar with this?

Kenny Kerr
  • 3,015
  • 15
  • 18
arsdever
  • 1,111
  • 1
  • 11
  • 32
  • I'm not aware of a Windows Runtime type, but since you are using C++, you can simply call into [GetLocaleInfoEx](https://learn.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getlocaleinfoex). – IInspectable Aug 11 '20 at 13:10

1 Answers1

1

Getting system language UWP C++/WinRT

You can use the Language Class to get the system language. First, you need to include the header files it needs. For example:

#include <winrt/Windows.System.UserProfile.h>
#include <winrt/Windows.Globalization.h>

void MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
{
    auto topUserLaunghage = Windows::System::UserProfile::GlobalizationPreferences::Languages().GetAt(0);
    Windows::Globalization::Language language{topUserLaunghage};
    hstring displayName = language.DisplayName();
}

Update:

When you change the Regional format, the date, time, etc will change its format, you could get the language that was most recently used to format dates and times by using ResolvedLanguage of DateTimeFormatter. For example:

Windows::Globalization::GeographicRegion userRegion{};
hstring regionCode = userRegion.CodeTwoLetter();
Windows::Foundation::Collections::IVector<hstring> coll{ winrt::single_threaded_vector<hstring>() };
coll.Append(regionCode);
auto dateTimeFormatter = Windows::Globalization::DateTimeFormatting::DateTimeFormatter(L"longdate", coll);
auto regionInfoName = dateTimeFormatter.ResolvedLanguage(); // This is the language from the Regional format
Faywang - MSFT
  • 5,798
  • 1
  • 5
  • 8
  • I have already seen this on MSDN. The thing is, on the first line, it gets the input language of the user or system, but not the UI language. What I want is to get the system UI language – arsdever Aug 13 '20 at 06:17
  • Are you referring to the UI language which can be set by an administrator in the Welcome screen and new user accounts settings? Do you want to get the Display language of Welcome screen? – Faywang - MSFT Aug 14 '20 at 08:29
  • Settings > Time & Language > Region > Regional format (isn't this the UI language?) – arsdever Aug 14 '20 at 08:32
  • For Unity, as I said, the parameter holds this exact property set on the system – arsdever Aug 14 '20 at 08:34
  • I have found out that I no longer need the answer of this question, but any way will accept it if anybody else needs the solution. Thanks for the info. – arsdever Aug 17 '20 at 09:11