Skip to content

Conversation

whiskhub
Copy link

Fixes #5335

A microsoft employee must use /azp run to validate using the pipelines below.

WARNING:
Comments made by azure-pipelines bot maybe inaccurate.
Please see pipeline link to verify that the build is being ran.

For status checks on the main branch, please use TransportPackage-Foundation-PR
(https://microsoft.visualstudio.com/ProjectReunion/_build?definitionId=81063&_a=summary)
and run the build against your PR branch with the default parameters.

@whiskhub
Copy link
Author

@microsoft-github-policy-service agree

@whiskhub
Copy link
Author

Hi @RDMacLachlan @codendone could this Pull Request be looked at for a future release? Thank you!

void ApplicationLanguages::PrimaryLanguageOverride(hstring const& language)
{
bool isValidLanguageTag = IsWellFormedTag(language.c_str());
bool isValidLanguageTag = language.empty() || IsWellFormedTag(language.c_str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my impression is that winrt::Windows::Globalization::ApplicationLanguages::PrimaryLanguageOverride(language) requires nullptr to reset PrimaryLanguageOverride. Have you verified that the test reached line 53?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In WinRT, an empty HSTRING is equivalent to a nullptr (https://devblogs.microsoft.com/oldnewthing/20160615-00/?p=93675).

So, windows::Globalization::ApplicationLanguages::PrimaryLanguageOverride does accept both nullptr/null and empty string.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it doesn't seem possible for externals to build anything from the Windows App SDK due to missing permissions for the ProjectReunion internal NuGet source. So I was only able to test in my own app.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the implementation of winrt::Windows::Globalization::ApplicationLanguages::PrimaryLanguageOverride(language) in OS, and it will reset the override when null is passed in, and fail if empty string is passed in.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In C++/WinRT it is impossible to compare a winrt::hstring to nullptr - the equality operator is explicitly deleted: bool operator==(hstring const& left, std::nullptr_t) = delete;
This is exactly because an empty HSTRING is always represented by a null pointer internally. Please see the blog post by Raymond Chen: https://devblogs.microsoft.com/oldnewthing/20220706-00/?p=106836

Maybe the OS code is not written in C++WinRT and is therefore able to compare with nullptr, but code with C++/WinRT cannot do that.

In C#, WG.ApplicationLanguages.PrimaryLanguageOverride = null; and WG.ApplicationLanguages.PrimaryLanguageOverride = ""; will also both work. You can try in a test app.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OS implementation doesn't use C++/WinRT. I assume it works because eventually nullptr is passed to it.

@huichen123
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

ApplicationLanguages.PrimaryLanguageOverride = "fr-FR";
ApplicationLanguages.PrimaryLanguageOverride = "";

Verify.AreEqual(ApplicationLanguages.PrimaryLanguageOverride, "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider adding a verification that winrt::Windows::Globalization::ApplicationLanguages::PrimaryLanguageOverride is reset for packaged process, as that's what matters.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I've added a test

ApplicationLanguages.PrimaryLanguageOverride = null;
Verify.AreEqual(Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride, ""); // C# projection of null HSTRING is empty string
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests are added under CommonTestCode. But they don't work for unpackaged test. Ideally they need to be moved to unittests.cs, or add a packaged check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I moved the test code over

@huichen123
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@huichen123
Copy link
Contributor

The build failed:
"D:\a_work\1\s\dev\MRTCore\mrt\MrtCore.sln" (default target) (1:2) ->
"D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\MrtCoreManagedTest.csproj" (default target) (8:6) ->
(XamlPreCompile target) ->
D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\TestCommon\CommonTestCode.cs(593,13): error CS0103: The name 'ApplicationLanguages' does not exist in the current context [D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\MrtCoreManagedTest.csproj]
D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\TestCommon\CommonTestCode.cs(594,13): error CS0103: The name 'ApplicationLanguages' does not exist in the current context [D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\MrtCoreManagedTest.csproj]
D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\TestCommon\CommonTestCode.cs(596,29): error CS0103: The name 'ApplicationLanguages' does not exist in the current context [D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\MrtCoreManagedTest.csproj]
D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\TestCommon\CommonTestCode.cs(601,13): error CS0103: The name 'ApplicationLanguages' does not exist in the current context [D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\MrtCoreManagedTest.csproj]
D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\TestCommon\CommonTestCode.cs(602,13): error CS0103: The name 'ApplicationLanguages' does not exist in the current context [D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\MrtCoreManagedTest.csproj]
D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\TestCommon\CommonTestCode.cs(604,29): error CS0103: The name 'ApplicationLanguages' does not exist in the current context [D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\MrtCoreManagedTest.csproj]
D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\UnitTest.cs(187,13): error CS0103: The name 'ApplicationLanguages' does not exist in the current context [D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\MrtCoreManagedTest.csproj]
D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\UnitTest.cs(190,13): error CS0103: The name 'ApplicationLanguages' does not exist in the current context [D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\MrtCoreManagedTest.csproj]

@whiskhub
Copy link
Author

/azp run

Copy link

Commenter does not have sufficient privileges for PR 5726 in repo microsoft/WindowsAppSDK

@whiskhub
Copy link
Author

Thank you @huichen123, I hope it's fixed with the latest commit.

It’s unfortunately not easy for 3rd parties to contribute, even for basic MRs like this one. I cannot compile it locally, nor can I trigger builds or see the build results.

@huichen123
Copy link
Contributor

new failure:
"D:\a_work\1\s\dev\MRTCore\mrt\MrtCore.sln" (default target) (1:2) ->
"D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\MrtCoreManagedTest.csproj" (default target) (8:6) ->
(XamlPreCompile target) ->
D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\TestCommon\CommonTestCode.cs(22,25): error CS0234: The type or namespace name 'Globalization' does not exist in the namespace 'Microsoft.Windows' (are you missing an assembly reference?) [D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\MrtCoreManagedTest.csproj]
D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\UnitTest.cs(6,25): error CS0234: The type or namespace name 'Globalization' does not exist in the namespace 'Microsoft.Windows' (are you missing an assembly reference?) [D:\a_work\1\s\dev\MRTCore\mrt\Microsoft.Windows.ApplicationModel.Resources\unittests\MrtCoreManagedTest.csproj]

@whiskhub
Copy link
Author

whiskhub commented Oct 1, 2025

It seems like CsWinRT doesn't generate a projection for Microsoft.Windows.Globalization (https://github.com/whiskhub/WindowsAppSDK/blob/main/dev/MRTCore/mrt/Microsoft.Windows.ApplicationModel.Resources/projection/Microsoft.Windows.ApplicationModel.Resources.Projection.csproj), and therefore the test project cannot access it. But I cannot see why it works for Microsoft.Windows.ApplicationModel.Resources then. Maybe somehow the winmd file is not properly included.

Sorry, without being able to debug this locally, I cannot fix it.

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

Successfully merging this pull request may close these issues.

ApplicationLanguages.PrimaryLanguageOverride cannot be unset
2 participants