Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRTCore] Add PrimaryLanguageOverride samples #358

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -8,11 +8,13 @@
#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[])
{
Expand All @@ -27,18 +29,22 @@ int wmain(int argc, wchar_t* argv[])
" 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";
return 1;
" 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.
Expand Down Expand Up @@ -76,6 +82,11 @@ int wmain(int argc, wchar_t* argv[])
{
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Application
x:Class="PrimaryLanguageOverride.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PrimaryLanguageOverride">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other app resources here -->
</ResourceDictionary>
</Application.Resources>
</Application>
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Shapes;
using Microsoft.Windows.ApplicationModel.Resources;
using Microsoft.Windows.Globalization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace PrimaryLanguageOverride
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
// Set the primary language override to German (Germany)
// This will cause the app to use the German (Germany) resources
// Both resources loaded manually with ResourceLoader and XAML resources fetched with x:Uid will be in German (Germany)
ApplicationLanguages.PrimaryLanguageOverride = "de-DE";

this.InitializeComponent();
}

/// <summary>
/// Invoked when the application is launched.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_resourceLoader = new ResourceLoader();

m_window = new MainWindow(m_resourceLoader);
m_window.Activate();

// Get the Window's HWND
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(m_window);

m_window.Title = "MRT Core PrimaryLanguageOverride sample";
m_window.Activate();

// The Window object doesn't have Width and Height properties in WInUI 3 Desktop yet.
// To set the Width and Height, you can use the Win32 API SetWindowPos.
// Note, you should apply the DPI scale factor if you are thinking of DPI instead of pixels.
SetWindowSize(hwnd, 350, 300);
}
private void SetWindowSize(IntPtr hwnd, int width, int height)
{
var dpi = GetDpiForWindow(hwnd);
float scalingFactor = (float)dpi / 96;
width = (int)(width * scalingFactor);
height = (int)(height * scalingFactor);

SetWindowPos(hwnd, System.IntPtr.Zero, 0, 0, width, height, 2);
}

[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

[DllImport("user32.dll", SetLastError = true)]
static extern uint GetDpiForWindow(IntPtr hWnd);

private ResourceLoader m_resourceLoader;
private Window m_window;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="PrimaryLanguageOverride.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PrimaryLanguageOverride"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top">
<Button x:Uid="SampleButton" Click="SampleButton_Click" Width="300" Margin="20,20,0,0">PrimaryLanguageOverride sample</Button>
<TextBox x:Name="output" Width="300" Margin="20,20,0,0" Text="" />
</StackPanel>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.Windows.ApplicationModel.Resources;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace PrimaryLanguageOverride
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public MainWindow(ResourceLoader resourceLoader)
{
m_resourceLoader = resourceLoader;

this.InitializeComponent();
}

private void SampleButton_Click(object sender, RoutedEventArgs e)
{
var resourceString = m_resourceLoader.GetString("SampleString");
output.Text = resourceString;
}

private ResourceLoader m_resourceLoader;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>PrimaryLanguageOverride</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;ARM64</Platforms>
<RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &gt;= 8">win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &lt; 8">win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<PublishProfile>win-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>
<WindowsSdkPackageVersion>10.0.19041.35-preview</WindowsSdkPackageVersion>
<WindowsPackageType>None</WindowsPackageType>
</PropertyGroup>

<ItemGroup>
<Content Include="Assets\SplashScreen.scale-200.png" />
<Content Include="Assets\LockScreenLogo.scale-200.png" />
<Content Include="Assets\Square150x150Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
<Content Include="Assets\StoreLogo.png" />
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.20240605-1229-Dev" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>

<!--
Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
Tools extension to be activated for this project even if the Windows App SDK Nuget
package has not yet been restored.
-->
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>

<!--
Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
Explorer "Package and Publish" context menu entry to be enabled for this project even if
the Windows App SDK Nuget package has not yet been restored.
-->
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34928.147
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrimaryLanguageOverride", "PrimaryLanguageOverride.csproj", "{A7EE0A53-508D-4350-A184-A08F82DE050E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|ARM64 = Release|ARM64
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Debug|ARM64.ActiveCfg = Debug|ARM64
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Debug|ARM64.Build.0 = Debug|ARM64
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Debug|ARM64.Deploy.0 = Debug|ARM64
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Debug|x64.ActiveCfg = Debug|x64
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Debug|x64.Build.0 = Debug|x64
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Debug|x64.Deploy.0 = Debug|x64
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Debug|x86.ActiveCfg = Debug|x86
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Debug|x86.Build.0 = Debug|x86
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Debug|x86.Deploy.0 = Debug|x86
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Release|ARM64.ActiveCfg = Release|ARM64
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Release|ARM64.Build.0 = Release|ARM64
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Release|ARM64.Deploy.0 = Release|ARM64
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Release|x64.ActiveCfg = Release|x64
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Release|x64.Build.0 = Release|x64
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Release|x64.Deploy.0 = Release|x64
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Release|x86.ActiveCfg = Release|x86
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Release|x86.Build.0 = Release|x86
{A7EE0A53-508D-4350-A184-A08F82DE050E}.Release|x86.Deploy.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {98D0CE59-33E6-43A6-BC91-9333B95F7057}
EndGlobalSection
EndGlobal
Loading