Asp.net核心本地化总是返回英语语言 [英] Asp.net core Localization always returns english clulture

查看:110
本文介绍了Asp.net核心本地化总是返回英语语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个多语言项目.对于静态值,我使用了resource(.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核心本地化总是返回英语语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆