gpt4 book ai didi

asp.net - 如何更改默认文化?

转载 作者:行者123 更新时间:2023-12-02 19:53:21 52 4
gpt4 key购买 nike

我使用 ASP.NET Core 创建了我的第一个应用程序。当我调试它时,我发现带有重音符号的单词有问题:

text on html with accentuation problem decode

如何正确本地化应用程序?

更新:

我尝试实现 Joe 的建议,但没有得到您在此图中看到的预期结果。

从数据库显示的字符串没问题,但 View 模板中使用的字符串(如标题或文本)显示不正确。

我不需要多语言应用程序,只需要一个葡萄牙语应用程序。

在旧的 asp.net 上,此配置是在 .config 上使用 element 完成的

text html

最佳答案

在project.json中你需要这个依赖项

"Microsoft.Extensions.Localization": "1.0.0-rc2-final",

在ConfigureServices的Startup.cs中,您需要这样的代码:

    services.AddLocalization(options => options.ResourcesPath = "GlobalResources");

services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("en"),
new CultureInfo("fr-FR"),
new CultureInfo("fr"),
};

// 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: "en-US", uiCulture: "en-US");

// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;

// These are the cultures the app supports for UI strings, i.e. we have localized resources for.
options.SupportedUICultures = supportedCultures;

// You can change which providers are configured to determine the culture for requests, or even add a custom
// provider with your own logic. The providers will be asked in order to provide a culture for each request,
// and the first to provide a non-null result that is in the configured supported cultures list will be used.
// By default, the following built-in providers are configured:
// - QueryStringRequestCultureProvider, sets culture via "culture" and "ui-culture" query string values, useful for testing
// - CookieRequestCultureProvider, sets culture via "ASPNET_CULTURE" cookie
// - AcceptLanguageHeaderRequestCultureProvider, sets culture via the "Accept-Language" request header
//options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context =>
//{
// // My custom request culture logic
// return new ProviderCultureResult("en");
//}));
});

在配置中你需要这样的代码:

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

我有一些working demo code here ,如果您需要更多

关于asp.net - 如何更改默认文化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37709060/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com