Asp.net core Localization 总是返回英语文化 [英] Asp.net core Localization always returns english clulture

查看:26
本文介绍了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 总是返回英语文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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