Heren.Localization 1.0.2

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

Heren.Localization

Json localization library for .NetCore projects.

Project

This library allows to use JSON files instead of RESX in an ASP.NET application. The code tries to be most compliant with Microsoft guidelines.

Nuget Installation

PM > Install-Package Heren.Localization

Configuration

An extension method is available for IServiceCollection. It has similar configuration and usage with Microsoft guidlines. There is no extra configuration options.

using Heren.Localization;

public void ConfigureServices(IServiceCollection services)
{
    services.AddJsonLocalization(options => { options.ResourcesPath = "Resources"; });
}

You can use localization middleware as described on microsoft document. You can see a short configuration example below and also you can use all explained information on microsoft document.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    var localizationOptions = new RequestLocalizationOptions();
    localizationOptions.SetDefaultCulture("en-US");
    localizationOptions.AddSupportedCultures(new string[] { "en-US", "tr-TR" });
    localizationOptions.AddSupportedUICultures(new string[] { "en-US", "tr-TR" });
    app.UseRequestLocalization(localizationOptions);
}

Samples

First of all usage is same with native usage. The only difference is the use of json files instead of resx files. For example, you should create json files like this;

.
├── Resources
│   ├── Controllers.AuthenticationController.json
│   ├────── Controllers.AuthenticationController.tr-TR.json
│   ├── Heren.Localizer.Demo.AnAssembly.AClass.json
│   ├────── Heren.Localizer.Demo.AnAssembly.AClass.tr-TR.json
│   ├── SharedResource.json
│   └────── SharedResource.tr-TR.json
│

Sample 1: Shared Resource Usage

namespace MyProject.Controllers
{
    public class ResourcesController : ControllerBase
    {
        private readonly IStringLocalizer _localizer;

        public ResourcesController(IStringLocalizer localizer)
        {
            _localizer = localizer;
        }
    }
}

If you use like IStringLocalizer (without generic type), then it reads from Resource/SharedResource.json file.

Sample 2: Generic Type Usage With Same Assembly Model

namespace MyProject.Controllers
{
    public class AuthenticationController : ControllerBase
    {
        private readonly IStringLocalizer<AuthenticationController> localizer _localizer;

        public AuthenticationController(IStringLocalizer<AuthenticationController> localizer)
        {
            _localizer = localizer;
        }
    }
}

If you use like IStringLocalizer<AuthenticationController>, then it reads from Resource/Controllers.AuthenticationController.json file.

Sample 3: Generic Type Usage With Different Assembly Model

namespace MyProject.Controllers
{
    public class AuthenticationController : ControllerBase
    {
        private readonly IStringLocalizer<DifferentAssembly.AClass> localizer _localizer;

        public AuthenticationController(IStringLocalizer<DifferentAssembly.AClass> localizer)
        {
            _localizer = localizer;
        }
    }
}

If you use like IStringLocalizer<DifferentAssembly.AClass>, then it reads resource from Resource/DifferentAssembly.AClass.json file. If the generic type model is not in executed assembly then, it reads resource from "Resource/{AssemblyName}.{NameSpace}.{ClassName}.json".

You can see the demo project for all different samples from here.

Sample Resource Files

{AssemblyName}.{NameSpace}.{ClassName}.en-US.json

{
  "EmailNotFound": "The e-mail address you tried to login is not registered in our system.",
  "WrongPassword": "The password you tried to login is incorrect.",
  "LockedOut": "Your account has been locked due to failed login attempts. You can log in again after {0}.",
  "RequiresEmailConfirmation": "You must confirm your e-mail address before login.",
  "RequiresPhoneConfirmation": "You must confirm your phone number before login."
}

{AssemblyName}.{NameSpace}.{ClassName}.tr-TR.json

{
  "EmailNotFound": "Giriş yapmaya çalıştığınız e-posta adresi sistemimizde kayıtlı değildir.",
  "WrongPassword": "Giriş yapmaya çalıştığınız parola hatalıdır.",
  "LockedOut": "Hesabınız başarısız giriş denemeleri nedeniyle kilitlenmiştir. {0} sonra tekrar giriş yapabilirsiniz.",
  "RequiresEmailConfirmation": "Giriş yapmak için önce e-posta adresinizi onaylamanız gerekmektedir.",
  "RequiresPhoneConfirmation": "Giriş yapmak için önce telefon numaranızı onaylamanız gerekmektedir."
}

Demo Project

You can find the demo project and sample usages here. You can run api project and try this endpoints to see outputs;

  • /api/resources
  • /api/resources?culture=tr-TR
  • /api/resources/apple
  • /api/resources/apple?culture=tr-TR
  • /api/anassembly
  • /api/anassembly?culture=tr-TR
  • /api/anassembly/message1
  • /api/anassembly/message1?culture=tr-TR

Good to Know

  • The resource key searches in the {culture}.json file. If the culture file is not exists or key not exists in the culture file, then resource key searches in base file.

  • The resource file is not reading every time. Files caches by culture in memory until the base file or the culture file is modified. So the library is applies memory cache and it expiration depends to files modifications.

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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.0.2 627 3/13/2022

All .NetCore Versions Supported