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

  1. Root: Initialize-KrRoot pins the content root for Razor assets.
  2. Server: New-KrServer and Add-KrEndpoint configure the listener.
  3. Localization: Add-KrLocalizationMiddleware reads string tables from Assets/i18n.
  4. Razor: Add-KrPowerShellRazorPagesRuntime serves pages from Assets/Pages-Localization at /ui.
  5. Start: Enable-KrConfiguration commits config and Start-KrServer runs.

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 /ui returns a 404, verify Add-KrPowerShellRazorPagesRuntime is enabled and the Assets/Pages-Localization folder exists.
  • If the page does not localize, ensure Add-KrLocalizationMiddleware runs before the Razor middleware and that Assets/i18n/<culture>/Messages.psd1 contains the expected keys.

References


Previous / Next

Previous: Localization Next: None