Razor Localization
Render localized Razor pages with per-request culture resolution.
Full source
File: pwsh/tutorial/examples/21.2-Razor-Localization.ps1
<#!
Sample: Razor Pages + Localization
File: 21.2-Razor-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 'Razor Localization Demo'
Add-KrEndpoint -Port $Port -IPAddress $IPAddress
Add-KrLocalizationMiddleware -ResourcesBasePath './Assets/i18n' -EnableQuery
Add-KrPowerShellRazorPagesRuntime -RootPath './Assets/Pages-Localization' -PathPrefix '/ui'
Enable-KrConfiguration
Start-KrServer
Step-by-step
- Root:
Initialize-KrRootpins the content root for Razor assets. - Server:
New-KrServerandAdd-KrEndpointconfigure the listener. - Localization:
Add-KrLocalizationMiddlewarereads string tables fromAssets/i18n. - Razor:
Add-KrPowerShellRazorPagesRuntimeserves pages fromAssets/Pages-Localizationat/ui. - Start:
Enable-KrConfigurationcommits config andStart-KrServerruns.
Try it
# Default culture
curl -i http://127.0.0.1:5000/ui
# Query override (Italian)
curl -i "http://127.0.0.1:5000/ui?lang=it-IT"
Troubleshooting
- If
/uireturns a 404, verifyAdd-KrPowerShellRazorPagesRuntimeis enabled and theAssets/Pages-Localizationfolder exists. - If the page does not localize, ensure
Add-KrLocalizationMiddlewareruns before the Razor middleware and thatAssets/i18n/<culture>/Messages.psd1contains the expected keys.
References
- Add-KrLocalizationMiddleware
- Add-KrPowerShellRazorPagesRuntime
- Get-KrLocalizedString
- Initialize-KrRoot
- Enable-KrConfiguration
- Start-KrServer
- Razor Guide
- Localization Guide
Previous / Next
Previous: Localization Next: None