Skip to content

Explicit ResourceContext Language is ignored when PrimaryLanguageOverride is set #6801

Description

@MrtSafin

Describe the bug

When Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride is non-empty, ResourceMap.TryGetValue(key, explicitResourceContext) resolves the resource using the global override instead of explicitResourceContext.QualifierValues["Language"]. The context still reports the requested language, but the returned candidate has the override language. This prevents an app from loading a string in a second language while its UI language is overridden.

Observed with Windows App SDK 2.4.0 and a PRI containing en-US, ru-RU, and kk-KZ variants of the same key. With no PrimaryLanguageOverride, the same contexts return the correct translations.

Steps to reproduce the bug

  1. Create a PRI with the same string key in Strings/en-US, Strings/ru-RU, and Strings/kk-KZ (for example, Country = Kazakhstan / Казахстан / Қазақстан).
  2. Load that PRI using Microsoft.Windows.ApplicationModel.Resources.ResourceManager. In an unpackaged .NET app, pass the PRI path to the constructor.
  3. Set Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "en-US".
  4. Run:
var manager = new ResourceManager(priPath);
var map = manager.MainResourceMap.GetSubtree("Resources");
foreach (var language in new[] { "en-US", "ru-RU", "kk-KZ" })
{
    var context = manager.CreateResourceContext();
    context.QualifierValues["Language"] = language;
    var candidate = map.TryGetValue("Country", context);
    Console.WriteLine($"{language}: {candidate?.QualifierValues["Language"]}: {candidate?.ValueAsString}");
}

Actual result with PrimaryLanguageOverride en-US: all three lookups return the en-US candidate. For example, ru-RU: en-US: Kazakhstan. Without the override, the respective en-US, ru-RU, and kk-KZ candidates are returned. The issue also reproduces in our packaged WinUI test app when its UI language is switched to en-US and a separate localizer requests ru-RU.

Expected behavior

An explicitly supplied ResourceContext with QualifierValues["Language"] = "ru-RU" should select the ru-RU candidate even when the application-wide PrimaryLanguageOverride is en-US. The global override should determine the default context, while the explicit context controls this particular lookup, as described in the Windows App SDK localization documentation.

Screenshots

No response

NuGet package version

2.4.0

Packaging type

Unpackaged

Windows version

No response

IDE

Other

Additional context

Environment: Windows build 26200; Visual Studio 2026; Microsoft.WindowsAppSDK NuGet 2.4.0.

The behavior appears to come from ResourceContext::Apply(): it applies entries from m_qualifierValueMap with MrmSetQualifier, then calls MrmSetQualifier for Language again using ApplicationLanguages::PrimaryLanguageOverride() when non-empty. Source: https://github.com/microsoft/WindowsAppSDK/blob/main/dev/MRTCore/mrt/Microsoft.Windows.ApplicationModel.Resources/src/ResourceContext.cpp#L596-L632

The documented explicit-context example uses resourceContext.QualifierValues["Language"] = "de-DE" to load a string for a specific language: https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/mrtcore/localize-strings#load-a-string-for-a-specific-language-or-other-context

As a control, directly calling the native MRM API (MrmCreateResourceContext, MrmSetQualifier, MrmLoadStringResource) on the same PRI returns the requested en-US, ru-RU, and kk-KZ variants even while PrimaryLanguageOverride is en-US. This points to the Windows App SDK ResourceContext wrapper rather than missing PRI translations.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions