v0.1.0Roslyn analyzer · .NET 8+ · MIT

Untranslated text fails the build

A Roslyn analyzer for Blazor. When a .razor component renders literal text instead of a localized resource, dotnet build stops and points at the line. Locally, in the IDE and in CI, with nothing to set up.

dotnet add package Blazor.Untranslated
Pages/Home.razor 3 literals, 1 resource
1@page "/"
2@inject IStringLocalizer<Home> L
3
4<h1>Coming soon</h1>
5<p>@L["Intro"]</p>
6<img src="logo.svg" alt="Company logo" />
7<Card Title="Latest news" Text="@L["News"]" />
$ dotnet build
Home.razor(4,5): error UNT001: Literal text "Coming soon" must come from a resource
Home.razor(6,26): error UNT001: Literal text "Company logo" must come from a resource
Home.razor(7,14): error UNT001: Literal text "Latest news" must come from a resource
Build failed with 3 error(s)

Sees what Razor renders

The rule reads the C# the Razor compiler generates, so it checks exactly what reaches the page, through @if, @foreach and generic components, instead of guessing from the markup.

Fails every build

UNT001 is an error. It fails dotnet build, the IDE and CI the same way, before anything is pushed. No script, no extra CI step, no Node.

Nothing at runtime

A development dependency: it runs inside the compiler and adds nothing to your output. It doesn't flow to projects that reference yours.

What it catches

Literal text with letters, wherever people see or hear it. Text from a resource passes, and so does anything without letters.

Where Example Result

Markup text

<h1>Coming soon</h1>
UNT001

Inside @if and @foreach blocks too.

Text content and expressions

@(open ? "Open now" : "Closed") @($"Welcome back, {name}") @("Total: " + count)
UNT001

Each literal, through conditionals, ??, concatenation, interpolation and casts.

Visible attributes

<img alt="Company logo" /> <input placeholder="Your email" />
UNT001

title, alt, placeholder, label, description, text, and the aria-* text attributes.

Component parameters

<Card Title="Latest news" /> <Picker Items="days" Label="Pick a day" />
UNT001

Parameters with those names, such as Title, Label and Text. Generic components included.

Allowed terms

<p>Contoso Pro</p> <p>Welcome to Contoso</p>
PassesUNT001

Terms you list are removed before the check, so the brand alone passes and a sentence around it does not.

Resources and no letters

<p>@L["Intro"]</p> <p>42 · 3.14 % →</p>
Passes

Numbers, symbols and punctuation are the same in every language.

Code, not text

<a href="/about" class="nav-link"> <script>, <style>, <!-- -->, @* *@
Ignored

Every other attribute, script and style content, HTML and Razor comments.

Text built in @code or C# and rendered through a variable is not seen; neither are missing translations of existing keys, nor .cshtml views. Keep that text in resources and check it in review.

Two properties. Both optional.

Put them in the project that renders the components. Items are separated by commas: the compiler receives MSBuild properties through an analyzer config file, where ; and # start a comment.

<PropertyGroup>
  <!-- Same in every language -->
  <UntranslatedAllowedTerms>Contoso,Pro</UntranslatedAllowedTerms>
  <!-- Checked besides the defaults -->
  <UntranslatedTextAttributes>data-tooltip,Caption</UntranslatedTextAttributes>
</PropertyGroup>
UntranslatedAllowedTerms
Text that stays the same in every language, such as brand and plan names. A term is removed from the text before the check, longest first.
UntranslatedTextAttributes
Attribute and component parameter names to check besides the defaults, compared without case: your own data-tooltip or a Caption parameter.
Turning it off
UNT001 is reported in .razor files, where #pragma can't reach it. For a whole project, add it to NoWarn or set dotnet_diagnostic.UNT001.severity = none in .editorconfig.

How it works

By the time the analyzer runs, the Razor compiler has already split markup from C#. The rule only has to read the calls that build the page.

  1. 01 · you write

    A component

    <h1>Coming soon</h1>
  2. 02 · Razor generates

    C# for the render tree

    AddMarkupContent(0, "<h1>Coming soon</h1>")
  3. 03 · the analyzer reads

    Every RenderTreeBuilder call

    AddMarkupContent, AddContent, AddAttribute and AddComponentParameter, and the literals they receive.

  4. 04 · you see

    The line in your .razor file

    Home.razor(4,5): error UNT001
  • The #pragma checksum at the top of each generated file, which names the .razor source.
  • Literal values at the call sites, wrapped at most in casts, conditionals, concatenation, interpolation and RuntimeHelpers.TypeCheck.
  • The TypeInference helpers that render generic components, whose literals sit at the call site.
  • The .razor files as additional files, which the analyzer reads to point at the line and column.
.NET 8 SDK .NET 9 SDK .NET 10 SDK .NET 11 SDK A Razor compiler that changed this shape could silence the rule, so CI builds fixture components with the package on every supported SDK and compares each reported line with markers in the fixtures.

Install

Blazor.Untranslated is 0.x: any minor version may change what the rule reports, so pin a version.

NuGet

A development dependency: nothing ships with your app

dotnet add package Blazor.Untranslated

<!-- or, in the .csproj -->
<PackageReference Include="Blazor.Untranslated" Version="0.1.0"
                  PrivateAssets="all" />

Then build. The first literal fails it.

Works with

Any project that compiles .razor components

  • The .NET 8, 9, 10 and 11 SDKs (built against Roslyn 4.8).
  • Blazor Server, WebAssembly, static server rendering and MAUI Blazor Hybrid.
  • Any localization: IStringLocalizer, generated .resx classes, your own lookup.
  • Not .cshtml views or Razor Pages.

No support. Built for my own projects and shared as is: issues and pull requests aren't monitored, and any release may change or break things.