Cms.AspNetCore.JsonLocalizer
1.0.6
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
<PackageReference Include="Cms.AspNetCore.JsonLocalizer" Version="1.0.6" />
<PackageVersion Include="Cms.AspNetCore.JsonLocalizer" Version="1.0.6" />
<PackageReference Include="Cms.AspNetCore.JsonLocalizer" />
paket add Cms.AspNetCore.JsonLocalizer --version 1.0.6
#r "nuget: Cms.AspNetCore.JsonLocalizer, 1.0.6"
#:package Cms.AspNetCore.JsonLocalizer@1.0.6
#addin nuget:?package=Cms.AspNetCore.JsonLocalizer&version=1.0.6
#tool nuget:?package=Cms.AspNetCore.JsonLocalizer&version=1.0.6
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 | Versions 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. |
-
net6.0
- Microsoft.AspNetCore.Http (>= 2.3.0)
- Microsoft.Extensions.DependencyInjection (>= 6.0.1)
-
net8.0
- Microsoft.AspNetCore.Http (>= 2.3.0)
- Microsoft.Extensions.DependencyInjection (>= 6.0.1)
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 |