Cms.AspNetCore.JsonLocalizer 1.0.6

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Cms.AspNetCore.JsonLocalizer --version 1.0.6
                    
NuGet\Install-Package Cms.AspNetCore.JsonLocalizer -Version 1.0.6
                    
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="Cms.AspNetCore.JsonLocalizer" Version="1.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Cms.AspNetCore.JsonLocalizer" Version="1.0.6" />
                    
Directory.Packages.props
<PackageReference Include="Cms.AspNetCore.JsonLocalizer" />
                    
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 Cms.AspNetCore.JsonLocalizer --version 1.0.6
                    
#r "nuget: Cms.AspNetCore.JsonLocalizer, 1.0.6"
                    
#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 Cms.AspNetCore.JsonLocalizer@1.0.6
                    
#: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=Cms.AspNetCore.JsonLocalizer&version=1.0.6
                    
Install as a Cake Addin
#tool nuget:?package=Cms.AspNetCore.JsonLocalizer&version=1.0.6
                    
Install as a Cake Tool

Cms.AspNetCore.JsonLocalizer

Cms.AspNetCore.JsonLocalizer is a flexible and efficient JSON-based localization library for ASP.NET Core applications. It provides an easy way to manage and retrieve localized strings using JSON resource files, supporting culture-specific localization and seamless integration with dependency injection.

Features

  • JSON-based localization
  • Culture-specific resource loading
  • Caching support for improved performance
  • Easy integration with ASP.NET Core dependency injection
  • Support for parameterized messages
  • Fallback to default culture if translation is not found

Installation

Install the package via NuGet:

dotnet add package Cms.AspNetCore.JsonLocalizer

Usage

1. Configure Services

In your Program.cs or Startup.cs, add the following:

using Cms.AspNetCore.JsonLocalizer.Extensions;

// ...

services.AddJsonLocalizer(Path.Combine(Directory.GetCurrentDirectory(), "Resources"));

2. Create JSON Resource Files

Create JSON files for each supported culture in the Resources directory. For example: en-US.json:

{
    "Welcome": "Welcome to our application!",
    "Greeting": "Hello, {0}!",
    "Menu": {
        "Home": "Home",
        "About": "About",
        "Contact": "Contact"
    }
}

es-ES.json:

{
    "Welcome": "¡Bienvenido a nuestra aplicación!",
    "Greeting": "¡Hola, {0}!",
    "Menu": {
        "Home": "Inicio",
        "About": "Acerca de",
        "Contact": "Contacto"
    }
}

3. Use in Controllers

Inject ILocalizer into your controllers:

using Cms.AspNetCore.JsonLocalizer.Interfaces;

public class HomeController : Controller
{
    private readonly ILocalizer _localizer;

    public HomeController(ILocalizer localizer)
    {
        _localizer = localizer;
    }

    public IActionResult Index()
    {
        ViewBag.Welcome = _localizer.GetString("Welcome").Value;
        ViewBag.Greeting = _localizer.GetString("Greeting", "Alice").Value;
        return View();
    }
}

4. Use in Views

In your views, you can use the ILocalizer directly:

@inject Cms.AspNetCore.JsonLocalizer.Interfaces.ILocalizer Localizer

<h1>@Localizer.GetString("Welcome").Value</h1>
<p>@Localizer.GetString("Greeting", "User").Value</p>

<nav>
    <ul>
        <li>@Localizer.GetString("Menu.Home").Value</li>
        <li>@Localizer.GetString("Menu.About").Value</li>
        <li>@Localizer.GetString("Menu.Contact").Value</li>
    </ul>
</nav>

Advanced Usage

Handling Missing Translations

You can check if a translation was found using the ResourceNotFound property:

var result = _localizer.GetString("NonExistentKey");
if (result.ResourceNotFound)
{
    // Handle missing translation
}

Changing Cultures

The library uses the Accept-Language header from the HTTP request to determine the culture. You can also set the culture manually in your application's middleware if needed.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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. 
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
2.0.0-development.3 201 5/16/2025
1.1.0-alpha.1 65 2/3/2025
1.0.7-beta.1 68 2/3/2025
1.0.6 107 2/3/2025
1.0.6-beta.51 70 2/3/2025
1.0.4 111 2/2/2025
1.0.0 99 2/1/2025
0.0.0 117 2/1/2025