Dosaic.Extensions.Localization.Generator
1.2.41
dotnet add package Dosaic.Extensions.Localization.Generator --version 1.2.41
NuGet\Install-Package Dosaic.Extensions.Localization.Generator -Version 1.2.41
<PackageReference Include="Dosaic.Extensions.Localization.Generator" Version="1.2.41" />
<PackageVersion Include="Dosaic.Extensions.Localization.Generator" Version="1.2.41" />
<PackageReference Include="Dosaic.Extensions.Localization.Generator" />
paket add Dosaic.Extensions.Localization.Generator --version 1.2.41
#r "nuget: Dosaic.Extensions.Localization.Generator, 1.2.41"
#:package Dosaic.Extensions.Localization.Generator@1.2.41
#addin nuget:?package=Dosaic.Extensions.Localization.Generator&version=1.2.41
#tool nuget:?package=Dosaic.Extensions.Localization.Generator&version=1.2.41
Dosaic.Extensions.Localization.Generator
Dosaic.Extensions.Localization.Generator is a Roslyn IIncrementalGenerator that runs at compile time to discover all types and members annotated with [LocalizedName] and emit a static EntityLabels lookup class into the consuming project. It can also write per-culture JSON translation files to disk during the build.
Installation
dotnet add package Dosaic.Extensions.Localization.Abstractions
dotnet add package Dosaic.Extensions.Localization.Generator
Or as PackageReference entries in your .csproj:
<PackageReference Include="Dosaic.Extensions.Localization.Abstractions" Version="" />
<PackageReference Include="Dosaic.Extensions.Localization.Generator" Version=""
OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
Features
- Incremental generation — uses
IIncrementalGenerator; only reruns when annotated nodes change - Multiple targets — handles
[LocalizedName]on classes, enums, enum members, properties, and fields Locale-typed API — all culture parameters useLocale.En/Locale.Deinstead of magic strings- Configurable default culture —
EntityLabels.DefaultCulturecan be set at runtime; defaults toLocale.En - Fallback — returns the member or type name when no translation is registered for the requested culture
- JSON export — writes a translation JSON file per culture when MSBuild properties are configured
Generated output
Given:
[LocalizedName(en: "Order", de: "Bestellung")]
public class Order
{
[LocalizedName(en: "Order Number", de: "Bestellnummer")]
public string OrderNumber { get; set; }
}
[LocalizedName(en: "Order Status", de: "Bestellstatus")]
public enum OrderStatus
{
[LocalizedName(en: "Active", de: "Aktiv")]
Active
}
The generator emits EntityLabels.g.cs:
// <auto-generated/>
namespace Dosaic.Extensions.Localization
{
public static partial class EntityLabels
{
// Labels keyed by "TypeName" (type-level) or "TypeName.MemberName" (member-level)
private static readonly Dictionary<string, Dictionary<string, string>> Labels = ...;
public static Locale DefaultCulture = Locale.En;
// Raw key lookup
public static string Get(string key);
public static string Get(string key, Locale culture);
// Type-level (class / enum)
public static string Get<T>();
public static string Get<T>(Locale culture);
// Enum member by value
public static string Get(System.Enum value);
public static string Get(System.Enum value, Locale culture);
// Property / field by expression
public static string Get<T>(Expression<Func<T, object>> memberSelector);
public static string Get<T>(Expression<Func<T, object>> memberSelector, Locale culture);
}
}
Key format
| Target | Key |
|---|---|
[LocalizedName] on class Order |
"Order" |
[LocalizedName] on enum OrderStatus |
"OrderStatus" |
[LocalizedName] on OrderStatus.Active |
"OrderStatus.Active" |
[LocalizedName] on Order.OrderNumber |
"Order.OrderNumber" |
JSON export
The generator can write translation files to disk on every build. Declare the output paths as MSBuild properties and expose them to the generator via CompilerVisibleProperty:
<PropertyGroup>
<LocalizationJsonDeOutputPath>$(MSBuildProjectDirectory)\localization.de.json</LocalizationJsonDeOutputPath>
<LocalizationJsonEnOutputPath>$(MSBuildProjectDirectory)\localization.en.json</LocalizationJsonEnOutputPath>
</PropertyGroup>
<ItemGroup>
<CompilerVisibleProperty Include="LocalizationJsonDeOutputPath" />
<CompilerVisibleProperty Include="LocalizationJsonEnOutputPath" />
</ItemGroup>
Either property can be omitted to skip that culture. The file is written on every compilation that touches an annotated node.
Output format
Entries are grouped by type name. Type-level labels use the "$" key within their group; member labels use the member name.
{
"Order": {
"$": "Bestellung",
"OrderNumber": "Bestellnummer"
},
"OrderStatus": {
"$": "Bestellstatus",
"Active": "Aktiv",
"Cancelled": "Storniert"
}
}
How it works
- Attribute discovery —
ForAttributeWithMetadataNamematches[Dosaic.Extensions.Localization.LocalizedNameAttribute]onPropertyDeclarationSyntax,FieldDeclarationSyntax,EnumMemberDeclarationSyntax,ClassDeclarationSyntax,RecordDeclarationSyntax,StructDeclarationSyntax, andEnumDeclarationSyntax - Symbol extraction —
IPropertySymbolandIFieldSymbolproduce member-level entries keyed"TypeName.MemberName";INamedTypeSymbolproduces type-level entries keyed"TypeName" - Source emission —
RegisterSourceOutputcollects all results and emitsEntityLabels.g.csinto the compilation - JSON writing — reads
LocalizationJsonDeOutputPath/LocalizationJsonEnOutputPathfromAnalyzerConfigOptionsProvider.GlobalOptionsand writes grouped JSON files to disk if paths are set
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.