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
<PackageReference Include="Blazor.Untranslated" Version="0.1.0" PrivateAssets="all" />
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.
Markup text
<h1>Coming soon</h1>Inside @if and @foreach blocks too.
Text content and expressions
@(open ? "Open now" : "Closed")
@($"Welcome back, {name}")
@("Total: " + count)Each literal, through conditionals, ??, concatenation, interpolation and casts.
Visible attributes
<img alt="Company logo" />
<input placeholder="Your email" />title, alt, placeholder, label, description, text, and the aria-* text attributes.
Component parameters
<Card Title="Latest news" />
<Picker Items="days" Label="Pick a day" />Parameters with those names, such as Title, Label and Text. Generic components included.
Allowed terms
<p>Contoso Pro</p>
<p>Welcome to Contoso</p>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>Numbers, symbols and punctuation are the same in every language.
Code, not text
<a href="/about" class="nav-link">
<script>, <style>, <!-- -->, @* *@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-tooltipor aCaptionparameter. - Turning it off
UNT001is reported in.razorfiles, where#pragmacan't reach it. For a whole project, add it toNoWarnor setdotnet_diagnostic.UNT001.severity = nonein.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.
-
01 · you write
A component
<h1>Coming soon</h1> -
02 · Razor generates
C# for the render tree
AddMarkupContent(0, "<h1>Coming soon</h1>") -
03 · the analyzer reads
Every RenderTreeBuilder call
AddMarkupContent,AddContent,AddAttributeandAddComponentParameter, and the literals they receive. -
04 · you see
The line in your .razor file
Home.razor(4,5): error UNT001
- The
#pragma checksumat the top of each generated file, which names the.razorsource. - Literal values at the call sites, wrapped at most in casts, conditionals, concatenation, interpolation and
RuntimeHelpers.TypeCheck. - The
TypeInferencehelpers that render generic components, whose literals sit at the call site. - The
.razorfiles as additional files, which the analyzer reads to point at the line and column.
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.resxclasses, your own lookup. - Not
.cshtmlviews 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.