-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathconsole_unpackaged_app.cpp
More file actions
96 lines (85 loc) · 4.74 KB
/
console_unpackaged_app.cpp
File metadata and controls
96 lines (85 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#include <windows.h>
#include <wil/resource.h>
#include <iostream>
#include "winrt\Windows.Foundation.h"
#include "winrt\Windows.Foundation.Collections.h"
#include "winrt\Microsoft.Windows.ApplicationModel.Resources.h"
#include "winrt\Microsoft.Windows.Globalization.h"
#include <MddBootstrap.h>
using namespace winrt;
using namespace winrt::Microsoft::Windows::ApplicationModel::Resources;
using namespace winrt::Microsoft::Windows::Globalization;
int wmain(int argc, wchar_t* argv[])
{
// Print usage help.
if ((argc < 2) || (_wcsicmp(argv[1], L"--help") == 0) || (_wcsicmp(argv[1], L"-?") == 0))
{
std::wcout << "Usage: console_unpackaged_app.exe [options] [mode]\n"
"options:\n"
" -?, --help = Display help\n"
"mode:\n"
" Mode for app run. Can be...\n"
" Default or default\n"
" Override or override\n"
" Fallback or fallback\n"
" PrimaryLanguageOverride or primarylanguageoverride\n"
" Default and override retrieve a sample string from string resource files.\n"
" For the override case, this sample uses the German language.\n"
" Fallback corresponds to the resource-not-found case, where we fallback to a legacy resource loader.\n"
" PrimaryLanguageOverride uses ApplicationLanguages.PrimaryLanguageOverride property to override the language to German.\n"
"\n"
"Examples:\n"
" Get the sample string for the default resource context\n"
" console_unpackaged_app.exe default\n"
" Get the sample string for the override resource context (sample uses the German language for the override context)\n"
" console_unpackaged_app.exe override\n"
" Get the sample string for the resource-not-found fallback case\n"
" console_unpackaged_app.exe fallback\n"
" Get the sample string for the language overriden with ApplicationLanguages.PrimaryLanguageOverride (sample uses the German language)\n"
" console_unpackaged_app.exe primarylanguageoverride\n";
return 1;
}
// Required for C++/WinRT. This call associates this thread with an apartment and initializes COM runtime.
init_apartment();
// Create a resource manager using the resources index (PRI file) generated during build.
auto factory = winrt::get_activation_factory<ResourceManager, IResourceManagerFactory>();
ResourceManager manager = factory.CreateInstance(L"console_unpackaged_app.pri");
manager.ResourceNotFound([](ResourceManager const&, ResourceNotFoundEventArgs const& args)
{
// There could be a resource in a legacy resource file that we retrieve using the corresponding legacy resource loader. For example, the C#
// MRT Core sample apps in this repo use the .NET resource loader as their fallback. Instead, we just hardcode a resource value here.
if (args.Name() == L"Resources/LegacyString")
{
auto candidate = ResourceCandidate(ResourceCandidateKind::String, L"Legacy Sample");
args.SetResolvedCandidate(candidate);
}
});
if ((_wcsicmp(argv[1], L"Default") == 0) || (_wcsicmp(argv[1], L"default") == 0))
{
std::wcout << manager.MainResourceMap().GetValue(L"Resources/SampleString").ValueAsString().c_str() << std::endl;
}
else if ((_wcsicmp(argv[1], L"Override") == 0) || _wcsicmp(argv[1], L"override") == 0)
{
// Create a custom resource context. Set the language to German.
ResourceContext overrideResourceContext = manager.CreateResourceContext();
overrideResourceContext.QualifierValues().Insert(L"Language", L"de-DE");
// Use the custom resource context for the resource lookup.
std::wcout << manager.MainResourceMap().GetValue(L"Resources/SampleString", overrideResourceContext).ValueAsString().c_str() << std::endl;
}
else if ((_wcsicmp(argv[1], L"Fallback") == 0) || (_wcsicmp(argv[1], L"fallback") == 0))
{
std::wcout << manager.MainResourceMap().GetValue(L"Resources/LegacyString").ValueAsString().c_str() << std::endl;
}
else if ((_wcsicmp(argv[1], L"PrimaryLanguageOverride") == 0) || (_wcsicmp(argv[1], L"primarylanguageoverride") == 0))
{
ApplicationLanguages::PrimaryLanguageOverride(L"de-DE");
std::wcout << manager.MainResourceMap().GetValue(L"Resources/SampleString").ValueAsString().c_str() << std::endl;
}
else
{
std::wcout << "Invalid argument!" << std::endl;
}
return 0;
}