| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Returns a localized string for the current request culture. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Looks up a key in the request-local string table exposed by Kestrun localization middleware. |
| | | 6 | | The current culture is determined by middleware and is not passed explicitly. |
| | | 7 | | .PARAMETER Key |
| | | 8 | | The localization key to retrieve. |
| | | 9 | | .PARAMETER Default |
| | | 10 | | Optional default value to return when the key is not present. |
| | | 11 | | .EXAMPLE |
| | | 12 | | Get-KrLocalizedString -Key 'Labels.Save' |
| | | 13 | | .EXAMPLE |
| | | 14 | | Get-KrLocalizedString -Key 'Labels.Save' -Default 'Save' |
| | | 15 | | .OUTPUTS |
| | | 16 | | System.String |
| | | 17 | | #> |
| | | 18 | | function Get-KrLocalizedString { |
| | | 19 | | [KestrunRuntimeApi('Route')] |
| | | 20 | | [CmdletBinding()] |
| | | 21 | | [OutputType([string])] |
| | | 22 | | param( |
| | | 23 | | [Parameter(Mandatory)] |
| | | 24 | | [string]$Key, |
| | | 25 | | |
| | | 26 | | [Parameter()] |
| | | 27 | | [string]$Default |
| | | 28 | | ) |
| | | 29 | | |
| | 0 | 30 | | $strings = $null |
| | 0 | 31 | | if ($null -ne $Context) { |
| | 0 | 32 | | $localizedProp = $Context.PSObject.Properties['LocalizedStrings'] |
| | 0 | 33 | | if ($null -ne $localizedProp) { |
| | 0 | 34 | | $strings = $localizedProp.Value |
| | 0 | 35 | | } elseif ($Context.PSObject.Properties['Strings']) { |
| | 0 | 36 | | $strings = $Context.Strings |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | if ($null -eq $strings) { |
| | 0 | 41 | | if ($PSBoundParameters.ContainsKey('Default')) { |
| | 0 | 42 | | return $Default |
| | | 43 | | } |
| | 0 | 44 | | return $null |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | if ($strings.ContainsKey($Key)) { |
| | 0 | 48 | | return $strings[$Key] |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | if ($PSBoundParameters.ContainsKey('Default')) { |
| | 0 | 52 | | return $Default |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | return $null |
| | | 56 | | } |