| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds localization middleware to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Enables PowerShell-style localization using string table files (Messages.psd1). |
| | | 6 | | The middleware resolves the culture once per request and exposes localized strings via |
| | | 7 | | Context.LocalizedStrings (alias: Context.Strings) and the Localizer variable in route runspaces. |
| | | 8 | | .PARAMETER Server |
| | | 9 | | The Kestrun server instance to configure. |
| | | 10 | | .PARAMETER Options |
| | | 11 | | A Kestrun.Localization.KestrunLocalizationOptions instance. Overrides individual parameters. |
| | | 12 | | .PARAMETER DefaultCulture |
| | | 13 | | Default culture used when no match is found. Default is 'en-US'. |
| | | 14 | | .PARAMETER ResourcesBasePath |
| | | 15 | | Base path for localization resources. Default is 'i18n'. |
| | | 16 | | .PARAMETER FileName |
| | | 17 | | Localization file name. Default is 'Messages.psd1'. |
| | | 18 | | .PARAMETER QueryKey |
| | | 19 | | Query string key used to request a culture. Default is 'lang'. |
| | | 20 | | .PARAMETER CookieName |
| | | 21 | | Cookie name used to request a culture. Default is 'lang'. |
| | | 22 | | .PARAMETER DisableAcceptLanguage |
| | | 23 | | Disables Accept-Language header resolution when specified. |
| | | 24 | | .PARAMETER EnableQuery |
| | | 25 | | Enables query string resolution when specified. Default is false. |
| | | 26 | | .PARAMETER EnableCookie |
| | | 27 | | Enables cookie resolution when specified. Default is false. |
| | | 28 | | .PARAMETER SetDefaultThreadCulture |
| | | 29 | | When specified, sets the process-wide default thread culture |
| | | 30 | | (CultureInfo.DefaultThreadCurrentCulture and |
| | | 31 | | CultureInfo.DefaultThreadCurrentUICulture) once during application |
| | | 32 | | startup, using the configured DefaultCulture. |
| | | 33 | | This culture is used as a fallback for newly created threads that do |
| | | 34 | | not have an explicit CurrentCulture set. It does not change the culture |
| | | 35 | | of existing threads or runspaces and does not replace per-request |
| | | 36 | | culture handling. |
| | | 37 | | Default is false. |
| | | 38 | | .PARAMETER PassThru |
| | | 39 | | Returns the server instance for chaining. |
| | | 40 | | .EXAMPLE |
| | | 41 | | Add-KrLocalizationMiddleware -ResourcesBasePath './Assets/i18n' |
| | | 42 | | .EXAMPLE |
| | | 43 | | $opts = [Kestrun.Localization.KestrunLocalizationOptions]::new() |
| | | 44 | | $opts.DefaultCulture = 'en-US' |
| | | 45 | | $opts.ResourcesBasePath = 'i18n' |
| | | 46 | | Add-KrLocalizationMiddleware -Options $opts -PassThru |
| | | 47 | | #> |
| | | 48 | | function Add-KrLocalizationMiddleware { |
| | | 49 | | [KestrunRuntimeApi('Definition')] |
| | | 50 | | [CmdletBinding(DefaultParameterSetName = 'Items')] |
| | | 51 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 52 | | param( |
| | | 53 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 54 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 55 | | |
| | | 56 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 57 | | [Kestrun.Localization.KestrunLocalizationOptions]$Options, |
| | | 58 | | |
| | | 59 | | [Parameter(ParameterSetName = 'Items')] |
| | | 60 | | [ValidateScript({ |
| | 0 | 61 | | try { [void][System.Globalization.CultureInfo]::GetCultureInfo($_); $true } |
| | 0 | 62 | | catch { throw "Invalid culture name '$_'. Use a valid BCP 47 / .NET culture (e.g. en-US, zh-Hans, sr-Lat |
| | | 63 | | })] |
| | | 64 | | [string]$DefaultCulture = 'en-US', |
| | | 65 | | |
| | | 66 | | |
| | | 67 | | [Parameter(ParameterSetName = 'Items')] |
| | | 68 | | [string]$ResourcesBasePath = 'i18n', |
| | | 69 | | |
| | | 70 | | [Parameter(ParameterSetName = 'Items')] |
| | | 71 | | [string]$FileName = 'Messages.psd1', |
| | | 72 | | |
| | | 73 | | [Parameter(ParameterSetName = 'Items')] |
| | | 74 | | [string]$QueryKey = 'lang', |
| | | 75 | | |
| | | 76 | | [Parameter(ParameterSetName = 'Items')] |
| | | 77 | | [string]$CookieName = 'lang', |
| | | 78 | | |
| | | 79 | | [Parameter(ParameterSetName = 'Items')] |
| | | 80 | | [switch]$DisableAcceptLanguage, |
| | | 81 | | |
| | | 82 | | [Parameter(ParameterSetName = 'Items')] |
| | | 83 | | [switch]$EnableQuery, |
| | | 84 | | |
| | | 85 | | [Parameter(ParameterSetName = 'Items')] |
| | | 86 | | [switch]$EnableCookie, |
| | | 87 | | |
| | | 88 | | [Parameter(ParameterSetName = 'Items')] |
| | | 89 | | [switch]$SetDefaultThreadCulture, |
| | | 90 | | |
| | | 91 | | [Parameter()] |
| | | 92 | | [switch]$PassThru |
| | | 93 | | ) |
| | | 94 | | begin { |
| | 0 | 95 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 96 | | } |
| | | 97 | | process { |
| | 0 | 98 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | 0 | 99 | | $Options = [Kestrun.Localization.KestrunLocalizationOptions]::new() |
| | 0 | 100 | | $Options.DefaultCulture = $DefaultCulture |
| | 0 | 101 | | $Options.ResourcesBasePath = (Resolve-KrPath -Path $ResourcesBasePath -KestrunRoot) |
| | 0 | 102 | | $Options.FileName = $FileName |
| | 0 | 103 | | $Options.QueryKey = $QueryKey |
| | 0 | 104 | | $Options.CookieName = $CookieName |
| | | 105 | | |
| | 0 | 106 | | if ($PSBoundParameters.ContainsKey('DisableAcceptLanguage')) { $Options.EnableAcceptLanguage = -not $Disable |
| | 0 | 107 | | if ($PSBoundParameters.ContainsKey('EnableQuery')) { $Options.EnableQuery = $EnableQuery.IsPresent } |
| | 0 | 108 | | if ($PSBoundParameters.ContainsKey('EnableCookie')) { $Options.EnableCookie = $EnableCookie.IsPresent } |
| | 0 | 109 | | if ($PSBoundParameters.ContainsKey('SetDefaultThreadCulture')) { $Options.SetDefaultThreadCulture = $SetDefa |
| | | 110 | | } else { |
| | 0 | 111 | | if ($null -eq $Options) { |
| | 0 | 112 | | throw 'Options cannot be null.' |
| | | 113 | | } |
| | | 114 | | try { |
| | 0 | 115 | | [void][System.Globalization.CultureInfo]::GetCultureInfo($Options.DefaultCulture) |
| | | 116 | | } catch { |
| | 0 | 117 | | throw "Invalid culture name '$($Options.DefaultCulture)'. Use a valid BCP 47 / .NET culture (e.g. en-US, |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | $Server.AddLocalization($Options) | Out-Null |
| | | 122 | | |
| | 0 | 123 | | if ($PassThru.IsPresent) { return $Server } |
| | | 124 | | } |
| | | 125 | | } |