如何更改默认文化?

Eri*_*lva 5 asp.net asp.net-mvc localization asp.net-core

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

\n\n

html 上的文本带有重音问题解码

\n\n

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

\n\n

更新:

\n\n

我尝试实施乔的建议,但我没有得到预期的结果,正如您在此图中看到的那样。

\n\n

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

\n\n

我不需要多语言应用程序,只需要 portugu\xc3\xaas 中的一个。

\n\n

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

\n\n

文本 html

\n

Joe*_*tte 4

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

"Microsoft.Extensions.Localization": "1.0.0-rc2-final",
Run Code Online (Sandbox Code Playgroud)

在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");
            //}));
        });
Run Code Online (Sandbox Code Playgroud)

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

var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
        app.UseRequestLocalization(locOptions.Value);
Run Code Online (Sandbox Code Playgroud)

这里有一些工作演示代码,如果您需要更多