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.
DOCUMENTATION NOW HERE:
http://localizationsqllocalizer.readthedocs.io/en/latest/index.html
Features
- Supports any EFCore persistence
- Import, export
- Cache, reset cache
- support for live update
- Configurable keys for localization records
- Default key display possible, if no localization available
- KISS
Examples:
https://github.com/damienbod/AspNetCoreLocalization/tree/master/src/AspNetCoreLocalization
https://github.com/damienbod/AngularLocalizationAspNetCore
https://damienbod.com/2016/07/15/import-export-asp-net-core-localized-data-as-csv/
Basic Usage ASP.NET Core
Add the NuGet package to the project.json file
"dependencies": { "Localization.SqlLocalizer": "1.0.10",
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"]; } } }
Release History
Version 1.0.10
- Support for automaically added undefined resources as ong as the culture is supported
- Support for net461
Version 1.0.6
- return default key if localization cannot be found support
Example:
var useTypeFullNames = true; var useOnlyPropertyNames = false; var returnOnlyKeyIfNotFound = true; services.AddSqlLocalization(options => options.UseSettings( useTypeFullNames, useOnlyPropertyNames, returnOnlyKeyIfNotFound, false ));
Version 1.0.5
- bugfix context System.InvalidOperationException import, export
Version 1.0.4
- Updated to .NET Core 1.1
- changed the constraint to included the resourceKey for new records
Version 1.0.3
- adding import, export interfaces
Version 1.0.2
- Updated to dotnet RTM
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
Links:
Microsoft ASP.NET Core Documentation for Globalization and localization
Thanks Damien for releasing that. Just wondering if DataAnnotationsLocalization will work using the SqlLocalization. Can you please share any examples of that. I couldn’t get it to work by following your earlier post before the RC2 was released.
If I use the ErrorMessageResourseName and ErrorMessaResourseType properties of DataAnnotations it doesn’t look up for the resource in the database. Any ideas?
Hi Shahzad, thanks
I’ll create an example tomorrow.
Greetings Damien
Thanks for pointing this out, I fixed this bug. The Data Annotations on the Box class now work.
https://damienbod.wordpress.com/2015/10/24/using-dataannotations-and-localization-in-asp-net-5-mvc-6
Thanks Damien. I will give it a go and will let you know.
Hi Damien, have been looking over your dotnet core repos with A2 and was going to jump to this one to use however I cannot get this running under vs2017 is there a vs2015 version per chance?
Hi Peter
Only VS2015 version at present. VS2017 tooling is not stable at present and the EF migrations don’t work yet, so I am waiting for a VS2017 update which fixes this and will migration the project then.
http://localizationsqllocalizer.readthedocs.io/en/latest/index.html
Greetings Damien
Hello –
How shound be format for the ResourceKey field for use inside View (injecting IViewLocalizer) ? I try for example: Views.Home.Index .. not works
Regards
Study
https://github.com/damienbod/AspNet5Localization/issues/31