Resources.h (6793B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <winrt/Windows.ApplicationModel.Resources.h> 5 6 #include <string> 7 #include <optional> 8 #include <vector> 9 #include "AppInstallerStrings.h" 10 11 using namespace std::string_view_literals; 12 13 namespace AppInstaller 14 { 15 namespace StringResource 16 { 17 #define WINGET_WIDE_STRINGIFY_HELP(_id_) L ## _id_ 18 #define WINGET_WIDE_STRINGIFY(_id_) WINGET_WIDE_STRINGIFY_HELP(_id_) 19 #define WINGET_DEFINE_RESOURCE_STRINGID(_id_) static constexpr AppInstaller::StringResource::StringId _id_ { WINGET_WIDE_STRINGIFY(#_id_) ## sv } 20 21 // A resource identifier 22 struct StringId : public std::wstring_view 23 { 24 explicit constexpr StringId(std::wstring_view id) : std::wstring_view(id) {} 25 26 // Sets the placeholder values in the resolved string id. 27 // Example: out << myStringId(placeholderVal1, placeholderVal2, ...) 28 template<typename ...T> 29 Utility::LocIndString operator()(T ... args) const; 30 31 // Creates a StringId that represents an empty resource string 32 static StringId Empty(); 33 34 private: 35 // Resolve the string ID to its corresponding localized string 36 // without replacing placeholders. 37 std::string Resolve() const; 38 }; 39 40 inline StringId StringId::Empty() { return StringId{ {} }; } 41 42 // Output resource identifier as localized string. 43 std::ostream& operator<<(std::ostream& out, StringId si); 44 45 // Resource string identifiers. 46 struct String 47 { 48 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableWinGet); 49 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableWingetSettings); 50 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableExperimentalFeatures); 51 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableLocalManifests); 52 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableHashOverride); 53 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableLocalArchiveMalwareScanOverride); 54 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableDefaultSource); 55 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableMSStoreSource); 56 WINGET_DEFINE_RESOURCE_STRINGID(PolicyAdditionalSources); 57 WINGET_DEFINE_RESOURCE_STRINGID(PolicyAllowedSources); 58 WINGET_DEFINE_RESOURCE_STRINGID(PolicySourceAutoUpdateInterval); 59 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableBypassCertificatePinningForMicrosoftStore); 60 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableWindowsPackageManagerCommandLineInterfaces); 61 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableWinGetConfiguration); 62 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableProxyCommandLineOptions); 63 WINGET_DEFINE_RESOURCE_STRINGID(PolicyEnableMcpServer); 64 65 WINGET_DEFINE_RESOURCE_STRINGID(SettingsWarningInvalidFieldFormat); 66 WINGET_DEFINE_RESOURCE_STRINGID(SettingsWarningInvalidFieldValue); 67 WINGET_DEFINE_RESOURCE_STRINGID(SettingsWarningInvalidValueFromPolicy); 68 WINGET_DEFINE_RESOURCE_STRINGID(SettingsWarningLoadedBackupSettings); 69 WINGET_DEFINE_RESOURCE_STRINGID(SettingsWarningParseError); 70 WINGET_DEFINE_RESOURCE_STRINGID(SettingsWarningUsingDefault); 71 72 WINGET_DEFINE_RESOURCE_STRINGID(UnknownErrorCode); 73 }; 74 } 75 76 namespace Resource 77 { 78 // Get an embedded resource from the binary and return as std::string_view. 79 // Resource data is valid as long as the binary is loaded. 80 std::string_view GetResourceAsString(int resourceName, int resourceType); 81 std::string_view GetResourceAsString(PCWSTR resourceName, PCWSTR resourceType); 82 83 // Get an embedded resource from the binary and return as std::pair<BYTE*, size_t>. 84 // Resource data is valid as long as the binary is loaded. 85 std::pair<const BYTE*, size_t> GetResourceAsBytes(int resourceName, int resourceType); 86 std::pair<const BYTE*, size_t> GetResourceAsBytes(PCWSTR resourceName, PCWSTR resourceType); 87 88 // A localized string 89 struct LocString : public Utility::LocIndString 90 { 91 LocString() = default; 92 93 LocString(StringResource::StringId id) : Utility::LocIndString(id()) {} 94 LocString(Utility::LocIndString locIndString) : Utility::LocIndString(std::move(locIndString)) {} 95 96 LocString(const LocString&) = default; 97 LocString& operator=(const LocString&) = default; 98 99 LocString(LocString&&) = default; 100 LocString& operator=(LocString&&) = default; 101 }; 102 } 103 104 namespace StringResource 105 { 106 // Tries to resolve a string, returning a nullopt if it cannot. 107 std::optional<Resource::LocString> TryResolveString(std::wstring_view resKey); 108 } 109 110 namespace details 111 { 112 // List of approved types for output, others are potentially not localized. 113 template <typename T> 114 struct IsApprovedForOutput 115 { 116 static constexpr bool value = std::is_arithmetic<T>::value; 117 }; 118 119 #define WINGET_CREATE_ISAPPROVEDFOROUTPUT_SPECIALIZATION(_t_) \ 120 template <> \ 121 struct IsApprovedForOutput<_t_> \ 122 { \ 123 static constexpr bool value = true; \ 124 } 125 126 // It is assumed that single char values need not be localized, as they are matched 127 // ordinally or they are punctuation / other. 128 WINGET_CREATE_ISAPPROVEDFOROUTPUT_SPECIALIZATION(char); 129 // Localized strings (and from an Id for one for convenience). 130 WINGET_CREATE_ISAPPROVEDFOROUTPUT_SPECIALIZATION(StringResource::StringId); 131 WINGET_CREATE_ISAPPROVEDFOROUTPUT_SPECIALIZATION(Resource::LocString); 132 // Strings explicitly declared as localization independent. 133 WINGET_CREATE_ISAPPROVEDFOROUTPUT_SPECIALIZATION(Utility::LocIndView); 134 WINGET_CREATE_ISAPPROVEDFOROUTPUT_SPECIALIZATION(Utility::LocIndString); 135 // Normalized strings come from user data and should therefore already by localized 136 // by how they are chosen (or there is no localized version). 137 WINGET_CREATE_ISAPPROVEDFOROUTPUT_SPECIALIZATION(Utility::NormalizedString); 138 } 139 140 template<typename ... T> 141 Utility::LocIndString StringResource::StringId::operator()(T ... args) const 142 { 143 static_assert((details::IsApprovedForOutput<std::decay_t<T>>::value && ...), "This type may not be localized, see comment for more information"); 144 return Utility::LocIndString{ Utility::Format(Resolve(), std::forward<T>(args)...) }; 145 } 146 } 147