JCTools.I18N
2.0.0.2
dotnet add package JCTools.I18N --version 2.0.0.2
NuGet\Install-Package JCTools.I18N -Version 2.0.0.2
<PackageReference Include="JCTools.I18N" Version="2.0.0.2" />
paket add JCTools.I18N --version 2.0.0.2
#r "nuget: JCTools.I18N, 2.0.0.2"
// Install JCTools.I18N as a Cake Addin #addin nuget:?package=JCTools.I18N&version=2.0.0.2 // Install JCTools.I18N as a Cake Tool #tool nuget:?package=JCTools.I18N&version=2.0.0.2
JCTools.I18N
A simplification of the configuration of location in .net core
Installation
PM> Install-Package JCTools.I18N
Usage
For configure the localization and globalization settings follow the next steps:
- Add the nuget package
PM> Install-Package JCTools.I18N
- Add a new private field at the Startup class
private readonly IHostingEnvironment _enviroment;
- Set the value of the created field in the last step, with the value of the constructor argument
public Startup(IHostingEnvironment env)
{
// Store the enviroment instance
_enviroment = env;
...
}
- Create a new empty class. The application will use this class how reference to the unique resources file. (You can use the Startup class)
- Create a new folder for stored the resource files, and add a new resource file named equals that the class of the previous step, this resource file not should have the culture postfix. This file will use for get the localized strings for the default culture. This file should have your access modifier setted to Public.
- Create the necessary resource files for stored the localized strings of the rest of supported cultures. This files should have your access modifier setted to public.
- Add the next lines to the startup class in the ConfugureServices method:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc();
services.AddLocalizationServices(
// The supported cultures
new List<CultureInfo>
{
new CultureInfo("en"),
new CultureInfo("en-US"),
new CultureInfo("es"),
new CultureInfo("es-MX")
},
// the default culture
defaultCulture: "es-MX"
)
.AddSingleLocalizationFile<I18N, Resources.I18N>(_enviroment);
}
- Add the next line in the method Configure of the Startup class:
app.UseLocalization();
The previous line should will be add before the next code:
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
For access at the localized strings into the Controllers, use:
- Add a private field for stored the localizer instance
private readonly SingleLocalizer _localizer;
- Add at the constructor of your controller an argument for receive the localizer instance through dependency injection and set the value of the created field in the previous step
public HomeController(SingleLocalizer localizer)
{
...
// store the receive argument into our field
_localizer = localizer;
...
}
- Use the field as shown below:
ViewData["Message"] = _localizer["About_Message"];
For access at the localized string into the views, use:
- Add at your _ViewImports.cshtml file the next dependency, it allows access in all views at the Localizer instance:
@inject JCTools.I18N.Services.SingleHtmlLocalizer Localizer
- Use the Localize instance as shown below:
<strong>@Localizer["Support"]:</strong> <a href="mailto:Support@example.com">Support@example.com</a>
For access to the localized strings into the models and viewmodels, use the data annotation of the namespace System.ComponentModel.DataAnnotations, how to shown below
using System.ComponentModel.DataAnnotations;
namespace JCTools.I18N.Test.ViewModels
{
public class ContactViewModel
{
[Display(Name = "ContactViewModel_Email", ResourceType = typeof(Resources.I18N))]
[EmailAddress(ErrorMessageResourceName = "ContactViewModel_EmailError", ErrorMessageResourceType = typeof(Resources.I18N))]
public string Email { get; set; }
[Display(Name = "ContactViewModel_Message", ResourceType = typeof(Resources.I18N))]
public string Message { get; set; }
}
}
For view spanish usage process, visit: JCTools.mx
License
Product | Versions 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. |
.NET Core | netcoreapp1.1 is compatible. netcoreapp2.0 is compatible. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
-
.NETCoreApp 1.1
- Microsoft.AspNetCore.Hosting.Abstractions (>= 1.1.2)
- Microsoft.AspNetCore.Localization (>= 1.1.2)
- Microsoft.AspNetCore.Localization.Routing (>= 1.1.2)
- Microsoft.AspNetCore.Mvc (>= 1.1.3)
- Microsoft.AspNetCore.Mvc.Razor (>= 1.1.3)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 1.1.1)
- Microsoft.Extensions.Localization (>= 1.1.2)
- Microsoft.Extensions.Localization.Abstractions (>= 1.1.2)
- Microsoft.Extensions.Options (>= 1.1.2)
- Microsoft.NETCore.App (>= 1.1.2)
- System.Globalization (>= 4.3.0)
-
.NETCoreApp 2.0
- Microsoft.AspNetCore.Hosting.Abstractions (>= 2.0.0)
- Microsoft.AspNetCore.Localization (>= 2.0.0)
- Microsoft.AspNetCore.Localization.Routing (>= 2.0.0)
- Microsoft.AspNetCore.Mvc (>= 2.0.0)
- Microsoft.AspNetCore.Mvc.Razor (>= 2.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 2.0.0)
- Microsoft.Extensions.Localization (>= 2.0.0)
- Microsoft.Extensions.Localization.Abstractions (>= 2.0.0)
- Microsoft.Extensions.Options (>= 2.0.0)
- System.Globalization (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.