-1

I plan to equip the settings page of my Windows App SDK app with a ComboBox that contains all languages my app supports so that the user can pick whatever language is desired. Graphically, it looks as follows:

enter image description here

I populate the content of the ComboBox by iterating over the languages that are returned by Windows.Globalization.ApplicationLanguages.ManifestLanguages and creating a view model for each language.

If the user then selects a language via ComboBox, I set the Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride to the language that the user selected.

These steps work as expected. But what I fail to understand is how to initially set the selected language of the ComboBox when the user enters my settings page.

To achieve that, I would need to know what language my app is currently running with. Initially, if the user hasn't picked a language yet, the PrimaryLanguageOverride property is simply empty. Therefore, I cannot use that property to get the language in that case. I can do so after the user picked a language, but not until that is the case.

So the question is, how do I query my application to get the actual language it is running with?

ackh
  • 1,648
  • 2
  • 18
  • 36
  • Are you looking for `GlobalizationPreferences.Languages` https://learn.microsoft.com/en-us/uwp/api/windows.system.userprofile.globalizationpreferences.languages ? – Simon Mourier Apr 11 '23 at 19:33
  • I don't think this is helping me. As far as i understand it, `GlobalizationPreferences.Languages` returns the list of languages that the user added in Windows under `Settings` > `Time & Language` > `Region & language` > `Languages`. Based on this list, the language of the app gets set accordingly. What I want to know is the language to which the app gets set when it is starting. – ackh Apr 11 '23 at 19:42
  • What's "the language to which the app gets set when it is starting" for you? – Simon Mourier Apr 11 '23 at 21:21
  • When the application starts, the Windows App SDK seems to consider the `ManifestLanguages` of my app, the `GlobalizationPreferences.Languages`, my app's default language and the `PrimaryLanguageOverride` property to determine the language with which the app gets started according to [this](https://learn.microsoft.com/en-us/windows/apps/design/globalizing/manage-language-and-region). The app then runs with the language that best matches. I'm trying to figure out what that language is because that language is the one in need to set the ComboBox to. – ackh Apr 11 '23 at 21:32
  • If you don't override anything programmatically or by manifest you will get GlobalizationPreferences.Languages[0] – Simon Mourier Apr 12 '23 at 05:45
  • That can only happen if my app supports `GlobalizationPreferences.Languages[0]`. If it doesn't, it will likely fall back on the app's default language. And because the entire process isn't trivial I would expect that there is an API that simply allows me to query which language is currently used by the app. But I cannot find one which means I would need to replicate what Windows App SDK is doing behind the scenes already. I'm either missing something or the people that came up with these APIs never implemented such an app themselves. – ackh Apr 12 '23 at 07:51
  • These API are the combined result of a 30+ years operating system and (UI or not) frameworks evolution. You said it, GlobalizationPreferences.Languages[0] or your app default language, not sure what more you're looking for exactly. – Simon Mourier Apr 12 '23 at 07:56
  • There isn't an API to get my app's default language that I'm aware of. It is defined via MSBuild and thus available at compile. I don't see how I can get that information at runtime. So, here we go again... Microsoft chose to throw everything overboard with Windows 8 Metro apps and to this day we still suffer from that decision because we now have the Win32 old world and the new Metro world from which WinUI / Windows App SDK evolved. And as I see day by day, that new world is far from the maturity we once had in Win32. – ackh Apr 12 '23 at 08:13

2 Answers2

0

AFAIK, you can't. I created a video about this. Eventually, I ended up by having the selected language in the app settings and set the language when the app is initialized.

Here's the GitHub repo for the sample app.

Andrew KeepCoding
  • 7,040
  • 2
  • 14
  • 21
-1

The below code returns the language that is used by the resource manager. Based on that, I then select the corresponding entry in the ComboBox.

winrt::Microsoft::Windows::ApplicationModel::Resources::ResourceManager resourceManager{};
auto selectedLanguage = resourceManager.MainResourceMap().GetValue(L"Resources/WhateverExistingResource").QualifierValues().Lookup(L"Language");
ackh
  • 1,648
  • 2
  • 18
  • 36