Skip to content

Commit bdf4432

Browse files
authored
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")); ```
1 parent f2b30b4 commit bdf4432

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/cascadia/WinRTUtils/LibraryResources.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ static void EnsureAllResourcesArePresent(const ScopedResourceLoader& loader)
7676

7777
#endif
7878

79-
static ScopedResourceLoader GetLibraryResourceLoader()
79+
const ScopedResourceLoader& GetLibraryResourceLoader()
8080
try
8181
{
82-
ScopedResourceLoader loader{ g_WinRTUtilsLibraryResourceScope };
82+
static ScopedResourceLoader loader{ g_WinRTUtilsLibraryResourceScope };
8383
#ifdef _DEBUG
8484
EnsureAllResourcesArePresent(loader);
8585
#endif
@@ -90,15 +90,13 @@ CATCH_FAIL_FAST()
9090
winrt::hstring GetLibraryResourceString(const std::wstring_view key)
9191
try
9292
{
93-
static auto loader{ GetLibraryResourceLoader() };
94-
return loader.GetLocalizedString(key);
93+
return GetLibraryResourceLoader().GetLocalizedString(key);
9594
}
9695
CATCH_FAIL_FAST()
9796

9897
bool HasLibraryResourceWithName(const std::wstring_view key)
9998
try
10099
{
101-
static auto loader{ GetLibraryResourceLoader() };
102-
return loader.HasResourceWithName(key);
100+
return GetLibraryResourceLoader().HasResourceWithName(key);
103101
}
104102
CATCH_FAIL_FAST()

src/cascadia/WinRTUtils/ScopedResourceLoader.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,14 @@ bool ScopedResourceLoader::HasResourceWithName(const std::wstring_view resourceN
4646
{
4747
return _resourceMap.HasKey(resourceName);
4848
}
49+
50+
ScopedResourceLoader::ScopedResourceLoader(winrt::Windows::ApplicationModel::Resources::Core::ResourceMap map, winrt::Windows::ApplicationModel::Resources::Core::ResourceContext context) noexcept :
51+
_resourceMap{ std::move(map) }, _resourceContext{ std::move(context) } {}
52+
53+
ScopedResourceLoader ScopedResourceLoader::WithQualifier(const wil::zwstring_view qualifierName, const wil::zwstring_view qualifierValue) const
54+
{
55+
auto newContext = _resourceContext.Clone();
56+
auto qualifierValues = newContext.QualifierValues();
57+
qualifierValues.Insert(qualifierName, qualifierValue); // mutable!
58+
return ScopedResourceLoader{ _resourceMap, std::move(newContext) };
59+
}

src/cascadia/WinRTUtils/inc/LibraryResources.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ namespace Microsoft::Console::Utils
6767
__pragma(warning(suppress : 26485)); \
6868
__declspec(selectany) extern const wchar_t* g_WinRTUtilsLibraryResourceScope{ (x) };
6969

70+
class ScopedResourceLoader;
71+
72+
const ScopedResourceLoader& GetLibraryResourceLoader();
7073
winrt::hstring GetLibraryResourceString(const std::wstring_view key);
7174
bool HasLibraryResourceWithName(const std::wstring_view key);
7275

src/cascadia/WinRTUtils/inc/ScopedResourceLoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ class ScopedResourceLoader
1111
winrt::hstring GetLocalizedString(const std::wstring_view resourceName) const;
1212
bool HasResourceWithName(const std::wstring_view resourceName) const;
1313

14+
ScopedResourceLoader WithQualifier(const wil::zwstring_view qualifierName, const wil::zwstring_view qualifierValue) const;
15+
1416
private:
17+
ScopedResourceLoader(winrt::Windows::ApplicationModel::Resources::Core::ResourceMap map, winrt::Windows::ApplicationModel::Resources::Core::ResourceContext context) noexcept;
1518
winrt::Windows::ApplicationModel::Resources::Core::ResourceMap _resourceMap;
1619
winrt::Windows::ApplicationModel::Resources::Core::ResourceContext _resourceContext;
1720
};

0 commit comments

Comments
 (0)