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.
- 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
Install the package via NuGet:
dotnet add package Cms.AspNetCore.JsonLocalizer
In your Program.cs or Startup.cs, add the following:
using Cms.AspNetCore.JsonLocalizer.Extensions;
// ...
services.AddJsonLocalizer(Path.Combine(Directory.GetCurrentDirectory(), "Resources"));
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"
}
}
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();
}
}
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>
You can check if a translation was found using the ResourceNotFound property:
var result = _localizer.GetString("NonExistentKey");
if (result.ResourceNotFound)
{
// Handle missing translation
}
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.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.