Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

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

#include <winrt/Windows.Globalization.h>

#include <AppModel.Identity.h>

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

winrt::Windows::Foundation::Collections::IVectorView<hstring> ApplicationLanguages::Languages()
{
return winrt::Windows::Globalization::ApplicationLanguages::Languages();
}

winrt::Windows::Foundation::Collections::IVectorView<hstring> ApplicationLanguages::ManifestLanguages()
{
return winrt::Windows::Globalization::ApplicationLanguages::ManifestLanguages();
}

hstring ApplicationLanguages::PrimaryLanguageOverride()
{
static wil::srwlock lock;

auto criticalSection{ lock.lock_shared() };
return m_language;
}

void ApplicationLanguages::PrimaryLanguageOverride(hstring const& language)
{
if (AppModel::Identity::IsPackagedProcess())
{
winrt::Windows::Globalization::ApplicationLanguages::PrimaryLanguageOverride(language);
}
else
{
static wil::srwlock lock;

auto criticalSection{ lock.lock_exclusive() };
m_language = language;
}
}
} // namespace winrt::Microsoft::Windows::ApplicationModel::Resources::implementation
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 winrt::Windows::Foundation::Collections::IVectorView<hstring> Languages();
static winrt::Windows::Foundation::Collections::IVectorView<hstring> ManifestLanguages();
static hstring PrimaryLanguageOverride();
static void PrimaryLanguageOverride(hstring const& language);

private:
static hstring m_language;
};
} // namespace winrt::Microsoft::Windows::ApplicationModel::Resources::implementation

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

[contract(MrtCoreContract, 2)]
runtimeclass ApplicationLanguages
{
static IVectorView<String> Languages { get; };
static IVectorView<String> ManifestLanguages { get; };
static String PrimaryLanguageOverride;
}

} // namespace Microsoft.Windows.ApplicationModel. Resources
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
</DisableSpecificWarnings>
<PreprocessorDefinitions>_WINRT_DLL;WIN32_LEAN_AND_MEAN;WINRT_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalUsingDirectories>$(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories)</AdditionalUsingDirectories>
<AdditionalIncludeDirectories>$(RepoRoot)\dev\Common;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -109,6 +110,7 @@
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="ApplicationLanguages.h" />
<ClInclude Include="Helper.h" />
<ClInclude Include="KnownResourceQualifierName.h" />
<ClInclude Include="pch.h" />
Expand All @@ -120,6 +122,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 +143,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 @@ -171,6 +174,7 @@
<Import Project="$(NugetPackageDirectory)\Microsoft.SourceLink.Common.$(MicrosoftSourceLinkCommonVersion)\build\Microsoft.SourceLink.Common.targets" Condition="Exists('$(NugetPackageDirectory)\Microsoft.SourceLink.Common.$(MicrosoftSourceLinkCommonVersion)\build\Microsoft.SourceLink.Common.targets')" />
<Import Project="$(NugetPackageDirectory)\Microsoft.Build.Tasks.Git.$(MicrosoftBuildTasksGitVersion)\build\Microsoft.Build.Tasks.Git.targets" Condition="Exists('$(NugetPackageDirectory)\Microsoft.Build.Tasks.Git.$(MicrosoftBuildTasksGitVersion)\build\Microsoft.Build.Tasks.Git.targets')" />
<Import Project="$(NugetPackageDirectory)\Microsoft.SourceLink.GitHub.$(MicrosoftSourceLinkGitHubVersion)\build\Microsoft.SourceLink.GitHub.targets" Condition="Exists('$(NugetPackageDirectory)\Microsoft.SourceLink.GitHub.$(MicrosoftSourceLinkGitHubVersion)\build\Microsoft.SourceLink.GitHub.targets')" />
<Import Project="$(NugetPackageDirectory)\Microsoft.Windows.ImplementationLibrary.$(MicrosoftWindowsImplementationLibraryVersion)\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('$(NugetPackageDirectory)\Microsoft.Windows.ImplementationLibrary.$(MicrosoftWindowsImplementationLibraryVersion)\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
Expand All @@ -185,5 +189,6 @@
<Error Condition="!Exists('$(NugetPackageDirectory)\Microsoft.Build.Tasks.Git.$(MicrosoftBuildTasksGitVersion)\build\Microsoft.Build.Tasks.Git.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(NugetPackageDirectory)\Microsoft.Build.Tasks.Git.$(MicrosoftBuildTasksGitVersion)\build\Microsoft.Build.Tasks.Git.targets'))" />
<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'))" />
<Error Condition="!Exists('$(NugetPackageDirectory)\Microsoft.Windows.ImplementationLibrary.$(MicrosoftWindowsImplementationLibraryVersion)\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(NugetPackageDirectory)\Microsoft.Windows.ImplementationLibrary.$(MicrosoftWindowsImplementationLibraryVersion)\build\native\Microsoft.Windows.ImplementationLibrary.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
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<package id="Microsoft.SourceLink.Common" version="1.1.1" targetFramework="native" developmentDependency="true" />
<package id="Microsoft.SourceLink.GitHub" version="1.1.1" targetFramework="native" developmentDependency="true" />
<package id="Microsoft.Windows.CppWinRT" version="2.0.230706.1" targetFramework="native" />
</packages>
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.220914.1" targetFramework="native" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <winrt/Windows.Foundation.Collections.h>
#include "..\..\core\src\MRM.h"

#include <wil/result_macros.h>
#include <wil/resource.h>

struct StringResourceFreer
{
void operator()(wchar_t* resource) { MrmFreeResource(resource); }
Expand Down