iCat.Localization
1.0.1
See the version list below for details.
dotnet add package iCat.Localization --version 1.0.1
NuGet\Install-Package iCat.Localization -Version 1.0.1
<PackageReference Include="iCat.Localization" Version="1.0.1" />
paket add iCat.Localization --version 1.0.1
#r "nuget: iCat.Localization, 1.0.1"
// Install iCat.Localization as a Cake Addin
#addin nuget:?package=iCat.Localization&version=1.0.1
// Install iCat.Localization as a Cake Tool
#tool nuget:?package=iCat.Localization&version=1.0.1
iCat.Localization
iCat.Localization is seamlessly integrated to the Microsoft.Extensions.Localization
package and compatible with the Microsoft's localization documentation. It can handle sentences, as well as multi-layer positioning and dynamic parameters.
Installation
dotnet add package iCat.Localization
dotnet add package iCat.Localization.Extension.Web
Configuration
Using
using iCat.Localization.Models;
using iCat.Localization.Extensions;
using iCat.Localization.Extension.Web;
Razor View, Model Validation
The configuration is used to use localization in razor view pages and model validation
builder.Services
.AddControllersWithViews()
.AddViewLocalization() // Localiztion in Razor View
.AddDataAnnotationsLocalization(); // Localization in Model Validation
CultureInfo supports list from request and key
Localizaion determines the current language through culture info in the current thread. .net provide Request Middleware to handle culture from route/query string/cookie, the setting configure the key in them and supportes list
// Configure cultureInfo from request and support list
builder.Services.AddRequestLocalizationOptions(new Syst Globalization. CultureInfo[] {
new System.Globalization.CultureInfo("en-US"),
new System.Globalization.CultureInfo("zh-TW"),
}, "LangCode"); // key in Route/QueryString/Cookie
Localization Resource
// Configure localization data
builder.Services.AddiCatLocalizationeService(neList<LocalizationMapping> {
new LocalizationMapping {
CultureName = "en-US",
LanguageData = new Dictionary<string, string>{
{"Error.Required", "Can't be null {0}"},
{"Error.MaxLength", "{0} Length Over Than {1}"},
{"Name", "Eric"},
{"TestSentence","My name is {#Name}" }
}
},
new LocalizationMapping {
CultureName = "zh-TW",
LanguageData = new Dictionary<string, string>{
{"Error.Required", "{0} 不能為空"},
{"Error.MaxLength", "超過 {1},欄位{0}"},
{"Name", "艾瑞克"},
{"TestSentence","{#Name} 是我的名字" }
}
}
}, new Options { EnableKeyNotFoundException = false });
String Extension
Initial string extension to make it easier to obtain localized strings
// Configure string extension for iCat.Localization
app.UseiCatLocalizationExtension();
Reserved word
There are reserved words when localizer parser strings, so don't use these words in string for localization.
{
{#
}
^
##
@@
Sample
Microsoft IStringLocalizer
Sentence parser does not work with Microsoft IStringLocalizer, It can only use generic scenarios
using Microsoft.Extensions.Localization;
public class HomeController : Controller
{
private readonly IStringLocalizer _stringLocalizer;
public HomeController(IStringLocalizer stringLocalizer)
{
_stringLocalizer = stringLocalizer;
}
public IActionResult Index()
{
var result = _stringLocalizer["Name"];
return View();
}
}
IiCatStringLocalizer
using iCat.Localization.Interfaces;
public class HomeController : Controller
{
private readonly IiCatStringLocalizer _iCatStringLocalizer;
public HomeController(IiCatStringLocalizer iCatStringLocalizer)
{
_iCatStringLocalizer = iCatStringLocalizer;
}
public IActionResult Index()
{
var result = _iCatStringLocalizer.Localize("My name is {Name}", "en-US");
return View();
}
}
String Extension
Generic usage
using iCat.Localization.Extensions;
public class HomeController : Controller
{
public HomeController()
{
}
public IActionResult Index()
{
var resultA = "My name is {Name}".Localize(); // current cultureInfo
var resultB = "My name is {Name}".Localize("en-US"); // specify cultureName
return View();
}
}
Parameters
Reserved word "{# //property }" will get string from dynamic parameter. The variable "converted" could be stored in database, and read then localized.
using iCat.Localization.Extensions;
public class HomeController : Controller
{
public IActionResult Index()
{
var converted = "My name is {Name}, Age is {#Age}, my school is {#School}".AddParams(new { School = "School", Age = "99" });
var resultA = converted.Localize(); // current cultureInfo
var resultB = converted.Localize("en-US"); // specify cultureName
return View();
}
}
Multi-layer
using iCat.Localization.Extensions;
public class HomeController : Controller
{
public IActionResult Index()
{
// {TestSentence} is "My name is {#Name}" in LocalizationMappingdata;
var converted = "{TestSentence}, Age is {#Age}, my school is{#School}".AddParams(new { Name = "Test", School = "School", Age ="99" });
var resultA = converted.Localize(); // current cultureInfo
var resultB = converted.Localize("en-US"); // specify cultureName
return View();
}
}
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 is compatible. 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. |
-
net6.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Localization (>= 8.0.0)
-
net7.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Localization (>= 8.0.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Localization (>= 8.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on iCat.Localization:
Package | Downloads |
---|---|
iCat.Localization.Extension.Web
Dynamic localization extension library for web project |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version 1.0.1