![]() |
VOOZH | about |
dotnet add package fbognini.EfCoreLocalization.Dashboard --version 0.1.0
NuGet\Install-Package fbognini.EfCoreLocalization.Dashboard -Version 0.1.0
<PackageReference Include="fbognini.EfCoreLocalization.Dashboard" Version="0.1.0" />
<PackageVersion Include="fbognini.EfCoreLocalization.Dashboard" Version="0.1.0" />Directory.Packages.props
<PackageReference Include="fbognini.EfCoreLocalization.Dashboard" />Project file
paket add fbognini.EfCoreLocalization.Dashboard --version 0.1.0
#r "nuget: fbognini.EfCoreLocalization.Dashboard, 0.1.0"
#:package fbognini.EfCoreLocalization.Dashboard@0.1.0
#addin nuget:?package=fbognini.EfCoreLocalization.Dashboard&version=0.1.0Install as a Cake Addin
#tool nuget:?package=fbognini.EfCoreLocalization.Dashboard&version=0.1.0Install as a Cake Tool
A flexible, database-driven localization provider for ASP.NET Core using Entity Framework Core. This library eliminates the need for static resource files, allowing dynamic management of translations without application redeployment.
This package consists of two NuGet packages:
Install the core library:
dotnet add package fbognini.EfCoreLocalization
Optionally, install the management dashboard:
dotnet add package fbognini.EfCoreLocalization.Dashboard
Register EfCoreLocalizationDbContext to your services. You'll need to configure it with your database connection:
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<EfCoreLocalizationDbContext>(options =>
options.UseSqlServer(connectionString, b => b.MigrationsAssembly("YourAppName")),
ServiceLifetime.Singleton,
ServiceLifetime.Singleton);
Add the localization services to your DI container:
builder.Services.AddLocalization();
builder.Services.AddEfCoreLocalization(builder.Configuration);
Generate and apply the necessary database tables.
#Via .NET CLI:
dotnet ef migrations add Localization --context EfCoreLocalizationDbContext
dotnet ef database update --context EfCoreLocalizationDbContext
Via Package Manager Console in Visual Studio:
Add-Migration Localization -Context EfCoreLocalizationDbContext
Update-Database -Context EfCoreLocalizationDbContext
Alternatively, apply migrations programmatically at startup:
var app = builder.Build();
await app.ApplyMigrationEFCoreLocalization();
Enable request localization using the database settings.
var app = builder.Build();
// ... other middleware
app.UseRequestLocalizationWithEFCoreLocalization();
// ... routing and endpoints
You can configure localization settings in your appsettings.json:
{
"EfCoreLocalization": {
"DefaultSchema": "localization",
"ReturnOnlyKeyIfNotFound": true,
"CreateNewRecordWhenDoesNotExists": true,
"GlobalResourceId": null,
"ResourceIdPrefix": null,
"RemovePrefixs": [],
"RemoveSuffixs": ["Dto"]
}
}
Or configure it in code:
builder.Services.AddEfCoreLocalization(options =>
{
options.DefaultSchema = "localization";
options.ReturnOnlyKeyIfNotFound = true;
options.CreateNewRecordWhenDoesNotExists = true;
});
| Option | Type | Description |
|---|---|---|
| DefaultSchema | string | The database schema for localization tables. |
| GlobalResourceId | string? | If set, overrides the ResourceId for all lookups. |
| ReturnOnlyKeyIfNotFound | bool | If true, returns the key string when a translation is missing. |
| CreateNewRecordWhenDoesNotExists | bool | If true, automatically inserts missing keys into the database. |
| RemovePrefixs | string[] | List of prefixes to strip from the ResourceId. |
| RemoveSuffixs | string[] | List of suffixes to strip from the ResourceId. |
Use standard ASP.NET Core interfaces (IViewLocalizer, IStringLocalizer<T>).
In your Razor views, inject IViewLocalizer and use it:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
<h1>@Localizer["Welcome"]</h1>
Or in your code-behind/controllers:
public class IndexModel : PageModel
{
private readonly IStringLocalizer<IndexModel> _localizer;
public IndexModel(IStringLocalizer<IndexModel> localizer)
{
_localizer = localizer;
}
public void OnGet()
{
var message = _localizer["Welcome"];
}
}
By default, the Resource ID matches the type name (e.g., "IndexModel"). You can customize this using the LocalizationKeyAttribute:
[LocalizationKey("MyCustomResource")]
public class IndexModel : PageModel
{
// ...
}
The Dashboard provides a UI to manage translations.
Register the dashboard services:
builder.Services.AddEfCoreLocalizationDashboard();
Then add the dashboard middleware. If you're using MVC, make sure to register your controller routes first:
var app = builder.Build();
// For MVC apps, register routes before the dashboard
app.MapControllerRoute(
name: "area",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// Add the dashboard
app.UseEfCoreLocalizationDashboard();
app.MapRazorPages(); // or your other route mappings
The dashboard will be available at /localization by default. You can change the path:
app.UseEfCoreLocalizationDashboard(pathMatch: "my-custom-path", options: dashboardOptions);
By default, the dashboard only allows requests from localhost. You can customize this:
var dashboardOptions = new DashboardOptions
{
Authorization = new[] { new YourCustomAuthorizationFilter() },
AsyncAuthorization = new[] { new YourAsyncAuthorizationFilter() }
};
The library stores translations in three main tables:
TextId and ResourceId)When you call Localizer["MyKey"], the library:
CultureInfo.CurrentCultureCheck out the SampleWebApp project in the repository for a complete working example.
This code was freely inspired by AspNetCoreLocalization by damienbod.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.