WeihanLi.Extensions.Localization.Json 2.0.0

Prefix Reserved
dotnet add package WeihanLi.Extensions.Localization.Json --version 2.0.0                
NuGet\Install-Package WeihanLi.Extensions.Localization.Json -Version 2.0.0                
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="WeihanLi.Extensions.Localization.Json" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WeihanLi.Extensions.Localization.Json --version 2.0.0                
#r "nuget: WeihanLi.Extensions.Localization.Json, 2.0.0"                
#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.
// Install WeihanLi.Extensions.Localization.Json as a Cake Addin
#addin nuget:?package=WeihanLi.Extensions.Localization.Json&version=2.0.0

// Install WeihanLi.Extensions.Localization.Json as a Cake Tool
#tool nuget:?package=WeihanLi.Extensions.Localization.Json&version=2.0.0                

WeihanLi.Extensions.Localization.Json WeihanLi.Extensions.Localization.Json

Intro

dotnet JSON file based localization

Build

AzureDevOps Build Status

Github Build Status

GetStarted

register required services:

services.AddJsonLocalization(options =>
    {
        options.ResourcesPath = Configuration.GetAppSetting("ResourcesPath");
        options.ResourcesPathType = ResourcesPathType.TypeBased; // by default, looking for resourceFile like Microsoft do
        // options.ResourcesPathType = ResourcesPathType.CultureBased; // looking for resource file in culture sub dir see details follows
    });

middleware config(the same with before):

app.UseRequestLocalization();

That's it~

Add your resource files

TypeBasedResourcePath

For Types:

Home/Index ⇒ Controllers/HomeController

the resource path looking for:

  • Controllers/HomeController.[cultureName].json

for example:

  • Resources/Controllers/HomeController.en.json
  • Resources/Controllers/HomeController.zh.json

For RazorViews:

for example:

  • Resources/Views/Home/Index.en.json
  • Resources/Views/Home/Index.zh.json

CultureBasedResourcePath

For Types:

Home/Index ⇒ Controllers/HomeController

the resource path looking for:

  • Resources/[cultureName]/Controllers/HomeController.json

for example:

  • Resources/en/Controllers/HomeController.json
  • Resources/zh/Controllers/HomeController.json

For RazorViews:

for example:

  • Resources/en/Views/Home/Index.json
  • Resources/zh/Views/Home/Index.json

Copy your resource files to output:

you had to set resource files copy to output to make it works normal

add the follows sample config to your startup project file:

<ItemGroup>
<Content Update="Resources\**\*.json">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

the config above is made to make sure your json resource files in Resources dir copied to output, change it if you need

Use

just like what you do before:

Controller sample:

public class ValuesController : Controller
{
    private readonly IStringLocalizer<ValuesController> _localizer;

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

    // GET: api/<controller>
    [HttpGet]
    public string Get()
    {
        return _localizer["Culture"];
    }
}

Razor View Sample:

@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Localization
@using WeihanLi.Extensions.Localization.Json.Sample.Controllers
@inject IHtmlLocalizer<HomeController> HtmlLocalizer
@inject IStringLocalizer<HomeController> StringLocalizer
@inject IViewLocalizer ViewLocalizer
@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<div>string: @StringLocalizer["Hello"]</div>

<div>html: @HtmlLocalizer["Hello"]</div>

<div>view: @ViewLocalizer["Hello"]</div>

Resource file sample:

{
  "Culture": "English"
}

Samples

Contact

Contact me via weihanli@outlook.com if you need

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  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. 
.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 (1)

Showing the top 1 popular GitHub repositories that depend on WeihanLi.Extensions.Localization.Json:

Repository Stars
WeihanLi/DbTool
数据库工具,根据表结构文档生成创建表sql,根据数据库表信息导出Model和表结构文档,根据文档生成数据库表,根据已有Model文件生成创建数据库表sql
Version Downloads Last updated
2.0.0 565 11/24/2023
1.0.3 6,679 3/22/2020
1.0.2 666 1/31/2020
1.0.1 524 1/15/2020
1.0.1-preview-20200115-163444 403 1/15/2020