LocaleLinkHtmlRewriter Pennington.Localization
Rewrites internal page links to include the current locale prefix. When the user is browsing in a non-default locale, links like /about become /fr/about automatically, so components don't need locale-aware URL helpers.
Skips external links, links already carrying a locale prefix, and paths that look like static assets (file extensions, /_content/, etc.).
Runs after xref resolution (Order 10) but before base-URL rewriting (Order 30), so locale detection and prefixing operate on logical root-relative paths (/about/), not paths already prefixed with the deployment base URL (/preview/about/). BaseUrl is the outermost transport layer and must apply last among URL rewriters.
Properties
Orderint- Sort order within the HTML rewriting pipeline. Rewriters run in ascending
Order. Xref resolution at 10, locale prefixing at 20, base-URL prefixing at 30 — the outside-in order that was previously expressed across separateIResponseProcessors.
Constructors
.ctor
#public LocaleLinkHtmlRewriter(LocalizationOptions localization);Creates the rewriter.
Parameters
localizationLocalizationOptions
Methods
ApplyAsync
#public Task ApplyAsync(IDocument document, HttpContext context);Mutate the shared parsed document. The orchestrator serializes it once after every rewriter has run.
Parameters
documentIDocumentcontextHttpContext
Returns
TaskShouldApply
#public bool ShouldApply(HttpContext context);Cheap gate checked before parsing. Return false to skip both PreParseAsync and ApplyAsync for this response. If every rewriter returns false, the orchestrator skips parsing entirely.
Parameters
contextHttpContext
Returns
boolPennington.Localization.LocaleLinkHtmlRewriter
namespace Pennington.Localization;
/// Rewrites internal page links to include the current locale prefix. When the user is browsing in a non-default locale, links like /about become /fr/about automatically, so components don't need locale-aware URL helpers. Skips external links, links already carrying a locale prefix, and paths that look like static assets (file extensions, /_content/, etc.).Runs after xref resolution (Order 10) but before base-URL rewriting (Order 30), so locale detection and prefixing operate on logical root-relative paths (/about/), not paths already prefixed with the deployment base URL (/preview/about/). BaseUrl is the outermost transport layer and must apply last among URL rewriters.
public class LocaleLinkHtmlRewriter
{
/// Creates the rewriter.
public LocaleLinkHtmlRewriter(LocalizationOptions localization);
/// Mutate the shared parsed document. The orchestrator serializes it once after every rewriter has run.
public Task ApplyAsync(IDocument document, HttpContext context);
/// Sort order within the HTML rewriting pipeline. Rewriters run in ascending Order. Xref resolution at 10, locale prefixing at 20, base-URL prefixing at 30 — the outside-in order that was previously expressed across separate IResponseProcessors.
public int Order => 20;
/// Cheap gate checked before parsing. Return false to skip both PreParseAsync and ApplyAsync for this response. If every rewriter returns false, the orchestrator skips parsing entirely.
public bool ShouldApply(HttpContext context);
}