Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRTCore] Add Microsoft.Windows.Globalization.ApplicationLanguages class #4181

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#include "pch.h"
#include "ApplicationLanguages.h"
#include "ApplicationLanguages.g.cpp"

namespace winrt::Microsoft::Windows::ApplicationModel::Resources::implementation
{
hstring ApplicationLanguages::m_language;

void ApplicationLanguages::PrimaryLanguageOverride(hstring language)
stefansjfw marked this conversation as resolved.
Show resolved Hide resolved
{
m_language = language;
}

hstring ApplicationLanguages::PrimaryLanguageOverride()
{
return m_language;
}

} // namespace winrt::Microsoft::Windows::ApplicationModel::Resources::implementation
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#pragma once
#include "ApplicationLanguages.g.h"

namespace winrt::Microsoft::Windows::ApplicationModel::Resources::implementation
{

struct ApplicationLanguages
{
ApplicationLanguages() = delete;

static void PrimaryLanguageOverride(hstring language);
static hstring PrimaryLanguageOverride();

private:
static hstring m_language;

Choose a reason for hiding this comment

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

What happens with multiple threads?

Copy link
Member

Choose a reason for hiding this comment

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

wil::srwlock could be used to protect access to this variable.

};

} // namespace winrt::Microsoft::Windows::ApplicationModel::Resources::implementation

namespace winrt::Microsoft::Windows::ApplicationModel::Resources::factory_implementation
{
struct ApplicationLanguages : ApplicationLanguagesT<ApplicationLanguages, implementation::ApplicationLanguages>
{
};
}
// namespace winrt::Microsoft::Windows::ApplicationModel::Resources::factory_implementation
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,11 @@ namespace Microsoft.Windows.ApplicationModel.Resources
static String TargetSize { get; };
static String Theme { get; };
}
} // namespace Microsoft.Windows.ApplicationModel.Resources

[contract(MrtCoreContract, 1)]
stefansjfw marked this conversation as resolved.
Show resolved Hide resolved
runtimeclass ApplicationLanguages
stefansjfw marked this conversation as resolved.
Show resolved Hide resolved
{
static String PrimaryLanguageOverride;
stefansjfw marked this conversation as resolved.
Show resolved Hide resolved
stefansjfw marked this conversation as resolved.
Show resolved Hide resolved
}

} // namespace Microsoft.Windows.ApplicationModel. Resources
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="ApplicationLanguages.h" />
<ClInclude Include="Helper.h" />
<ClInclude Include="KnownResourceQualifierName.h" />
<ClInclude Include="pch.h" />
Expand All @@ -120,6 +121,7 @@
<ClInclude Include="ResourceNotFoundEventArgs.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="ApplicationLanguages.cpp" />
<ClCompile Include="Helper.cpp" />
<ClCompile Include="KnownResourceQualifierName.cpp" />
<ClCompile Include="pch.cpp">
Expand All @@ -140,7 +142,7 @@
<None Include="Microsoft.Windows.ApplicationModel.Resources.def" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Microsoft.Windows.ApplicationModel.Resources.rc">
<ResourceCompile Include="Microsoft.Windows.ApplicationModel.Resources.rc">
<AdditionalIncludeDirectories>$(RepoRoot);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ResourceCompile>
</ItemGroup>
Expand Down Expand Up @@ -186,4 +188,4 @@
<Error Condition="!Exists('$(NugetPackageDirectory)\Microsoft.SourceLink.GitHub.$(MicrosoftSourceLinkGitHubVersion)\build\Microsoft.SourceLink.GitHub.props')" Text="$([System.String]::Format('$(ErrorText)', '$(NugetPackageDirectory)\Microsoft.SourceLink.GitHub.$(MicrosoftSourceLinkGitHubVersion)\build\Microsoft.SourceLink.GitHub.props'))" />
<Error Condition="!Exists('$(NugetPackageDirectory)\Microsoft.SourceLink.GitHub.$(MicrosoftSourceLinkGitHubVersion)\build\Microsoft.SourceLink.GitHub.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(NugetPackageDirectory)\Microsoft.SourceLink.GitHub.$(MicrosoftSourceLinkGitHubVersion)\build\Microsoft.SourceLink.GitHub.targets'))" />
</Target>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<ClCompile Include="KnownResourceQualifierName.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ApplicationLanguages.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
Expand Down Expand Up @@ -58,6 +61,9 @@
<ClInclude Include="KnownResourceQualifierName.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ApplicationLanguages.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Midl Include="Microsoft.Windows.ApplicationModel.Resources.idl" />
Expand All @@ -75,4 +81,7 @@
<UniqueIdentifier>{54696afe-f42a-494a-bc24-3fd0d4192122}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Microsoft.Windows.ApplicationModel.Resources.rc" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ void ResourceContext::Apply()
winrt::check_hresult(MrmSetQualifier(m_resourceContext, eachValue.Key().c_str(), eachValue.Value().c_str()));
}
}
if (!ApplicationLanguages::PrimaryLanguageOverride().empty())
Copy link
Contributor

Choose a reason for hiding this comment

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

PrimaryLanguageOverride

explicit language context should trump PrimaryLanguageOverride

Copy link
Contributor Author

@stefansjfw stefansjfw Jun 26, 2024

Choose a reason for hiding this comment

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

With W.G.PrimaryLanguageOverride set, newly created ResourceContext takes PrimaryLanguageOverride into account, i.e. does not override language QualifierValue to default language.

Copy link
Contributor

Choose a reason for hiding this comment

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

e.g. if default language is "en-US", PrimaryLanguageOverride is "de-DE", and ResourceContext["Lanauge"]="fr-FR", what's the language that will take effect? I'd think "fr-FR"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

according to this logic, no, PrimaryLanguageOverride should take precedence over ResourceContext["Language"]. GetLangugageContext returns Windows::Globalization::ApplicationLanguages::Languages() which is affected if W.G.PrimaryLanguageOverride is set. This behavior remains the same with this change

Copy link
Contributor

Choose a reason for hiding this comment

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

I think in that logic ResourceContext["Language"] still takes precedence. It would be strange to the caller that an explicit request is not honored

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it now. You're right. Fixed by latest change. Now explicit set of context Language is not overridden by PrimaryLanguageOverride.

I tested with this WinAppSDK sample. Default language is PrimaryLanguageOverride - de-DE, but fetching resources with m_overrideResourceContext passed is fetching it-IT resources:

image

{
winrt::check_hresult(MrmSetQualifier(m_resourceContext, c_languageQualifierName, ApplicationLanguages::PrimaryLanguageOverride().c_str()));
}
}

hstring ResourceContext::GetLangugageContext()
Expand Down