<i id='ec4T5'><tr id='ec4T5'><dt id='ec4T5'><q id='ec4T5'><span id='ec4T5'><b id='ec4T5'><form id='ec4T5'><ins id='ec4T5'></ins><ul id='ec4T5'></ul><sub id='ec4T5'></sub></form><legend id='ec4T5'></legend><bdo id='ec4T5'><pre id='ec4T5'><center id='ec4T5'></center></pre></bdo></b><th id='ec4T5'></th></span></q></dt></tr></i><div id='ec4T5'><tfoot id='ec4T5'></tfoot><dl id='ec4T5'><fieldset id='ec4T5'></fieldset></dl></div>

    <legend id='ec4T5'><style id='ec4T5'><dir id='ec4T5'><q id='ec4T5'></q></dir></style></legend>

    <small id='ec4T5'></small><noframes id='ec4T5'>

      <tfoot id='ec4T5'></tfoot>

          <bdo id='ec4T5'></bdo><ul id='ec4T5'></ul>

        Asp.net core Localization 总是返回英语文化

        时间:2023-08-25

        <small id='nP5F0'></small><noframes id='nP5F0'>

          <legend id='nP5F0'><style id='nP5F0'><dir id='nP5F0'><q id='nP5F0'></q></dir></style></legend>

              <bdo id='nP5F0'></bdo><ul id='nP5F0'></ul>
              1. <tfoot id='nP5F0'></tfoot>

                    <tbody id='nP5F0'></tbody>

                  <i id='nP5F0'><tr id='nP5F0'><dt id='nP5F0'><q id='nP5F0'><span id='nP5F0'><b id='nP5F0'><form id='nP5F0'><ins id='nP5F0'></ins><ul id='nP5F0'></ul><sub id='nP5F0'></sub></form><legend id='nP5F0'></legend><bdo id='nP5F0'><pre id='nP5F0'><center id='nP5F0'></center></pre></bdo></b><th id='nP5F0'></th></span></q></dt></tr></i><div id='nP5F0'><tfoot id='nP5F0'></tfoot><dl id='nP5F0'><fieldset id='nP5F0'></fieldset></dl></div>
                  本文介绍了Asp.net core Localization 总是返回英语文化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试开发一个多语言项目.对于静态值,我使用了资源(.resx 文件)

                  I am trying to develop a multilanguage project.For static value I used resource(.resx file )

                  我创建了两个资源文件Home.resx(默认或英语)和 home.resx(用于阿拉伯语)它适用于默认或英语

                  I create two resources file Home.resx(default or English) and home.resx(for the Arabic language) it works for default or English

                  然后我尝试更改语言

                  System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ar");
                   var home = Resources.Home.Home1;
                  

                  但它仍然返回英文值而不是阿拉伯值

                  But it still return English value instead of Arabic value

                  这是我的 startup.cs 配置函数

                  here is my startup.cs Configure function

                  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
                          {
                  
                     var supportedCultures = new List<System.Globalization.CultureInfo>
                              {
                                  new System.Globalization.CultureInfo("en-US"),
                  
                                  new System.Globalization.CultureInfo("ar-AR"),
                  
                              };
                              var options = new RequestLocalizationOptions
                              {
                                  DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US"),
                                  SupportedCultures = supportedCultures,
                                  SupportedUICultures = supportedCultures
                              };
                              app.UseRequestLocalization(options);
                  ......
                  

                  我的代码有什么问题?

                  推荐答案

                  好的,我会告诉你我在项目中做了什么.在 Startup.cs 的 ConfigureServices 方法中的 services.AddMvc() 之后键入以下代码.

                  Ok, I will tell you what I do in my projects. Type the following code after services.AddMvc() in your ConfigureServices method of the Startup.cs.

                  IList<CultureInfo> supportedCultures = new List<CultureInfo>
                  {
                      new CultureInfo("en-US"),
                      new CultureInfo("fr-FR"),
                      new CultureInfo("el-GR"),
                  };
                  
                  var MyOptions = new RequestLocalizationOptions()
                  {
                      DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US"),
                      SupportedCultures = supportedCultures,
                      SupportedUICultures = supportedCultures
                  };
                  MyOptions.RequestCultureProviders = new[]
                  {
                       new RouteDataRequestCultureProvider() { RouteDataStringKey = "lang", Options = MyOptions }
                  };
                  
                  services.AddSingleton(MyOptions);
                  

                  现在在您的项目中定义以下类

                  Now define the following class in your project

                  public class LocalizationPipeline
                  {
                      public void Configure(IApplicationBuilder app, RequestLocalizationOptions options)
                      {
                          app.UseRequestLocalization(options);
                      }
                  }
                  

                  更改您的默认路由:

                  app.UseMvc(routes =>
                  {
                      routes.MapRoute(
                          name: "default",
                          template: "{lang=en-US}/{controller=Home}/{action=Index}/{id?}");
                  });
                  

                  为每个控制器使用 MiddlewareFilter.

                  In use the MiddlewareFilter for each of your controllers.

                  [MiddlewareFilter(typeof(LocalizationPipeline))]
                  public class HomeController : Controller
                  {
                      public string GetCulture()
                      {
                          return $"CurrentCulture:{CultureInfo.CurrentCulture.Name}, CurrentUICulture:{CultureInfo.CurrentUICulture.Name}";
                      }
                  }
                  

                  您可以像这样更改当前语言:

                  You can change the current language like this:

                  <ul>
                      <li>@Html.ActionLink("EN", ViewContext.RouteData.Values["action"] as string, ViewContext.RouteData.Values["controller"] as string, new { lang = "en-US" })</li>
                      <li>@Html.ActionLink("FR", ViewContext.RouteData.Values["action"] as string, ViewContext.RouteData.Values["controller"] as string, new { lang = "fr-FR" })</li>
                      <li>@Html.ActionLink("GR", ViewContext.RouteData.Values["action"] as string, ViewContext.RouteData.Values["controller"] as string, new { lang = "el-GR" })</li>
                  </ul>
                  

                  如果您还想支持 Razor Pages,请进行以下更改.添加以下更改 servcies.AddMvc().

                  If you want to support Razor Pages as well, make the following changes. Add the following change the servcies.AddMvc().

                  services.AddMvc()
                      .AddRazorPagesOptions(options =>
                      {
                          options.Conventions.AddFolderRouteModelConvention("/", model =>
                          {
                              foreach (var selector in model.Selectors)
                              {
                                  var attributeRouteModel = selector.AttributeRouteModel;
                                  attributeRouteModel.Template = AttributeRouteModel.CombineTemplates("{lang=el-GR}", attributeRouteModel.Template);
                              }
                          });
                      });
                  

                  在您的 PageModels 上使用以下 MiddlewareFilter 属性

                  Use the following MiddlewareFilter attribute over your PageModels

                  [MiddlewareFilter(typeof(LocalizationPipeline))]
                  public class ContactModel : PageModel
                  {
                      public string Message { get; set; }
                  
                      public void OnGet()
                      {
                          Message = "Your contact page.";
                      }
                  }
                  

                  我唯一没有做的事情是通过在 Startup.cs 中以编程方式添加 MiddlewareFilter 来为所有控制器和 PageModels 自动定义 LocalizationPipeline.

                  The only thing that I have not managed to do is to automatically define the LocalizationPipeline for all the controllers and PageModels by adding the MiddlewareFilter programmatically inside the Startup.cs.

                  这篇关于Asp.net core Localization 总是返回英语文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:根据办公产品的语言本地化 VSTO 插件 下一篇:CurrentThread.CurrentUICulture 设置正确,但似乎被 as

                  相关文章

                  最新文章

                1. <legend id='Pu1KL'><style id='Pu1KL'><dir id='Pu1KL'><q id='Pu1KL'></q></dir></style></legend>

                  1. <small id='Pu1KL'></small><noframes id='Pu1KL'>

                    1. <tfoot id='Pu1KL'></tfoot>

                      <i id='Pu1KL'><tr id='Pu1KL'><dt id='Pu1KL'><q id='Pu1KL'><span id='Pu1KL'><b id='Pu1KL'><form id='Pu1KL'><ins id='Pu1KL'></ins><ul id='Pu1KL'></ul><sub id='Pu1KL'></sub></form><legend id='Pu1KL'></legend><bdo id='Pu1KL'><pre id='Pu1KL'><center id='Pu1KL'></center></pre></bdo></b><th id='Pu1KL'></th></span></q></dt></tr></i><div id='Pu1KL'><tfoot id='Pu1KL'></tfoot><dl id='Pu1KL'><fieldset id='Pu1KL'></fieldset></dl></div>
                        <bdo id='Pu1KL'></bdo><ul id='Pu1KL'></ul>