如何更改默认文化?

时间:2016-06-08 17:24:29

标签: asp.net asp.net-mvc localization asp.net-core

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

text on html with accentuation problem decode

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

更新

我试图实施Joe的建议,但是我没有得到预期的结果,如图所示。

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

我不想要一个多语言应用程序,只需要一个português。

在旧的asp.net上,这个配置是在带有元素

的.config上完成的

text html

1 个答案:

答案 0 :(得分:4)

在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,如果您需要更多