diff --git a/Fido.ps1 b/Fido.ps1 index 9e1d94f..28c6bcd 100644 --- a/Fido.ps1 +++ b/Fido.ps1 @@ -22,43 +22,33 @@ # TODO: # - Add a -NoHide param -# - Add translations -# - Sort Windows 7 downloads # - Display all arch links? # - Icon does not display in taskbar when shell window is reduced # - Validate that download links are from Microsoft servers # - Add Expert mode for Home China and suff? -# Parameters +#region Parameters param( # (Optional) Name of a pipe the download URL should be sent to. # If not provided, a browser window is opened instead. [string]$PipeName, - # (Optional) Name of the perferred locale to use for the UI (e.g. "en-US", "fr-FR") - # If not provided, the current Windows UI locale is used. - [string]$Locale = [System.Globalization.CultureInfo]::CurrentUICulture.Name, + # (Optional) '|' separated UI localization strings. + [string]$Messages, # (Optional) Path to the file that should be used for the UI icon. [string]$Icon, # (Optional) The title to display on the application window [string]$AppTitle = "Fido - Windows Retail ISO Downloader" ) +#endregion +#region Testing $Debug = $False $Testing = $False -if ($Testing) { - $Locale = "fr-CA" -} - -$TestLangs = '{"languages":[ - { "language":"English", "text":"Anglais", "id":"100" }, - { "language":"English (International)", "text":"Anglais (International)", "id":"101" }, - { "language":"French", "text":"Français", "id":"102" }, - { "language":"French (Canadian)", "text":"Français (Canadien)", "id":"103" } -]}' +#endregion Write-Host Please Wait... -# Custom Assembly Types +#region Assembly Types $code = @" [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)] internal static extern IntPtr LoadLibrary(string lpLibFileName); @@ -101,8 +91,9 @@ Add-Type -MemberDefinition $code -Namespace Gui -UsingNamespace "System.IO", "Sy Add-Type -AssemblyName PresentationFramework # Hide the powershell window: https://stackoverflow.com/a/27992426/1069307 $null = [Gui.Utils]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0) +#endregion -# Data +#region Data # TODO: Fetch this as JSON data? $WindowsVersions = @( @( @@ -262,89 +253,61 @@ $WindowsVersions = @( ) ) ) +#endregion -# Translated messages. Empty string means same as English -# TODO: Fetch this as JSON data? -$Translations = @( - @( - "en-US" - "Version" - "Release" - "Edition" - "Language" - "Arch" - "Download" - "Confirm" - "Close" - "Cancel" - "Error" - ), - @( - "fr-FR" - "" - "" - "Édition" - "Langue de produit" - "" - "Télécharger" - "Confirmer" - "Fermer" - "Abandonner" - "Erreur" - ) -) - -# Functions +#region Functions function Select-Language([int]$ArrayIndex, [string]$LangName) { - if (($Locale.StartsWith("ar") -and $LangName -like "*Arabic*") -or ` - ($Locale -eq "pt-BR" -and $LangName -like "*Brazil*") -or ` - ($Locale.StartsWith("ar") -and $LangName -like "*Bulgar*") -or ` - ($Locale -eq "zh-CN" -and $LangName -like "*Chinese*" -and $LangName -like "*simp*") -or ` - ($Locale -eq "zh-TW" -and $LangName -like "*Chinese*" -and $LangName -like "*trad*") -or ` - ($Locale.StartsWith("hr") -and $LangName -like "*Croat*") -or ` - ($Locale.StartsWith("cz") -and $LangName -like "*Czech*") -or ` - ($Locale.StartsWith("da") -and $LangName -like "*Danish*") -or ` - ($Locale.StartsWith("nl") -and $LangName -like "*Dutch*") -or ` - ($Locale -eq "en-US" -and $LangName -eq "English") -or ` - ($Locale.StartsWith("en") -and $LangName -like "*English*" -and $LangName -like "*inter*") -or ` - ($Locale.StartsWith("et") -and $LangName -like "*Eston*") -or ` - ($Locale.StartsWith("fi") -and $LangName -like "*Finn*") -or ` - ($Locale -eq "fr-CA" -and $LangName -like "*French*" -and $LangName -like "*Canad*") -or ` - ($Locale.StartsWith("fr") -and $LangName -eq "French") -or ` - ($Locale.StartsWith("de") -and $LangName -like "*German*") -or ` - ($Locale.StartsWith("el") -and $LangName -like "*Greek*") -or ` - ($Locale.StartsWith("he") -and $LangName -like "*Hebrew*") -or ` - ($Locale.StartsWith("hu") -and $LangName -like "*Hungar*") -or ` - ($Locale.StartsWith("id") -and $LangName -like "*Indones*") -or ` - ($Locale.StartsWith("it") -and $LangName -like "*Italia*") -or ` - ($Locale.StartsWith("ja") -and $LangName -like "*Japan*") -or ` - ($Locale.StartsWith("ko") -and $LangName -like "*Korea*") -or ` - ($Locale.StartsWith("lv") -and $LangName -like "*Latvia*") -or ` - ($Locale.StartsWith("lt") -and $LangName -like "*Lithuania*") -or ` - ($Locale.StartsWith("ms") -and $LangName -like "*Malay*") -or ` - ($Locale.StartsWith("nb") -and $LangName -like "*Norw*") -or ` - ($Locale.StartsWith("fa") -and $LangName -like "*Persia*") -or ` - ($Locale.StartsWith("pl") -and $LangName -like "*Polish*") -or ` - ($Locale -eq "pt-PT" -and $LangName -eq "Portuguese") -or ` - ($Locale.StartsWith("ro") -and $LangName -like "*Romania*") -or ` - ($Locale.StartsWith("ru") -and $LangName -like "*Russia*") -or ` - ($Locale.StartsWith("sr") -and $LangName -like "*Serbia*") -or ` - ($Locale.StartsWith("sk") -and $LangName -like "*Slovak*") -or ` - ($Locale.StartsWith("sl") -and $LangName -like "*Slovenia*") -or ` - ($Locale -eq "es-ES" -and $LangName -eq "Spanish") -or ` - ($Locale.StartsWith("es") -and $Locale -ne "es-ES" -and $LangName -like "*Spanish*") -or ` - ($Locale.StartsWith("sv") -and $LangName -like "*Swed*") -or ` - ($Locale.StartsWith("th") -and $LangName -like "*Thai*") -or ` - ($Locale.StartsWith("tr") -and $LangName -like "*Turk*") -or ` - ($Locale.StartsWith("uk") -and $LangName -like "*Ukrain*") -or ` - ($Locale.StartsWith("vi") -and $LangName -like "*Vietnam*")) { + # Use the system locale to try select the most appropriate language + [string]$SysLocale = [System.Globalization.CultureInfo]::CurrentUICulture.Name + if (($SysLocale.StartsWith("ar") -and $LangName -like "*Arabic*") -or ` + ($SysLocale -eq "pt-BR" -and $LangName -like "*Brazil*") -or ` + ($SysLocale.StartsWith("ar") -and $LangName -like "*Bulgar*") -or ` + ($SysLocale -eq "zh-CN" -and $LangName -like "*Chinese*" -and $LangName -like "*simp*") -or ` + ($SysLocale -eq "zh-TW" -and $LangName -like "*Chinese*" -and $LangName -like "*trad*") -or ` + ($SysLocale.StartsWith("hr") -and $LangName -like "*Croat*") -or ` + ($SysLocale.StartsWith("cz") -and $LangName -like "*Czech*") -or ` + ($SysLocale.StartsWith("da") -and $LangName -like "*Danish*") -or ` + ($SysLocale.StartsWith("nl") -and $LangName -like "*Dutch*") -or ` + ($SysLocale -eq "en-US" -and $LangName -eq "English") -or ` + ($SysLocale.StartsWith("en") -and $LangName -like "*English*" -and $LangName -like "*inter*") -or ` + ($SysLocale.StartsWith("et") -and $LangName -like "*Eston*") -or ` + ($SysLocale.StartsWith("fi") -and $LangName -like "*Finn*") -or ` + ($SysLocale -eq "fr-CA" -and $LangName -like "*French*" -and $LangName -like "*Canad*") -or ` + ($SysLocale.StartsWith("fr") -and $LangName -eq "French") -or ` + ($SysLocale.StartsWith("de") -and $LangName -like "*German*") -or ` + ($SysLocale.StartsWith("el") -and $LangName -like "*Greek*") -or ` + ($SysLocale.StartsWith("he") -and $LangName -like "*Hebrew*") -or ` + ($SysLocale.StartsWith("hu") -and $LangName -like "*Hungar*") -or ` + ($SysLocale.StartsWith("id") -and $LangName -like "*Indones*") -or ` + ($SysLocale.StartsWith("it") -and $LangName -like "*Italia*") -or ` + ($SysLocale.StartsWith("ja") -and $LangName -like "*Japan*") -or ` + ($SysLocale.StartsWith("ko") -and $LangName -like "*Korea*") -or ` + ($SysLocale.StartsWith("lv") -and $LangName -like "*Latvia*") -or ` + ($SysLocale.StartsWith("lt") -and $LangName -like "*Lithuania*") -or ` + ($SysLocale.StartsWith("ms") -and $LangName -like "*Malay*") -or ` + ($SysLocale.StartsWith("nb") -and $LangName -like "*Norw*") -or ` + ($SysLocale.StartsWith("fa") -and $LangName -like "*Persia*") -or ` + ($SysLocale.StartsWith("pl") -and $LangName -like "*Polish*") -or ` + ($SysLocale -eq "pt-PT" -and $LangName -eq "Portuguese") -or ` + ($SysLocale.StartsWith("ro") -and $LangName -like "*Romania*") -or ` + ($SysLocale.StartsWith("ru") -and $LangName -like "*Russia*") -or ` + ($SysLocale.StartsWith("sr") -and $LangName -like "*Serbia*") -or ` + ($SysLocale.StartsWith("sk") -and $LangName -like "*Slovak*") -or ` + ($SysLocale.StartsWith("sl") -and $LangName -like "*Slovenia*") -or ` + ($SysLocale -eq "es-ES" -and $LangName -eq "Spanish") -or ` + ($SysLocale.StartsWith("es") -and $Locale -ne "es-ES" -and $LangName -like "*Spanish*") -or ` + ($SysLocale.StartsWith("sv") -and $LangName -like "*Swed*") -or ` + ($SysLocale.StartsWith("th") -and $LangName -like "*Thai*") -or ` + ($SysLocale.StartsWith("tr") -and $LangName -like "*Turk*") -or ` + ($SysLocale.StartsWith("uk") -and $LangName -like "*Ukrain*") -or ` + ($SysLocale.StartsWith("vi") -and $LangName -like "*Vietnam*")) { return $ArrayIndex } return -1 } -function Add-Title([string]$Name) +function Add-Entry([int]$pos, [string]$Name, [array]$Items, [string]$DisplayName) { $Title = New-Object System.Windows.Controls.TextBlock $Title.FontSize = $WindowsVersionTitle.FontSize @@ -353,14 +316,11 @@ function Add-Title([string]$Name) $Title.HorizontalAlignment = "Left" $Title.VerticalAlignment = "Top" $Margin = $WindowsVersionTitle.Margin - $Margin.Top += $script:Stage * $script:dh + $Margin.Top += $pos * $dh $Title.Margin = $Margin - $Title.Text = $Name - return $Title -} + $Title.Text = Get-Translation($Name) + $XMLGrid.Children.Insert(2 * $Stage + 2, $Title) -function Add-Combo -{ $Combo = New-Object System.Windows.Controls.ComboBox $Combo.FontSize = $WindowsVersion.FontSize $Combo.Height = $WindowsVersion.Height; @@ -368,9 +328,27 @@ function Add-Combo $Combo.HorizontalAlignment = "Left" $Combo.VerticalAlignment = "Top" $Margin = $WindowsVersion.Margin - $Margin.Top += $script:Stage * $script:dh + $Margin.Top += $pos * $script:dh $Combo.Margin = $Margin $Combo.SelectedIndex = 0 + if ($Items) { + $Combo.ItemsSource = $Items + if ($DisplayName) { + $Combo.DisplayMemberPath = $DisplayName + } else { + $Combo.DisplayMemberPath = $Name + } + } + $XMLGrid.Children.Insert(2 * $Stage + 3, $Combo) + + $XMLForm.Height += $dh; + $Margin = $Confirm.Margin + $Margin.Top += $dh + $Confirm.Margin = $Margin + $Margin = $Back.Margin + $Margin.Top += $dh + $Back.Margin = $Margin + return $Combo } @@ -420,17 +398,20 @@ function ConvertTo-ImageSource # Translate a message string function Get-Translation([string]$Text) { - if (-not $Translations[0].Contains($Text)) { + if (-not $English.Contains($Text)) { Write-Host "Error: '$Text' is not a translatable string" return "(Untranslated)" } - foreach($Translation in $Translations) { - if ($Translation[0].StartsWith($ShortLocale)) { - for ($i = 1; $i -lt $Translation.Length; $i++) { - if ($Translations[0][$i] -eq $Text) { - if ($Translation[$i]) { - return $Translation[$i] - } + if ($Localized) { + if ($Localized.Length -ne $English.Length) { + Write-Host "Error: '$Text' is not a translatable string" + } + for ($i = 0; $i -lt $English.Length; $i++) { + if ($English[$i] -eq $Text) { + if ($Localized[$i]) { + return $Localized[$i] + } else { + return $Text } } } @@ -450,40 +431,55 @@ function Error([string]$ErrorMessage) $script:Stage = -1 $Confirm.IsEnabled = $True } +#endregion -# XAML Form +#region Form [xml]$Form = @" - + +