ASP.NET 5.0 - 如何启动 CookieRequestCultureProvider 和 运行
ASP.NET 5.0 - How to get CookieRequestCultureProvider up and running
本地化 - 设置
我无法使用 cookie 进行本地化。我很乐意了解如何最终整合这项技术。我不得不说,我是开发人员世界的新手,并尝试研究互联网上的所有方法,以便自己解决这个问题。
到目前为止,Startup.cs 中的默认设置对我来说是可以理解的。其他的classes,又得重新去理解了。我目前不知道如何以及在何处使用 CookieRequestCultureProvider class 来使网站的 cookie 功能正常工作。
提前致谢。
PS:我参与了以下教程。
https://www.mikesdotnetting.com/article/345/localisation-in-asp-net-core-razor-pages-cultures
https://www.mikesdotnetting.com/article/346/using-resource-files-in-razor-pages-localisation
以下是我目前的设置:
dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.301
Commit: ef17233f86
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19043
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk.0.301\
Host (useful for support):
Version: 5.0.7
Commit: 556582d964
.NET SDKs installed:
5.0.301 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.16 [C:\ProgramFiles\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
startup.cs | ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
// Localization - adding resource path
services.AddLocalization(options =>
{
// We will put our translations in a folder called Resources
options.ResourcesPath = "Resources";
});
// Localization - look up MS docs
// Add framework services.
services.AddMvc()
// adding localization for views
.AddViewLocalization()
// e.g. [Display(Name = "Remember Me?")]
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
{
var assemblyName = new AssemblyName(typeof(CommonResources).GetTypeInfo().Assembly.FullName);
return factory.Create(nameof(CommonResources), assemblyName.Name);
};
});
// we are configuring the localization service to support a list of provided cultures.
services.Configure<RequestLocalizationOptions>(options =>
{
// Localization - the list of supported cultures
// combination of parent language and region
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("de-DE"),
new CultureInfo("en-GB"),
new CultureInfo("nl-NL")
};
// State what the default culture for your application is. This will be used if no
// specific culture can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture(
culture: supportedCultures[0],
uiCulture: supportedCultures[0]);
// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// UI strings that we have localized
options.SupportedUICultures = supportedCultures;
});
...
}
startup.cs | Configure
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider services)
{
...
app.UseRouting();
// Localization - middleware
// 1.QueryStringRequestCultureProvider – culture and ui - culture are passed in the query string.
// 2.CookieRequestCultureProvider – culture is set in the ASP.NET Core culture cookie.
// 3.AcceptLanguageHeaderRequestCultureProvider – which gets the culture from the browser’s language set by the user.
// specify that globalization is being used in the pipeline.
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
...
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
Culture Switcher Razor Component
<div>
<form id="culture-switcher">
<select name="culture" id="culture-options">
<option></option>
@foreach (var culture in Model.SupportedCultures)
{
<option value="@culture.Name" selected="@(Model.CurrentUICulture.Name == culture.Name)">@culture.ThreeLetterWindowsLanguageName</option>
}
</select>
</form>
</div>
<script>
document.getElementById("culture-options").addEventListener("change", () => {
document.getElementById("culture-switcher").submit();
});
</script>
CommonLocalizationService.cs
using KarriereWebseiteRazor.Resources;
using Microsoft.Extensions.Localization;
using System.Reflection;
namespace KarriereWebseiteRazor.Services
{
public class CommonLocalizationService
{
private readonly IStringLocalizer _localizer;
public CommonLocalizationService(IStringLocalizerFactory factory)
{
var assemblyName = new AssemblyName(typeof(CommonResources).GetTypeInfo().Assembly.FullName);
_localizer = factory.Create(nameof(CommonResources), assemblyName.Name);
}
public string Get(string key)
{
return _localizer[key];
}
}
}
也许实现您想要的最简单方法是更改 ViewComponent 中的 JavaScript 以设置 cookie:
const cultureOptions = document.getElementById("culture-options");
cultureOptions.addEventListener("change", () => {
const culture = cultureOptions.value;
document.cookie = `.AspNetCore.Culture=c=${culture}|uic=${culture}`;
location.reload()
});
本地化 - 设置
我无法使用 cookie 进行本地化。我很乐意了解如何最终整合这项技术。我不得不说,我是开发人员世界的新手,并尝试研究互联网上的所有方法,以便自己解决这个问题。
到目前为止,Startup.cs 中的默认设置对我来说是可以理解的。其他的classes,又得重新去理解了。我目前不知道如何以及在何处使用 CookieRequestCultureProvider class 来使网站的 cookie 功能正常工作。
提前致谢。
PS:我参与了以下教程。
https://www.mikesdotnetting.com/article/345/localisation-in-asp-net-core-razor-pages-cultures
https://www.mikesdotnetting.com/article/346/using-resource-files-in-razor-pages-localisation
以下是我目前的设置:
dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.301
Commit: ef17233f86
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19043
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk.0.301\
Host (useful for support):
Version: 5.0.7
Commit: 556582d964
.NET SDKs installed:
5.0.301 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.16 [C:\ProgramFiles\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
startup.cs | ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
// Localization - adding resource path
services.AddLocalization(options =>
{
// We will put our translations in a folder called Resources
options.ResourcesPath = "Resources";
});
// Localization - look up MS docs
// Add framework services.
services.AddMvc()
// adding localization for views
.AddViewLocalization()
// e.g. [Display(Name = "Remember Me?")]
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
{
var assemblyName = new AssemblyName(typeof(CommonResources).GetTypeInfo().Assembly.FullName);
return factory.Create(nameof(CommonResources), assemblyName.Name);
};
});
// we are configuring the localization service to support a list of provided cultures.
services.Configure<RequestLocalizationOptions>(options =>
{
// Localization - the list of supported cultures
// combination of parent language and region
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("de-DE"),
new CultureInfo("en-GB"),
new CultureInfo("nl-NL")
};
// State what the default culture for your application is. This will be used if no
// specific culture can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture(
culture: supportedCultures[0],
uiCulture: supportedCultures[0]);
// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// UI strings that we have localized
options.SupportedUICultures = supportedCultures;
});
...
}
startup.cs | Configure
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider services)
{
...
app.UseRouting();
// Localization - middleware
// 1.QueryStringRequestCultureProvider – culture and ui - culture are passed in the query string.
// 2.CookieRequestCultureProvider – culture is set in the ASP.NET Core culture cookie.
// 3.AcceptLanguageHeaderRequestCultureProvider – which gets the culture from the browser’s language set by the user.
// specify that globalization is being used in the pipeline.
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
...
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
Culture Switcher Razor Component
<div>
<form id="culture-switcher">
<select name="culture" id="culture-options">
<option></option>
@foreach (var culture in Model.SupportedCultures)
{
<option value="@culture.Name" selected="@(Model.CurrentUICulture.Name == culture.Name)">@culture.ThreeLetterWindowsLanguageName</option>
}
</select>
</form>
</div>
<script>
document.getElementById("culture-options").addEventListener("change", () => {
document.getElementById("culture-switcher").submit();
});
</script>
CommonLocalizationService.cs
using KarriereWebseiteRazor.Resources;
using Microsoft.Extensions.Localization;
using System.Reflection;
namespace KarriereWebseiteRazor.Services
{
public class CommonLocalizationService
{
private readonly IStringLocalizer _localizer;
public CommonLocalizationService(IStringLocalizerFactory factory)
{
var assemblyName = new AssemblyName(typeof(CommonResources).GetTypeInfo().Assembly.FullName);
_localizer = factory.Create(nameof(CommonResources), assemblyName.Name);
}
public string Get(string key)
{
return _localizer[key];
}
}
}
也许实现您想要的最简单方法是更改 ViewComponent 中的 JavaScript 以设置 cookie:
const cultureOptions = document.getElementById("culture-options");
cultureOptions.addEventListener("change", () => {
const culture = cultureOptions.value;
document.cookie = `.AspNetCore.Culture=c=${culture}|uic=${culture}`;
location.reload()
});