< Summary - Kestrun — Combined Coverage

Information
Class: Public.Middleware.Add-KrLocalizationMiddleware
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrLocalizationMiddleware.ps1
Tag: Kestrun/Kestrun@ca54e35c77799b76774b3805b6f075cdbc0c5fbe
Line coverage
0%
Covered lines: 0
Uncovered lines: 20
Coverable lines: 20
Total lines: 125
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 01/24/2026 - 19:35:59 Line coverage: 0% (0/20) Total lines: 125 Tag: Kestrun/Kestrun@f59dcba478ea75f69584d696e5f1fb1cfa40aa51

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrLocalizationMiddleware.ps1

#LineLine coverage
 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#>
 48function 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({
 061                try { [void][System.Globalization.CultureInfo]::GetCultureInfo($_); $true }
 062                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 {
 095        $Server = Resolve-KestrunServer -Server $Server
 96    }
 97    process {
 098        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 099            $Options = [Kestrun.Localization.KestrunLocalizationOptions]::new()
 0100            $Options.DefaultCulture = $DefaultCulture
 0101            $Options.ResourcesBasePath = (Resolve-KrPath -Path $ResourcesBasePath -KestrunRoot)
 0102            $Options.FileName = $FileName
 0103            $Options.QueryKey = $QueryKey
 0104            $Options.CookieName = $CookieName
 105
 0106            if ($PSBoundParameters.ContainsKey('DisableAcceptLanguage')) { $Options.EnableAcceptLanguage = -not $Disable
 0107            if ($PSBoundParameters.ContainsKey('EnableQuery')) { $Options.EnableQuery = $EnableQuery.IsPresent }
 0108            if ($PSBoundParameters.ContainsKey('EnableCookie')) { $Options.EnableCookie = $EnableCookie.IsPresent }
 0109            if ($PSBoundParameters.ContainsKey('SetDefaultThreadCulture')) { $Options.SetDefaultThreadCulture = $SetDefa
 110        } else {
 0111            if ($null -eq $Options) {
 0112                throw 'Options cannot be null.'
 113            }
 114            try {
 0115                [void][System.Globalization.CultureInfo]::GetCultureInfo($Options.DefaultCulture)
 116            } catch {
 0117                throw "Invalid culture name '$($Options.DefaultCulture)'. Use a valid BCP 47 / .NET culture (e.g. en-US,
 118            }
 119        }
 120
 0121        $Server.AddLocalization($Options) | Out-Null
 122
 0123        if ($PassThru.IsPresent) { return $Server }
 124    }
 125}

Methods/Properties

Add-KrLocalizationMiddleware()