ASP .NET Core默认语言始终为英语

编程入门 行业动态 更新时间:2024-10-18 21:25:33
本文介绍了ASP .NET Core默认语言始终为英语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我按照Microsoft博客中的说明设置了本地化,但是默认语言始终是英语.这是我的Startup.cs在本地化方面的样子.

I set the localization as described in Microsoft's blog, but the default language is always English. This is how my Startup.cs looks like with regards to the localization.

CultureInfo[] supportedCultures = new[] { new CultureInfo("ar"), new CultureInfo("en") };

在ConfigureServices方法中:

In ConfigureServices method:

services.Configure<RequestLocalizationOptions>(options => { options.DefaultRequestCulture = new RequestCulture("ar", "ar"); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; }); services.AddLocalization(options => { options.ResourcesPath = "Resources"; }); services.AddMvc() .AddViewLocalization() .AddDataAnnotationsLocalization();

在配置"方法中:

app.UseRequestLocalization(new RequestLocalizationOptions() { DefaultRequestCulture = new RequestCulture("ar"), SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures });

谢谢:)

推荐答案

您将阿拉伯语"设置为DefaultRequestCulture,但是如果没有任何内置提供程序可以确定请求区域性,则使用DefaultRequestCulture.默认提供程序为:

You are setting "arabic" as DefaultRequestCulture but DefaultRequestCulture is used if none of the built-in providers can determine the request culture. The default providers are:

  • QueryStringRequestCultureProvider
  • CookieRequestCultureProvider
  • AcceptLanguageHeaderRequestCultureProvider
  • QueryStringRequestCultureProvider
  • CookieRequestCultureProvider
  • AcceptLanguageHeaderRequestCultureProvider
  • 很有可能是根据浏览器正在发送的Accept-Language HTTP标头确定区域性的.

    Most likely the culture is determined from the Accept-Language HTTP header that the browser is sending.

    您必须删除AcceptLanguageHeaderRequestCultureProvider才能回退到DefaultRequestCulture.为此,我们可以覆盖RequestLocalizationOptions的RequestCultureProviders列表,并仅使用其他两个提供程序.在Startup.cs:

    You have to remove the AcceptLanguageHeaderRequestCultureProvider in order to fallback to DefaultRequestCulture. To do that, we can overwrite the RequestCultureProviders list of RequestLocalizationOptions and use only the other two providers. In Startup.cs:

    public void ConfigureServices(IServiceCollection services) { CultureInfo[] supportedCultures = new[] { new CultureInfo("ar"), new CultureInfo("en") }; services.Configure<RequestLocalizationOptions>(options => { options.DefaultRequestCulture = new RequestCulture("ar"); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; options.RequestCultureProviders = new List<IRequestCultureProvider> { new QueryStringRequestCultureProvider(), new CookieRequestCultureProvider() }; }); }

    和Configure方法中,只需在app.UseMvc();

    更多推荐

    ASP .NET Core默认语言始终为英语

    本文发布于:2023-11-17 08:38:59,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1609487.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:英语   语言   ASP   NET   Core

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!