Add localization framework for the GUI (en-US baseline) (#764)

Credit to @jmolinap2 for their work in #643, which was used as inspiration.
This commit is contained in:
Sashank
2026-09-10 17:48:43 +02:00
committed by GitHub
parent 6012b02ea2
commit 32024662f3
62 changed files with 2947 additions and 742 deletions
+49 -5
View File
@@ -1,6 +1,15 @@
<#
.SYNOPSIS
Shows a themed Windows 11-style message box.
.DESCRIPTION
Falls back to a plain native MessageBox if the themed dialog's own schema fails to
localize (a bad %LANG:% marker in MessageBox.xaml, not a missing app-wide translation),
so a broken schema can't take down every error dialog in the app at once.
.OUTPUTS
System.String. One of 'OK', 'Cancel', 'Yes', or 'No', matching the pressed button
in both the themed dialog and the native fallback.
#>
function Show-MessageBox {
param (
@@ -50,6 +59,41 @@ function Show-MessageBox {
# Load XAML from file
$xaml = Get-Content -Path $script:MessageBoxSchema -Raw
try {
$xaml = ConvertTo-LocalizedXaml -Xaml $xaml
}
catch {
# The themed dialog depends on the schema's own %LANG:% markers resolving. If that ever
# breaks (a bad marker added to MessageBoxSchema, not a missing app-wide translation - that
# never reaches this function), fall back to a plain native MessageBox rather than losing
# the ability to show any error dialog at all.
Write-Warning "Falling back to a native message box, themed dialog failed to localize: $($_.Exception.Message)"
# Hide overlay before showing the fallback (only if this dialog was the one that showed it)
if ($overlay -and -not $overlayWasAlreadyVisible) {
try {
$ownerWindow.Dispatcher.Invoke([action]{ $overlay.Visibility = 'Collapsed' })
}
catch { }
}
$nativeButton = switch ($Button) {
'OKCancel' { [System.Windows.MessageBoxButton]::OKCancel }
'YesNo' { [System.Windows.MessageBoxButton]::YesNo }
default { [System.Windows.MessageBoxButton]::OK }
}
$nativeIcon = switch ($Icon) {
'Warning' { [System.Windows.MessageBoxImage]::Warning }
'Error' { [System.Windows.MessageBoxImage]::Error }
'Question' { [System.Windows.MessageBoxImage]::Question }
'Information' { [System.Windows.MessageBoxImage]::Information }
'Success' { [System.Windows.MessageBoxImage]::Information }
default { [System.Windows.MessageBoxImage]::None }
}
$nativeResult = [System.Windows.MessageBox]::Show($Message, $Title, $nativeButton, $nativeIcon)
return [string]$nativeResult
}
$reader = [System.Xml.XmlReader]::Create([System.IO.StringReader]::new($xaml))
try {
$msgWindow = [System.Windows.Markup.XamlReader]::Load($reader)
@@ -121,22 +165,22 @@ function Show-MessageBox {
# Configure buttons - store result in window's Tag property
switch ($Button) {
'OK' {
$button1.Content = 'OK'
$button1.Content = Get-Translation -Key 'MessageBoxOk'
$button1.Add_Click({ $msgWindow.Tag = 'OK'; $msgWindow.Close() })
$button2.Visibility = 'Collapsed'
# Right-align sole button by moving it to column 1
[System.Windows.Controls.Grid]::SetColumn($button1, 1)
}
'OKCancel' {
$button1.Content = 'OK'
$button2.Content = 'Cancel'
$button1.Content = Get-Translation -Key 'MessageBoxOk'
$button2.Content = Get-Translation -Key 'MessageBoxCancel'
$button1.Add_Click({ $msgWindow.Tag = 'OK'; $msgWindow.Close() })
$button2.Add_Click({ $msgWindow.Tag = 'Cancel'; $msgWindow.Close() })
$button2.Visibility = 'Visible'
}
'YesNo' {
$button1.Content = 'Yes'
$button2.Content = 'No'
$button1.Content = Get-Translation -Key 'MessageBoxYes'
$button2.Content = Get-Translation -Key 'MessageBoxNo'
$button1.Add_Click({ $msgWindow.Tag = 'Yes'; $msgWindow.Close() })
$button2.Add_Click({ $msgWindow.Tag = 'No'; $msgWindow.Close() })
$button2.Visibility = 'Visible'