Language Routing (Front-Office URL Prefix)

Overview

Split the front office by URL:

URL Rendered language
/Feedback Site default language (zh-CN in the Demo)
/en-US/Feedback English
/en-US English home page

The prefix decides both the interface language and the data translations: with the Localized Data extension installed, .FromLang() and CMS front-office content follow that language automatically — no change to page routes or query code.

  • Off by default (EnableLangRoute = false); when off the site behaves exactly as before;
  • Works without the localized-data extension: only the interface language switches, data stays as it was;
  • The back office is excluded: /Admin (including the secret-code path), /_blazor / /_framework / /_content and /hubs keep using the cookie / Accept-Language;
  • The URL wins: no prefix means the site default language — neither the cookie nor the browser's Accept-Language is consulted, so the same URL always returns the same content, which is what indexing and shared links need.

For switching the back-office interface language see Localization.

Usage

1. Turn It On

builder.AddEasyAdminBlazor(new EasyAdminBlazorOptions
{
    EnableLocalization = true,
    EnableLangRoute = true,   // front-office URL language prefix
    ...
});

Uncomment this block in Program.cs (needed for EnableLangRoute alone as well):

var option = app.Services.GetService<IOptions<RequestLocalizationOptions>>();
if (option != null)
{
    app.UseRequestLocalization(option.Value);
}

Which languages are supported and which one is the default stay in the one place the site already uses — do not configure them a second time for this feature:

"BootstrapBlazorOptions": {
  "DefaultCultureInfo": "zh-CN",
  "SupportedCultures": [ "zh-CN", "en-US" ]
}

2. Page Routes Stay As They Are

Keep writing @page "/Feedback". Do not create a page copy or an extra route per language — prefixed URLs match the same route automatically.

Links the framework generates carry the prefix; addresses you hardcode yourself need ILangUrl:

Form Prefix What to do
<a asp-page="/News"> Automatic Nothing
Url.Page("/News") / RedirectToPage("/News") Automatic Nothing
<form method="post"> (no action) Automatic Nothing
<a href="/News"> Missing Change to href="@Lang.For("/News")"
<form action="/Contact"> Missing Change to action="@Lang.For("/Contact")"
External https://…, in-page anchor #top — Leave as-is, do not pass through Lang.For
Back-office link /Admin/... No prefix Nothing (For will not add a prefix either)

In Razor Pages, inject once in Pages/_ViewImports.cshtml and it is available site-wide:

@inject EasyAdminBlazor.ILangUrl Lang
<a href="@Lang.For("/News")">News</a>
<a href="@Lang.For("/News", "en-US")">English news</a>
<form method="post" action="@Lang.For("/Contact")"> ... </form>

For() rules:

  • No prefix when the current language is the default (/News), otherwise a prefix (/en-US/News);
  • The two-argument overload builds for a given language — that is what the switcher uses; a code outside the supported list falls back to the default;
  • Accepts site-internal paths only (/News or News, the latter becomes /News);
  • Returns its argument unchanged when the switch is off, so templates can be wired site-wide first and the switch decided later.

4. The Language Switcher

ILangUrl also exposes "which languages" and "which page am I on", so the switcher is the current path in another language:

@if (Lang.Languages.Count > 1)
{
    <div class="flex items-center gap-2">
        @foreach (var culture in Lang.Languages)
        {
            @* Short label from the parent culture's native name: zh-CN → 中文, en-US → English *@
            var label = culture.Parent.Name.Length > 0 ? culture.Parent.NativeName : culture.NativeName;
            var active = string.Equals(culture.Name, Lang.CurrentLanguage, StringComparison.OrdinalIgnoreCase);
            <a class="@(active ? "text-indigo-600" : "text-slate-400")"
               href="@Lang.For(Lang.CurrentPath, culture.Name)">@label</a>
        }
    </div>
}

Languages is an empty collection when the switch is off, so the whole block disappears. CurrentPath includes the query string, so switching /en-US/News?page=2 back to Chinese gives /News?page=2 — the page number survives.

Complete markup to copy from: the Demo's EasyAdminBlazor.Demo/Pages/Shared/_Layout.cshtml, or _LayoutPublic.cshtml in the development skill's corporate template.

URL-to-Language Rules

URL Result
/News Site default language
/en-US/News English
/EN-us/News English (case-insensitive)
/en/News English (the parent code folds to en-US from the supported list; generated links keep /en/News, the visitor's URL is not rewritten)
/en-US English home page
/en-US/ English home page
/fr/News (fr unsupported) Treated as an ordinary path, 404 when nothing matches — no silent fallback
/Admin/Users Back office, language from the cookie, not part of URL routing
/en-US/Admin/Users Also reaches the back office, in English, and the secret-code check still applies (a prefix does not bypass it)
Site deployed under /site /site/en-US/News; For() produces /site/en-US/News too

The default language is decided in this order (the same rule the localized-data extension uses):

  1. BootstrapBlazorOptions.DefaultCultureInfo — an explicit declaration wins, even when it is not in the supported list;
  2. none declared → the interface language at process startup (parent codes fold into the supported list);
  3. still nothing → the first entry of SupportedCultures;
  4. empty list → zh-CN.

Step 2 can produce different results on a different machine or base image, so declare DefaultCultureInfo explicitly on a production site.

What Follows the URL

Does follow:

  • Built-in text of BootstrapBlazor components and the framework (paging, validation messages, buttons);
  • CMS front-office content: title / summary / body / SEO, category names, page elements (requires the localized-data extension);
  • Business data whose query calls .FromLang() (requires the localized-data extension).

Does not follow:

  • Hardcoded template text. The Demo and corporate template navs literally say 「首页 / 服务 / 产品 / 新闻」; those strings never go through localization and stay Chinese on the English page. Translate them by branching on Lang.CurrentLanguage, or move the text into resource files / CMS page elements;
  • Fields brought in by navigation. .FromLang() applies to the main query only; entities loaded through Include / IncludeMany stay in the default language (see Localized Data);
  • The back office. All /Admin paths are excluded, and admin lists always show the entity's own fields.

A front office built from Blazor components (rather than Razor Pages) has not been exercised — run the language switch and a form post yourself before adopting it.

Configuration and API

Member Description
EasyAdminBlazorOptions.EnableLangRoute Master switch, default false
ILangUrl.CurrentLanguage Language code of this request (URL-driven in the front office, current UI language in the back office)
ILangUrl.DefaultLanguage Site default language, rendered without a prefix
ILangUrl.CurrentPath Current path with the language prefix removed, including the query string
ILangUrl.Languages Switchable languages; empty when the switch is off
ILangUrl.For(path) Build a site-internal link in the current language
ILangUrl.For(path, language) Build a site-internal link in a given language

ILangUrl is a Scoped service; take it by constructor injection or @inject.