【问题标题】:How to change the default culture?如何更改默认文化?
【发布时间】:2016-06-08 17:24:28
【问题描述】:

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

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

更新:

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

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

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

在旧的 asp.net 上,此配置是在带有元素的 .config 上完成的

text html

【问题讨论】:

  • 您能检查一下您的视图 (*.cshml) 文件使用的是什么编码吗?它们是 UTF-8 还是某种 ISO-8859-x 编码?
  • 您可以通过选择文件并转到文件>另存为...>使用编码保存...并选择“Unicode(带签名的UTF-8)”来检查它
  • 完美,成功了!
  • 谢谢你,没错!
  • 也看看这个问题:stackoverflow.com/questions/696627/…

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 2014-09-30
    • 2022-09-06
    • 1970-01-01
    • 2013-07-16
    • 2018-06-18
    • 1970-01-01
    相关资源
    最近更新 更多