在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):RickStrahl/Westwind.Globalization开源软件地址(OpenSource Url):https://github.com/RickStrahl/Westwind.Globalization开源编程语言(OpenSource Language):C# 45.9%开源软件介绍(OpenSource Introduction):West Wind GlobalizationDatabase Resource Localization for .NETWestwind.Globalization (.NET Standard, .NET 4.5+): Westwind.Globalization.AspNetCore (.NET Core 3.1, 2.1): Westwind.Globalization.Web (.NET 4.5+): This library and tooling provides easy to use database resource managers and providers that allow you to use a database for storing localization resources. Unlike static Resx resources, database resources are dynamic, can be changed at runtime and are editable by multiple users at the same time. The custom resource managers, providers and ASP.NET Core StringLocalizers use the standard .NET resource infrastructure, so other than startup configuration there are no code changes when switching from using traditional Resx resources. It's also possible to import resources into a database, edit them dynamically, and then export them back out into Resx and optionally strongly typed classes so your deployed applications can run with Resx resources, while you can use dynamic Database resources during development. A rich, Web based resource editor is also provided that makes it easy to create resource content and translate it interactively in a running application where you can see resource changes immediately applied without recompilation. You can import and export Resx resources, generate strongly typed classes and serve resources to JavaScript applications using the database resources. Quick Links
Requirements:
.NET Version SupportVersion 3.0 adds support for the 2.0 versions of .NET Standard, .NET Core and ASP.NET Core. The following versions are provided:
InstallationInstallation is different depending on which version of .NET you are running under .NET Core and Full Framework use different project types and NuGet Packages for the Web support.
For installation use NuGet. To Install for .NET CorePlease read the installation instructions below to configure once you've installed the project. You will need to configure startup settings in order for the Db Providers to run.
If you're not using a Web Project you can just use the base package:
To Install on .NET FrameworkPlease read the Installation Section below or watch the Getting Started Video, which describes how to install the packages, configure the project, import existing resources.
or if you don't want sample resources and a test page, simply use:
If you're not using a Web Project or you're using MVC/Web API and don't need the Web Resource Editor you can just use the core package:
Resources:
Features
Because this library uses the standard .NET resource infrastructure using the DbResourceProvider or DbResourceManager requires no code changes from ResX resources, other than provider configuration in your .config file and potentially a few lines of startup code. You can import existing Resx resources and edit them interactively. Serve your resources either with the DbResourceProvider or DbResourceManager, or if you chose export them back out to Resx files for your final application code. Importing and exporting is a easily done from the Web admin interface or can be fired using code including as part of your build process.
Web Resource EditorOne of the main reasons people want to use Database resources rather than Resx resources is that it allows for dynamic updates of resources. Resx resources are static and compiled into an application and so are typically tied to the development process, while dynamic resources can be updated separately even after the application has been completed and deployed. Since data is stored in a database it's easy to create editing front ends or programmatic tools that simply manipulate the database. This library ships with a Web interface that allows editing of resources interactively and an easy to use data API to update resources programmatically. The resource editor is an easy way to localize resources interactively, but it's not the only way you can do this of course. Since you have access to the data API underneath it as well as the database itself, it's easy to create your own customized UI or data driven API that suits your application needs exactly.
How the database Providers workThis library implements a custom .NET
The Underneath the .NET providers lies a the Additionally the API can be directly accessed to provide resource access, and the DbRes helper class provides very easy access to these resources using the DbRes.T() method which can be thought of as a high level translation method. This interface is also directly accessible and allows your code as well as support code like the UI Web Resource editor to easily access and manipulate resources in real-time at runtime. This library includes quite a proliferation of classes most of it due to the implementation requirements for the .NET providers which require implementation of a host of interface based classes for customization. There are three distinct resource access mechanisms supported:
Running the Sample ApplicationTo run the sample application you have to set up a database to provide the resources. The following assumes you are using the default configuration which uses SQL Server and a database named Localizations - you can change this via web.config settings (see the following section for more details).
Installation and ConfigurationThe easiest way to use this library in your own applications is to install the NuGet package into an ASP.NET application. ASP.NET Core Packages
For non-Web applications or if you use only the DbRes based localization features, you can just install the base package.
ASP.NET Core integration works in combination with ASP.NET Core new Localization features. Westwind.Globalization builds on top of this, or you can just use its native features. West Wind Globalization supports:
ASP.NET Core ConfigurationConfiguration can be accomplished in 3 ways:
Configuration values are applied in the order listed, with later assignments over-writing earlier settings. DbResourceConfiguration.jsonYou can create a standalone {
"ResourceAccessMode": "DbResourceManager",
"ConnectionString": "server=.;database=localizations;integrated security=true;",
"DataProvider": "SqlServer",
"ResourceTableName": "Localizations",
"ResxExportProjectType": "Project",
"ResxBaseFolder": "~/Properties/",
"StronglyTypedGlobalResource": "~/Properties/Resources.cs",
"ResourceBaseNamespace": "AppResources",
"AddMissingResources": true,
"LocalizationFormWebPath": "~/LocalizationAdmin/",
"GoogleApiKey": "XXXfaSyDcvmGhGN7FlynP9QUZOLF8_4K8iF9ChWo",
"BingClientId": "12345-4b99-47ed-be7e-caf733526020"
} If this file exists configuration values are read from it.
ASP.NET Core IConfigurationFor ASP.NET Core operation Westwind.Globalization also registers the This means you can use any configured configuration providers - most commonly:
You can store configuration settings in {
"Logging": {...},
"DbResourceConfiguration": {
"ResourceAccessMode": "DbResourceManager",
"ConnectionString": "server=.;database=localizations;integrated security=true;",
"DataProvider": "SqlServer",
"ResourceTableName": "Localizations",
"StronglyTypedGlobalResource": "~/Properties/Resources.cs",
"ResourceBaseNamespace": "AppResources",
"ResxExportProjectType": "Project",
"ResxBaseFolder": "~/Properties/",
"AddMissingResources": true,
"LocalizationFormWebPath": "~/LocalizationAdmin/",
"BingClientId": "12345-4b99-47ed-be7e-caf733526020",
"GoogleApiKey": "XXXfaSyDcvmGhGN7FlynP9QUZOLF8_4K8iF9ChWo"
}
} If provided the We recommend you only use one of the files to avoid confusion. For ASP.NET Core projects we recommend you store settings in Enabling West Wind Globalization in ASP.NET CoreYou also need to explicitly enable localization features in ASP.NET Core using the following code in the public void ConfigureServices(IServiceCollection services)
{
// Standard ASP.NET Localization features are recommended
// Make sure this is done FIRST!
services.AddLocalization(options =>
{
// I prefer Properties over the default `Resources` folder
// due to namespace issues if you have a Resources type as
// most people do for shared resources.
options.ResourcesPath = "Properties";
});
// Replace StringLocalizers with Db Resource Implementation
services.AddSingleton(typeof(IStringLocalizerFactory),
typeof(DbResStringLocalizerFactory));
services.AddSingleton(typeof(IHtmlLocalizerFactory),
typeof(DbResHtmlLocalizerFactory));
// Required: Enable Westwind.Globalization (opt parm is optional)
// shown here with optional manual configuration code
services.AddWestwindGlobalization(opt =>
{
// the default settings comme from DbResourceConfiguration.json if exists
// you can override the settings here, the config you create is added
// to the DI system (DbResourceConfiguration)
// Resource Mode - from Database (or Resx for serving from Resources)
opt.ResourceAccessMode = ResourceAccessMode.DbResourceManager; // .Resx
// Make sure the database you connect to exists
opt.ConnectionString = "server=.;database=localizations;uid=localizations;pwd=local";
// Database provider used - Sql Server is the default
opt.DataProvider = DbResourceProviderTypes.SqlServer;
// The table in which resources are stored
opt.ResourceTableName = "localizations";
opt.AddMissingResources = false;
opt.ResxBaseFolder = "~/Properties/";
// Set up security for Localization Administration form
opt.ConfigureAuthorizeLocalizationAdministration(actionContext =>
{
// return true or false whether this request is authorized
return true; //actionContext.HttpContext.User.Identity.IsAuthenticated;
});
});
...
// .NET Core 2.x
// services.AddMvc()
// .AddViewLocalization()
// .AddDataAnnotationsLocalization();
// .AddApplicationPart(typeof(DbResViewLocalizer).Assembly);
// .NET Core 3.1
services.AddMvc(opt =>
// required for the Administration interface for dynamic serialization
.AddNewtonsoftJson();
// for MVC/RazorPages localization
.AddViewLocalization()
// for ViewModel Error Annotation Localization
.AddDataAnnotationsLocalization();
// this *has to go here* after view localization has been initialized
// so that Pages can localize - note required even if you're not using
// the DbResource manager.
services.AddTransient<IViewLocalizer, DbResViewLocalizer>();
} Any code changes made override any of the file values. You can also replace the entire In addition you probablywant to add standard ASP.NET Core Localization features to the public void Configure(IApplicationBuilder app)
{
..
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("en"),
new CultureInfo("de-DE"),
new CultureInfo("de"),
new CultureInfo("fr")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
..
// .NET Core 3.1
app.UseRouting();
app.UseDefaultFiles();
app.UseStaticFiles();
// .NET Core 3.1 required for Endpoints
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapDefaultControllerRoute();
});
// .NET Core 2.x only needs this
//app.UseMvc()
}
Dependency Injection for DbResourceConfigurationYou can get access to DbResourceConfiguration in a number of ways:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论