Telegrator.Localized 1.17.3

dotnet add package Telegrator.Localized --version 1.17.3
                    
NuGet\Install-Package Telegrator.Localized -Version 1.17.3
                    
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="Telegrator.Localized" Version="1.17.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Telegrator.Localized" Version="1.17.3" />
                    
Directory.Packages.props
<PackageReference Include="Telegrator.Localized" />
                    
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 Telegrator.Localized --version 1.17.3
                    
#r "nuget: Telegrator.Localized, 1.17.3"
                    
#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 Telegrator.Localized@1.17.3
                    
#: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=Telegrator.Localized&version=1.17.3
                    
Install as a Cake Addin
#tool nuget:?package=Telegrator.Localized&version=1.17.3
                    
Install as a Cake Tool

Telegrator.Localized

The Telegrator.Localized package provides seamless localization support for your Telegrator bots. It leverages the standard .NET IStringLocalizer ecosystem, allowing you to easily build bots that communicate with users in their preferred language.

Features

  • Standard .NET Localization: Works out-of-the-box with .resx files and standard .NET localization services.
  • Dynamic Culture Resolution: Automatically detects user language from Telegram updates or allows custom resolution logic (e.g., from a database).
  • Aspect-Oriented: Uses Telegrator's aspect system (LocalizedAspect) to set the thread's culture before handler execution without cluttering your business logic.
  • Easy Templating: Pass dynamic arguments to your localized strings effortlessly.

Installation

Install the package via NuGet:

dotnet add package Telegrator.Localized

Quick Start

1. Create Resource Files

Create standard .resx files in your project (e.g., inside a Resources folder):

  • BotMessages.en.resx: Welcome"Welcome to our bot!"
  • BotMessages.ru.resx: Welcome"Добро пожаловать в нашего бота!"

2. Register Services

Add localization services and Telegrator to your DI container:

using Telegrator.Localized;

var builder = Host.CreateApplicationBuilder(args);

// Add standard .NET localization
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");

// Register culture resolver and the localized aspect
builder.Services.AddSingleton<ICultureResolver, DefaultCultureResolver>();

// Add Telegrator
builder.AddTelegrator();

var host = builder.Build();
await host.RunAsync();

3. Using in Handlers

Implement the ILocalizedHandler<T> interface (e.g., ILocalizedMessageHandler) to gain access to the LocalizationProvider.

[CommandHandler]
[CommandAllias("start")]
[BeforeExecution<LocalizedAspect>] // Ensure culture is set before execution
public class StartHandler : CommandHandler, ILocalizedMessageHandler
{
    public IStringLocalizer LocalizationProvider { get; }

    public StartHandler(IStringLocalizer<StartHandler> localizer)
    {
        LocalizationProvider = localizer;
    }

    public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
    {
        // 1. Send localized response immediately
        await this.ResponseLocalized("Welcome");
        
        // 2. Get localized string for custom use (e.g., buttons, logging)
        string welcomeText = this.Localize("Welcome");
        
        return Ok;
    }
}

Localized Templating

Often you need to include dynamic values (like usernames or counts) in your localized messages.

Using Arguments in Resources

In your .resx file, define strings with standard C# format placeholders:

  • Greeting"Hello, {0}! Welcome back."

Passing Arguments in Code

The extension methods accept optional arguments:

public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
{
    string name = container.HandlingUpdate.Message!.From!.FirstName;
    
    // Result: "Hello, John! Welcome back." (in user's language)
    await this.ResponseLocalized("Greeting", name);
    
    return Ok;
}

Culture Resolvers

By default, Telegrator determines the user's language from the LanguageCode field in the Telegram update via the DefaultCultureResolver. However, you can implement a custom resolver to load language preferences from a database.

Implementing a Custom Resolver

Implement ICultureResolver to override the default behavior:

public class DatabaseCultureResolver : ICultureResolver
{
    private readonly IUserRepository _userRepository;

    public DatabaseCultureResolver(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public async Task<CultureInfo> ResolveAsync(IHandlerContainer container)
    {
        var userId = container.HandlingUpdate.GetSenderId();
        var preferredLanguage = await _userRepository.GetLanguageAsync(userId);
        
        return new CultureInfo(preferredLanguage ?? "en");
    }
}

Registering the Resolver

Replace the default resolver in your DI container:

builder.Services.AddSingleton<ICultureResolver, DatabaseCultureResolver>();

Limitations

  • Fallback: If the user's language is not supported, it falls back to your application's default culture.
  • Static Content: Localization only works for messages sent by the bot, not for built-in Telegram UI elements (unless you use BotFather to translate them).
Product 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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.17.3 35 5/22/2026