Localization
Serve localized responses with PowerShell-style string tables resolved per request.
Full source
File: pwsh/tutorial/examples/21.1-Localization.ps1
<#!
Sample: Localization (PowerShell string tables)
File: 21.1-Localization.ps1
#>
param(
[int]$Port = 5000,
[IPAddress]$IPAddress = [IPAddress]::Loopback
)
Initialize-KrRoot -Path $PSScriptRoot
New-KrLogger |
Set-KrLoggerLevel -Value Information |
Add-KrSinkConsole |
Register-KrLogger -Name 'console' -SetAsDefault
New-KrServer -Name 'Localization Demo'
Add-KrEndpoint -Port $Port -IPAddress $IPAddress
Add-KrLocalizationMiddleware -ResourcesBasePath './Assets/i18n' -EnableQuery -EnableCookie
Add-KrMapRoute -Verbs Get -Pattern '/hello' -ScriptBlock {
Expand-KrObject -InputObject $Context.Culture -Label 'Current Culture'
$culture = [System.Globalization.CultureInfo]::CurrentCulture
$now = [DateTime]::ParseExact('20260829', 'yyyyMMdd', [System.Globalization.CultureInfo]::InvariantCulture)
$payload = [ordered]@{
culture = $Context.Culture
hello = Get-KrLocalizedString -Key 'Hello' -Default 'Hello'
save = Get-KrLocalizedString -Key 'Labels.Save' -Default 'Save'
cancel = Get-KrLocalizedString -Key 'Labels.Cancel' -Default 'Cancel'
dateSample = $now.ToString('D', $culture)
currencySample = 1234.56.ToString('C', $culture)
calendar = [System.Globalization.CultureInfo]::GetCultureInfo($Context.Culture).Calendar
calendarName = [System.Globalization.CultureInfo]::GetCultureInfo($Context.Culture).Calendar.GetType().Name
}
Write-KrJsonResponse -InputObject $payload -StatusCode 200
}
Add-KrMapRoute -Verbs Get -Pattern '/cultures' -ScriptBlock {
$cultures = Get-KrLocalizationCulture | Sort-Object Name | ForEach-Object { $_.Name }
Write-KrJsonResponse -InputObject @{ cultures = $cultures } -StatusCode 200
}
Enable-KrConfiguration
Start-KrServer
Step-by-step
- Root:
Initialize-KrRootanchors relative paths for the localization folder. - Logging:
New-KrLoggerconfigures console output. - Server:
New-KrServercreates the host andAdd-KrEndpointbinds a listener. - Localization:
Add-KrLocalizationMiddlewareloads string tables fromAssets/i18n. - Route:
Add-KrMapRoutereturns localized JSON usingGet-KrLocalizedString. - Route:
Add-KrMapRouteexposes/culturesusingGet-KrLocalizationCulture. - Start:
Enable-KrConfigurationandStart-KrServerapply and run.
Try it
# Default culture
curl -i http://127.0.0.1:5000/hello
# Query override (Italian)
curl -i "http://127.0.0.1:5000/hello?lang=it-IT"
# List available cultures
curl -i http://127.0.0.1:5000/cultures
Troubleshooting
- If
/helloalways returns the default culture, verify the request includes?lang=it-IT(or anAccept-Languageheader) and thatAdd-KrLocalizationMiddlewarepoints at the correctAssets/i18nfolder. - If
/culturesis empty, check that culture folders exist (for exampleAssets/i18n/en-US/Messages.psd1) and that the server started without localization parsing errors.
References
- Add-KrLocalizationMiddleware
- Add-KrMapRoute
- Get-KrLocalizationCulture
- Get-KrLocalizedString
- Write-KrJsonResponse
- Initialize-KrRoot
- Enable-KrConfiguration
- Start-KrServer
- Localization Guide
Previous / Next
Previous: Tasks Next: Razor Localization