Beskar.CodeGeneration.LanguageGenerator 1.2.1

dotnet add package Beskar.CodeGeneration.LanguageGenerator --version 1.2.1
                    
NuGet\Install-Package Beskar.CodeGeneration.LanguageGenerator -Version 1.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Beskar.CodeGeneration.LanguageGenerator" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Beskar.CodeGeneration.LanguageGenerator" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Beskar.CodeGeneration.LanguageGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Beskar.CodeGeneration.LanguageGenerator --version 1.2.1
                    
#r "nuget: Beskar.CodeGeneration.LanguageGenerator, 1.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Beskar.CodeGeneration.LanguageGenerator@1.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Beskar.CodeGeneration.LanguageGenerator&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Beskar.CodeGeneration.LanguageGenerator&version=1.2.1
                    
Install as a Cake Tool

Beskar.CodeGeneration.LanguageGenerator

A high-performance C# Source Generator for .NET 10 that transforms simple enumerations into a robust, type-safe translation infrastructure. It automatically generates translation keys and a fluent TranslationFacade to streamline localization in your applications.

Key Features

  • 🚀 Zero-Reflection: All keys and mapping logic are generated at compile-time for maximum performance.
  • 🏗️ Fluent Facade: Provides a strongly-typed API (translation.Group.Key) instead of error-prone magic strings.
  • 📦 Multiple Providers: Use the built-in JsonTranslationProvider or implement your own for Databases or Third-party APIs.
  • 🔍 Culture Detection: Support for multiple prioritized language detectors (e.g., SystemCultureDetector).
  • 🛠️ Default Values: Define fallback translations directly in your code using attributes.

Installation

The generator is part of the Beskar.CodeGeneration suite. Ensure your project targets .NET 10 or higher to leverage the latest C# features.

Quick Start

1. Define your Translation Groups

Decorate your enums with [TranslationGroup] and members with [TranslationKey].

[TranslationGroup]
public enum TestGroup
{
   [TranslationKey]
   Test = 1,
   
   [TranslationKey(defaultValue: "Default Greeting")]
   WelcomeMessage = 2,
}

[TranslationGroup(GroupName = "Identity")]
public enum RegisterGroup
{
   [TranslationKey(keyName: "Title")]
   Header = 1,
   
   [TranslationKey]
   Description = 2,
}

2. Configure Dependency Injection

Register the necessary services to enable the translation engine.

var services = new ServiceCollection()
   .AddSingleton<ILanguageDetector, SystemCultureDetector>()
   .AddSingleton<ITranslationProvider, JsonTranslationProvider>()
   .AddSingleton<TranslationFacade>();
   
var provider = services.BuildServiceProvider();

// Initialize the Json Provider (Example)
var jsonProvider = (JsonTranslationProvider)provider.GetRequiredService<ITranslationProvider>();
jsonProvider.Initialize("Translations"); // Path to your .json files
await jsonProvider.PopulateCache(CancellationToken.None);

var translation = provider.GetRequiredService<TranslationFacade>();

3. Usage

Access your translations with full IntelliSense support.

// Accessing generated properties via the Facade
string welcome = translation.TestGroup.WelcomeMessage;
string desc = translation.Identity.Description;

// Accessing raw generated constant keys
string rawKey = LangKey.TestGroup.WelcomeMessage;

Generated Artifacts

In the background, the generator creates partial classes to handle the heavy lifting:

  • LangKey: A static class containing const string definitions for all translation keys, an AllKeys collection, and a GetDefaultValue lookup based on your attributes.
  • TranslationFacade: A sealed class that wraps the ITranslationProvider to provide the fluent access layer. It uses the new C# field keyword for efficient lazy loading of group facades.

Advanced Configuration

Custom Providers

You can create custom providers by inheriting from TranslationBaseProvider. This allows you to fetch translations from a database, a CMS, or an external API.

public class SqlTranslationProvider : TranslationBaseProvider
{
   public override async ValueTask PopulateCache(CancellationToken ct) 
   {
      // Custom logic to load translations from SQL
      // AddToCache("en", data);
   }
}
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.1 110 4/29/2026
1.2.0 114 4/26/2026
1.1.9 100 4/25/2026
1.1.8 103 4/8/2026
1.1.7 105 4/7/2026
1.1.6 116 4/5/2026
1.1.5 102 4/5/2026
1.1.4 112 4/4/2026
1.1.3 104 4/4/2026
1.1.2 107 4/2/2026
1.1.1 113 4/2/2026