Expose a library's Resource Loader, allow a user to "subset" it (#19131)

This pull request elevates ScopedResourceLoader to the API surface of
LibraryResources, which will allow any library resource consumer to
directly interact with its resource loader.

One of those new interactions is to make a sub-context with a specific
narrowed-down qualifier. Like this:

```c++
auto englishOnlyLoader = GetLibraryResourceLoader().WithQualifier(L"language", L"en-us");
/* auto foo = */ englishOnlyLoader.GetLocalizedString(USES_RESOURCE(L"AppName"));
```
This commit is contained in:
Dustin L. Howett 2025-07-14 17:43:48 -05:00 committed by GitHub
parent f2b30b4e1e
commit bdf44322f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 6 deletions

View File

@ -76,10 +76,10 @@ static void EnsureAllResourcesArePresent(const ScopedResourceLoader& loader)
#endif
static ScopedResourceLoader GetLibraryResourceLoader()
const ScopedResourceLoader& GetLibraryResourceLoader()
try
{
ScopedResourceLoader loader{ g_WinRTUtilsLibraryResourceScope };
static ScopedResourceLoader loader{ g_WinRTUtilsLibraryResourceScope };
#ifdef _DEBUG
EnsureAllResourcesArePresent(loader);
#endif
@ -90,15 +90,13 @@ CATCH_FAIL_FAST()
winrt::hstring GetLibraryResourceString(const std::wstring_view key)
try
{
static auto loader{ GetLibraryResourceLoader() };
return loader.GetLocalizedString(key);
return GetLibraryResourceLoader().GetLocalizedString(key);
}
CATCH_FAIL_FAST()
bool HasLibraryResourceWithName(const std::wstring_view key)
try
{
static auto loader{ GetLibraryResourceLoader() };
return loader.HasResourceWithName(key);
return GetLibraryResourceLoader().HasResourceWithName(key);
}
CATCH_FAIL_FAST()

View File

@ -46,3 +46,14 @@ bool ScopedResourceLoader::HasResourceWithName(const std::wstring_view resourceN
{
return _resourceMap.HasKey(resourceName);
}
ScopedResourceLoader::ScopedResourceLoader(winrt::Windows::ApplicationModel::Resources::Core::ResourceMap map, winrt::Windows::ApplicationModel::Resources::Core::ResourceContext context) noexcept :
_resourceMap{ std::move(map) }, _resourceContext{ std::move(context) } {}
ScopedResourceLoader ScopedResourceLoader::WithQualifier(const wil::zwstring_view qualifierName, const wil::zwstring_view qualifierValue) const
{
auto newContext = _resourceContext.Clone();
auto qualifierValues = newContext.QualifierValues();
qualifierValues.Insert(qualifierName, qualifierValue); // mutable!
return ScopedResourceLoader{ _resourceMap, std::move(newContext) };
}

View File

@ -67,6 +67,9 @@ namespace Microsoft::Console::Utils
__pragma(warning(suppress : 26485)); \
__declspec(selectany) extern const wchar_t* g_WinRTUtilsLibraryResourceScope{ (x) };
class ScopedResourceLoader;
const ScopedResourceLoader& GetLibraryResourceLoader();
winrt::hstring GetLibraryResourceString(const std::wstring_view key);
bool HasLibraryResourceWithName(const std::wstring_view key);

View File

@ -11,7 +11,10 @@ public:
winrt::hstring GetLocalizedString(const std::wstring_view resourceName) const;
bool HasResourceWithName(const std::wstring_view resourceName) const;
ScopedResourceLoader WithQualifier(const wil::zwstring_view qualifierName, const wil::zwstring_view qualifierValue) const;
private:
ScopedResourceLoader(winrt::Windows::ApplicationModel::Resources::Core::ResourceMap map, winrt::Windows::ApplicationModel::Resources::Core::ResourceContext context) noexcept;
winrt::Windows::ApplicationModel::Resources::Core::ResourceMap _resourceMap;
winrt::Windows::ApplicationModel::Resources::Core::ResourceContext _resourceContext;
};