問題描述
我正在嘗試開發(fā)一個多語言項(xiàng)目.對于靜態(tài)值,我使用了資源(.resx 文件)
I am trying to develop a multilanguage project.For static value I used resource(.resx file )
我創(chuàng)建了兩個資源文件Home.resx(默認(rèn)或英語)和 home.resx(用于阿拉伯語)它適用于默認(rèn)或英語
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 配置函數(shù)
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);
......
我的代碼有什么問題?
推薦答案
好的,我會告訴你我在項(xiàng)目中做了什么.在 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);
現(xiàn)在在您的項(xiàng)目中定義以下類
Now define the following class in your project
public class LocalizationPipeline
{
public void Configure(IApplicationBuilder app, RequestLocalizationOptions options)
{
app.UseRequestLocalization(options);
}
}
更改您的默認(rèn)路由:
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}";
}
}
您可以像這樣更改當(dāng)前語言:
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,請進(jìn)行以下更改.添加以下更改 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.
這篇關(guān)于Asp.net core Localization 總是返回英語文化的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!