Quantcast
Channel: damienbod – Software Engineering
Viewing all articles
Browse latest Browse all 353

Released SQL Localization NuGet package for ASP.NET Core, dotnet

$
0
0

I have released a simple SQL Localization NuGet package which can be used with ASP.NET Core and any database supported by Entity Framework Core. The localization can be used like the default ASP.NET Core localization.

I would be grateful for feedback, new feature requests, pull requests, or ways of improving this package.

NuGet | Issues | Code

Examples:

https://github.com/damienbod/AspNet5Localization/tree/master/AspNet5Localization/src/AspNet5Localization

https://github.com/damienbod/Angular2LocalizationAspNetCore

Release History

Version 1.0.1

  • Added Unique constraint for key, culture
  • Fixed type full name cache bug

Version 1.0.0

  • Initial release
  • Runtime localization updates
  • Cache support, reset cache
  • ASP.NET DI support
  • Supports any Entity Framework Core database

Basic Usage ASP.NET Core

Add the NuGet package to the project.json file

"dependencies": {
        "Localization.SqlLocalizer": "1.0.0.0",

Add the DbContext and use the AddSqlLocalization extension method to add the SQL Localization package.

public void ConfigureServices(IServiceCollection services)
{
	// init database for localization
	var sqlConnectionString = Configuration["DbStringLocalizer:ConnectionString"];

	services.AddDbContext<LocalizationModelContext>(options =>
		options.UseSqlite(
			sqlConnectionString,
			b => b.MigrationsAssembly("Angular2LocalizationAspNetCore")
		)
	);

	// Requires that LocalizationModelContext is defined
	services.AddSqlLocalization(options => options.UseTypeFullNames = true);

Create your database

dotnet ef migrations add Localization --context LocalizationModelContext

dotnet ef database update Localization --context LocalizationModelContext

And now it can be used like the default localization.
See Microsoft ASP.NET Core Documentation for Globalization and localization

Add the standard localization configuration to your Startup ConfigureServices method:

services.Configure<RequestLocalizationOptions>(
	options =>
		{
			var supportedCultures = new List<CultureInfo>
			{
				new CultureInfo("en-US"),
				new CultureInfo("de-CH"),
				new CultureInfo("fr-CH"),
				new CultureInfo("it-CH")
			};

			options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
			options.SupportedCultures = supportedCultures;
			options.SupportedUICultures = supportedCultures;
		});

services.AddMvc()
	.AddViewLocalization()
	.AddDataAnnotationsLocalization();

And also in the configure method:

var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
            app.UseRequestLocalization(locOptions.Value);

Use like the standard localization.

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;

namespace AspNet5Localization.Controllers
{
    [Route("api/[controller]")]
    public class AboutController : Controller
    {
        private readonly IStringLocalizer<SharedResource> _localizer;
        private readonly IStringLocalizer<AboutController> _aboutLocalizerizer;

        public AboutController(IStringLocalizer<SharedResource> localizer, IStringLocalizer<AboutController> aboutLocalizerizer)
        {
            _localizer = localizer;
            _aboutLocalizerizer = aboutLocalizerizer;
        }

        [HttpGet]
        public string Get()
        {
            // _localizer["Name"]
            return _aboutLocalizerizer["AboutTitle"];
        }
    }
}

Links:

Microsoft ASP.NET Core Documentation for Globalization and localization



Viewing all articles
Browse latest Browse all 353

Trending Articles