Asp.net核心本地化总是返回英文clulture

时间:2017-11-20 18:09:32

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

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

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

然后我尝试改变语言

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

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

这是我的startup.cs配置功能

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);
......

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:2)

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

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);

现在在项目中定义以下类

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。

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

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

<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()。

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属性
[MiddlewareFilter(typeof(LocalizationPipeline))]
public class ContactModel : PageModel
{
    public string Message { get; set; }

    public void OnGet()
    {
        Message = "Your contact page.";
    }
}

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