SMGLocalization.SourceGenerator 1.0.1

dotnet add package SMGLocalization.SourceGenerator --version 1.0.1
                    
NuGet\Install-Package SMGLocalization.SourceGenerator -Version 1.0.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="SMGLocalization.SourceGenerator" Version="1.0.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SMGLocalization.SourceGenerator" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="SMGLocalization.SourceGenerator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 SMGLocalization.SourceGenerator --version 1.0.1
                    
#r "nuget: SMGLocalization.SourceGenerator, 1.0.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 SMGLocalization.SourceGenerator@1.0.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=SMGLocalization.SourceGenerator&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=SMGLocalization.SourceGenerator&version=1.0.1
                    
Install as a Cake Tool

SMG.Localization.Generator

A powerful source generator for strongly-typed localization in .NET applications. Supports nested JSON structures, dependency injection, and real-time language switching with events.

Features

  • Strongly-typed localization with IntelliSense support
  • Nested JSON structure support (Common.App.Name)
  • Dependency Injection integration
  • Language change events for real-time UI updates
  • Blazor and MAUI support
  • Fallback language support
  • Zero runtime overhead - all generated at compile time

Installation

<PackageReference Include="SMGLocalization.SourceGenerator" Version="1.0.0">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>

Quick Start

1. Create JSON files in i18n folder

i18n/Common.en.json:

{
  "App": {
    "Name": "Smart Lighting",
    "Version": "1.0"
  },
  "Buttons": {
    "Save": "Save",
    "Cancel": "Cancel"
  }
}

i18n/Common.tr.json:

{
  "App": {
    "Name": "Akıllı Aydınlatma",
    "Version": "1.0"
  },
  "Buttons": {
    "Save": "Kaydet",
    "Cancel": "İptal"
  }
}

2. Register services

using SMG.Localization.Generated;

builder.Services.AddLocalization(options =>
{
    options.DefaultLanguage = "en";
    options.FallbackLanguage = "en";
});

3. Use in your code

Dependency Injection:

@inject ILocalizationService Localization

<h3>@Localization.Current.Common.App.Name</h3>
<button @onclick="() => Localization.SetLanguage("tr")">Türkçe</button>

Static usage:

LocalizationManager.SetLanguage("tr");
Console.WriteLine(LocalizationManager.Current.Common.App.Name);

Language Change Events

@implements IDisposable

protected override void OnInitialized()
{
    Localization.LanguageChanged += OnLanguageChanged;
}

private void OnLanguageChanged(object sender, LanguageChangedEventArgs e)
{
    InvokeAsync(StateHasChanged); // Update UI
}

public void Dispose()
{
    Localization.LanguageChanged -= OnLanguageChanged;
}

MAUI Specific Setup

For MAUI projects, add this to prevent bundle errors:

 <ItemGroup>
    <Content Remove="i18n\**\*.json" />
    <AdditionalFiles Include="i18n\**\*.json" CopyToOutputDirectory="Always"/>
</ItemGroup>

Requirements

  • .NET Standard 2.0+
  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Options

JSON File Naming

Use this pattern: {Scope}.{Language}.json

  • Common.en.jsonCommon class
  • Home.tr.jsonHome class
  • Admin.fr.jsonAdmin class

Generated Code Structure

// Generated classes
public static class Common
{
    public static class App
    {
        public static string Name { get; }
        public static string Version { get; }
    }
    
    public static class Buttons
    {
        public static string Save { get; }
        public static string Cancel { get; }
    }
}

// DI Service
public interface ILocalizationService
{
    string CurrentLanguage { get; }
    void SetLanguage(string language);
    Localization Current { get; }
    event EventHandler<LanguageChangedEventArgs> LanguageChanged;
}

License

MIT License

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .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.

Version Downloads Last Updated
1.0.1 208 10/1/2025
1.0.0 216 9/8/2025