Search code examples
uwplocalizationcultureinfoculture

Determine which resource culture fallback has been chosen by Windows 10 in UWP


I have a UWP app with the following Resource files for localization

en

es-ES

fr-CA

fr-LU

I have set in Windows 10's settings the preferred languages in priority order for testing purposes

Italian

French (France)

English (UK)

Hence my OS is currently in the Italian language.

The app displays a TextBlock with a x:Uid="Test", where Test.Text is set as a key in each of the resources files, and the value is of the form "This is a test coming from es-ES" for the resource file es-ES, etc.

Running the app shows that fr-LU has been chosen.

From reading the docs, I understand that the CurrentUICulture is set to fr-FR for this app, probably because the Italian resource file is not found, so the second choice is chosen, fr-FR, but then fr-FR is not found, hence a sibling is chosen, ending in fr-LU. It seems the system choses this over fr-CA because it is the last in the list of the ManifestLanguages. I even added a resource file of a non-existing culture, fr-ZZ, and it gets selected! That is weird but it is not my problem since I am meant to put only valid cultures.

Question: How can I determine in code which resource or culture tag has been eventually chosen (fr-LU)?

I can answer this question the silly way, by setting Test.Text to be exactly equal to the culture, use the resource loader and read the value, to then transform it to a culture or override the primary language. But that is reverse engineering that will strongly depend on a string in my resource files for only that purpose.

I am trying to solve this problem because I am presenting a Listview of the ManifestLanguages to allow the user to change languages, and want the language (here fr-LU) to be the SelectedItem of the ListView because this is what the system decided to display upon launch. Thank you


Solution

  • Running the app shows that fr-LU has been chosen.

    From this document, the system will perform resource matching in the order of 1. Exact match; 2. & 3. Region match; 4. Parent match; 5. Sibling match. In this case, sibling matching is performed. About sibling match, the document mentions in the event of multiple sibling matches, the last enumerated sibling will be the winner, in the absence of a higher match. Since the order of fr-LU is at the end, so the system chooses it as the display language. fr-ZZ does the same.

    How can I determine in code which resource or culture tag has been eventually chosen (fr-LU)?

    You can't directly get the resource which has been chosen, because this is choice of system. If you still want to get it, using resource loader is recommended.